Ammo Heightfield Terrain Shape

@MackeyK24 @regna

Now that I’m thinking about this, I’m pretty sure I ran into this same problem before a long time ago. The problem is that the vertices are in some format or order that doesn’t match the order you’re feeding into Ammo.js.

It’s mostly fixed here:

These are the relevant changes:

// line 495
const numberOfRows = subdivisions + 1;
const numberOfCols = subdivisions + 1;

// line 507
for (let i = 0; i < numberOfRows; i++) {
    // for (let j = 0; j < numberOfCols; j++) {
    for (let j = numberOfCols - 1; j > -1; j--) {
        height = heightData[(i * numberOfCols + j)*3 + 1];
        ammoInstance.HEAPF32[ammoHeightData + p2 >> 2] = height;
        p2 += 4;
    }
}

//line 524
let flipQuadEdges = true; // not sure why, but phys surface appears to fit better this way

// line 565
const originY = (maxHeight - minHeight) * 0.5 - 2; // I forgot why this adjustment is necessary, and the value 2 was just a best guess on my part. I think there's also a slight x and z origin tweaking needed as well.
1 Like