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