CANNON.js not detecting collision between Trimesh and sphere

I’m using Perlin noise to create a terrain automatically in three.js. I then create a physics body with that mesh using Trimesh and get vertices and indices from the geometry. However for some reason when I drop a sphere on it then it just falls right through it. The body shape exists because I create another mesh with the vertices found in the body to make sure that it is made properly. Here is my tri mesh code:

var platShape = CreateTrimesh(platformGeometry, platform)
var platBody = new CANNON.Body({ mass: 0, shape: platShape });
copy(platform, platBody)
world.addBody(platBody)
//createThree(platBody)

function buffer(geo) {
    const bufferedGeo = new THREE.BufferGeometry().fromGeometry(geo);
    return bufferedGeo;
}

function CreateTrimesh(geometry) {
    const vertices = buffer(geometry).attributes.position.array;

    // Each triangle is defined by three consecutive vertices
    const numTriangles = vertices.length / 9; // Each triangle has 3 vertices (3 components each)
    const triangleIndices = [];
    for (let i = 0; i < numTriangles; i++) {
        triangleIndices.push([i * 3, i * 3 + 1, i * 3 + 2]);
    }

    return new CANNON.Trimesh(vertices, triangleIndices);
}

Also it works sometimes depending on a bunch of weird factors like where the sphere is, and if its moving horizontally and stuff.

If anyone needs more info then feel free to ask

platBody.collisionResponse = 0;

looks suspicious…

Sorry that was left there from a previous test. Forgot to remove it.

1 Like

hey im having the same issue did you figure out hoe to fix it

1 Like

Physics sims can be really hard… and easy to break… and once you break them, tricky to Unbreak.
The main ways that physics sims break is when you violate the constraints somehow… like starting things out with dynamic objects intersecting…
The other way they break is by using units that are out of the “safe” range for the engine… Making masses too high or too small… making objects too big or too small. Especially making both too big and too small at the same time.
Another way they break is by objects moving too fast relative to the thickness of other objects… so that they “tunnel” through each other, and end up interpenetrating.

1 Like
  • value of noise sample at edge/repeat may be misaligned (self-intersection, hole, NaN)

  • async/await the terrain algo result

  • MergeVertices to make mesh topology robust

  • test terrain trimesh by CSG subtraction from a box… Is outcome inconsistently reversed or upside-down?

  • …if so, this may explain why “sideways” force contacts other bodies

  • increase mass/area, and geometry should have depth

  • include a generic floor in collision group to avert complete failure

  • test terrain collision by augmenting faces with primitives

It’s complicated,
Ambassk8r Truckstop

2 Likes