Search code examples
java3dvertex-bufferjmonkeyengine

Collision detection fails after updating vertex buffer


Using jMonkeyEngine, I am updating specific vertices of a mesh by modifying the vertexbuffer. That all works fine. However, I've noticed that collision detection no longer works on the moved bit of 'mesh' (mesh in quotes b/c although I am using the Mesh object the mesh consists of a few quads all using their own vertices - they do not touch).

Stranger still, when I click where the quad used to be the listener activates. It's as if there is something not updated in the background.

Here is the code I am using to update the vertexbuffer - simplified for this test case:

Mesh m = results.getCollision(i).getGeometry().getMesh();    

VertexBuffer vb = m.getBuffer(Type.Position);
float[] vert = BufferUtils.getFloatArray((FloatBuffer) vb.getData());
vert[2]-=1; //move the first quad down with each click ... 
vert[5]-=1;
vert[8]-=1;
vert[11]-=1;
vb.updateData(BufferUtils.createFloatBuffer(vert));

Does anyone know why the listener does not detect a click on the bit of mesh that has been moved?


Solution

  • After changing the mesh of the geometry you need to call

    yourSpatial.updateModelBound();
    

    The reason is that the bounding volume is the first thing collision is checked against (because it's cheaper). If it detects a hit, it will proceed to check the mesh. This is why it detects a hit where the mesh used to be.