LWJGL Mesh to JBullet collider
<p>I'm working on creating a voxel engine in LWJGL 3, I have all the basics down (chunks, mesh rendering, etc).</p>

<p>Now I'm working on adding physics using JBullet. This is my first time using JBullet directly, but I've used Bullet before in other 3D engines.</p>

<p>From <a href="http://www.java-gaming.org/index.php/topic,22894.0" rel="nofollow noreferrer">here</a> I gathered that all I needed to do to create a collision object the same shape as my mesh was the plug the vertices and indices into a <code>TriangleIndexVertexArray</code> and use that for a <code>BvhTriangleMeshShape</code>.</p>

<p>Here is my code:</p>

<pre><code>    float[] coords = mesh.getVertices();
    int[] indices = mesh.getIndices();

    if (indices.length &gt; 0) {
        IndexedMesh indexedMesh = new IndexedMesh();
        indexedMesh.numTriangles = indices.length / 3;
        indexedMesh.triangleIndexBase = ByteBuffer.allocateDirect(indices.length*Float.BYTES).order(ByteOrder.nativeOrder());
        indexedMesh.triangleIndexBase.asIntBuffer().put(indices);
        indexedMesh.triangleIndexStride = 3 * Float.BYTES;
        indexedMesh.numVertices = coords.length / 3;
        indexedMesh.vertexBase = ByteBuffer.allocateDirect(coords.length*Float.BYTES).order(ByteOrder.nativeOrder());
        indexedMesh.vertexBase.asFloatBuffer().put(coords);
        indexedMesh.vertexStride = 3 * Float.BYTES;

        TriangleIndexVertexArray vertArray = new TriangleIndexVertexArray();
        vertArray.addIndexedMesh(indexedMesh);

        boolean useQuantizedAabbCompression = false;
        BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(vertArray, useQuantizedAabbCompression);

        CollisionShape collisionShape = meshShape;

        CollisionObject colObject = new CollisionObject();
        colObject.setCollisionShape(collisionShape);
        colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x, position.y, position.z), 1f)));
        dynamicsWorld.addCollisionObject(colObject);

    } else {
        System.err.println("Failed to extract geometry from model. ");
    }
</code></pre>

<p>I know that the vertices and indices are valid as I'm getting them here after drawing my mesh.</p>

<p>This seems to somewhat work, but when I try to drop a cube rigidbody onto the terrain, it seems to collide way above the terrain! (I know that the cube is setup correctly because if I remove the mesh collider it hits the base ground plane at <code>y=0</code>).</p>

<p><a href="https://i.stack.imgur.com/5ac3v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5ac3v.png" alt="enter image description here"></a></p>

<p>I thought maybe it was a scaling issue (although I don't see how that could be), so I tried changing:</p>

<p><code>colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x, position.y, position.z), 1f)));</code> to:</p>

<p><code>colObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(position.x, position.y, position.z), 0.5f)));</code></p>

<p>But after changing the scale from <code>1</code> it acted like the mesh collider didn't exist. </p>

<p>It's hard to find any resources or code for JBullet surrounding mesh collision, and I've been working on this for almost 2 days, so I'm hoping maybe some of you people who have done it before can help me out :)</p>

<p><strong>Update 1:</strong></p>

<p>I created an implementation of the IDebugDrawer so I could draw the debug infomation in the scene. </p>

<p>To test it I ran it with just a basic ground plane and a falling cube. I noticed that when the cube is falling the aabb matches the cube size, but when it hits the floor the aabb becomes significantly larger then it was. </p>

<p><a href="https://i.stack.imgur.com/ECDad.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ECDad.png" alt="enter image description here"></a></p>

<p>I'm going to assue that this is normal Bullet behavior due to collition bouncing, and look at that later as it doesn't effect my current problem.</p>

<p>I re-enabled the generation of the colliders from the chunk meshs, and saw this:</p>

<p><a href="https://i.stack.imgur.com/HFLiF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HFLiF.png" alt="enter image description here"></a></p>

<p>It appears that the aabb visualization of the chunk is a lot higher then the actual chunk (I know my y positioning of the overall collision object is correct). </p>

<p><a href="https://i.stack.imgur.com/Md13X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Md13X.png" alt="enter image description here"></a></p>

<p>I'm going to try to figure out if I can draw the actual collision mesh or not.</p>

<p><strong>Update 2:</strong></p>

<p>As far as I can see looking at the source, the meshof the colliders should be drawing in debug, so I'm not sure why it isn't. </p>

<p>I tried changing the Box rigidbody to a sphere, and it actually rolled across the top of the visualized aabb for the terrain collider. It just rolled flat though and didn't go hit or down where there where hills or dips in the terrain where, so it was obviously just rolling across the flat top of the aabb.</p>