JBullet TriangleIndexVertexArray troubles

Help!!!

I’m trying to import my static terrain objects into JBullet, but can’t seem to get the TriangleIndexVertexArray correctly setup. I THINK my problem is setting up the index.

So… I have my triangle geometry stored in a ByteBuffer like this:

xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz xyzxyzxyz …

(with no spaces of course. I just put them there so you see the individual triangles in my format)

This is my code to import it into JBullet:


int numtriangles = entity.getDrawObject().getPolygonCount();
int numverts = numtriangles * 3;
indexBuffer = BufferUtils.createByteBuffer(numverts * 3 * 4).order(ByteOrder.nativeOrder());
indexBuffer.clear();
for (int i=0;i<numtriangles;i++) {
	indexBuffer.putInt(i);
	indexBuffer.putInt(i);
	indexBuffer.putInt(i);
}
geometry = BufferUtils.createByteBuffer(numtriangles * 3 * 4).order(ByteOrder.nativeOrder());
geometry.clear();
for (int i=0;i<numtriangles*3;i++) {
	geometry.putFloat(entity.getDrawObject().getCurrentGeometry().get(i)*entity.getScale());
}
geometry.rewind();
indexBuffer.rewind();

TriangleIndexVertexArray trimesh = new TriangleIndexVertexArray(numtriangles, indexBuffer, 3 * 4, numtriangles, geometry, 3 * 4);
BvhTriangleMeshShape trimeshshape = new BvhTriangleMeshShape(trimesh,true);

I’ve tried to figure out how the index is supposed to be setup from the ConvcaveDemo.java file, but I’m completely lost. I’m also not sure if I have my strides correct or not at this point (as I’ve experimented so much, I’ve forgotten where I’ve been).

Could someone please set me straight?

Ugh… there’s another possibility…

I think I may be setting it up correctly, BUT the Bullet physics library doesn’t support collisions between static and kinematic objects. I just read on another forum that you have to implement that collision handling yourself.

Is that true? If so, I may have to go back to doing my own “simpler” collision detection and handling.

Yes, BvhTriangleMeshShape is only for static geometry, you can use GImpactMeshShape for dynamic trimeshes. However as they lack actual volume they’re prone to tunneling and are computationally slow. Best is to use simpler geometry, like ConvexHullShape or so.

I’m going to say this as often as it takes:
[x] Start small
[x] Get it to work
[x] Expand on it
[x] Verify it works every step you take

You are not even sure what your problem is… TriangleIndexVertexArray (packing/indexing) / JBullet.

Let me help you a bit…


for (int i=0;i<numtriangles;i++) {
	indexBuffer.putInt(i);
	indexBuffer.putInt(i);
	indexBuffer.putInt(i);
}

This is clearly wrong: every index refers to a vertex not a triangle, so your 3 indices-to-vertices in your triangle are pointing to the same memory location: the surface area of your triangle is zero.

Please, if something doesn’t work, simplify. Try to get it to work with a cube first. Work from there.

[quote]Please, if something doesn’t work, simplify. Try to get it to work with a cube first. Work from there.
[/quote]
Actually, I AM trying just to get it to work with a cube. :stuck_out_tongue:
And I think that it is in fact working (as I can query and see the collision occurring)… it’s just that Bullet doesn’t do anything about it. But I’ll keep fiddling with that index like you said.

EDIT: I tried setting the index up to point at each vertice (basically a straight count from 0 to X). I get an outofrange error from the JBullet library when I do that though.

[quote]Yes, BvhTriangleMeshShape is only for static geometry, you can use GImpactMeshShape for dynamic trimeshes. However as they lack actual volume they’re prone to tunneling and are computationally slow. Best is to use simpler geometry, like ConvexHullShape or so.
[/quote]
My movable object is a Kinematic Character Controller (and I’ve tried using convexhull, box, and sphere for the collision shape), and the cubes I’m checking it against are the only static trimeshes. For some crazy reason I figured that Bullet would do some collision handling in that situation.

What I’m eventually wanting to do is take my terrain objects and import their geometry into JBullet, and then have a character that walks via user control and doesn’t step into the imported items.

Here is the code I’m using. Haven’t cleaned it up so it contains some mysterious code but I hope it still helps:


            float[] coords = extractor.getCoords();
            int[] indices = extractor.getIndices();

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

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

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

                //init(meshShape);
                //setMass(0);

                this.collisionShape = meshShape;
                Vector3f localInertia = new Vector3f(0, 0, 0);
                RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0, this, collisionShape, localInertia);
                body = new RigidBody(rbInfo);
                //body.setMassProps(0, localInertia);
                body.setUserPointer(this);

                body.setCollisionFlags(body.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);
                body.setActivationState(CollisionObject.DISABLE_DEACTIVATION);

                sys.addCollider(this);
            } else {
                System.err.println("Failed to extract geometry from model. "+getSimulationObject());
            }


sys.addCollider adds the RigidBody to the DynamicsWorld.
coords contains xyzxyzxyz etc coordinates.
One triange in indices would be 0,1,2.

Thank you Tom. I implemented it exactly like you said (literally copying your code block and fitting it together into my own stuff), but I get this when I run it:


**36 - 36
0 = 300.0
1 = 25.0
2 = 300.0
3 = 300.0
4 = 25.0
5 = -300.0
6 = -300.0
7 = 25.0
8 = -300.0
9 = 300.0
10 = 25.0
11 = 300.0
12 = -300.0
13 = 25.0
14 = -300.0
15 = -300.0
16 = 25.0
17 = 300.0
18 = -300.0
19 = -25.0
20 = -300.0
21 = -300.0
22 = -25.0
23 = 300.0
24 = -300.0
25 = 25.0
26 = 300.0
27 = -300.0
28 = -25.0
29 = -300.0
30 = -300.0
31 = 25.0
32 = 300.0
33 = -300.0
34 = 25.0
35 = -300.0
Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.nio.Buffer.checkIndex(Buffer.java:520)
	at java.nio.DirectByteBuffer.getFloat(DirectByteBuffer.java:832)
	at com.bulletphysics.collision.shapes.ByteBufferVertexData.getVertex(ByteBufferVertexData.java:58)
	at com.bulletphysics.collision.shapes.VertexData.getTriangle(VertexData.java:53)
	at com.bulletphysics.collision.shapes.StridingMeshInterface.internalProcessAllTriangles(StridingMeshInterface.java:51)
	at com.bulletphysics.collision.shapes.StridingMeshInterface.calculateAabbBruteForce(StridingMeshInterface.java:78)
	at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:76)
	at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:63)
	at com.tommyengine.physics.jbullet.JBObjectStatic.createCollisionShape(JBObjectStatic.java:104)
	at com.tommyengine.physics.jbullet.JBObjectStatic.establishPhysicality(JBObjectStatic.java:120)
	at com.tommyengine.physics.jbullet.JBObjectStatic.<init>(JBObjectStatic.java:43)
	at com.tommyengine.tests.ObjectViewer.startloop(ObjectViewer.java:475)
	at com.tommyengine.tests.ObjectViewer.startloop(ObjectViewer.java:646)
	at com.tommyengine.tests.ObjectViewer.startloop(ObjectViewer.java:646)
	at com.tommyengine.tests.ObjectViewer.begin(ObjectViewer.java:198)
	at com.tommyengine.tests.ObjectViewer.main(ObjectViewer.java:123)

I’m trying to import a box made of triangles. The above numbers are the index = coordinate. The very top number is the total number of indices and coordinates. The line that generates the outofbounds exception is:

Any ideas why I’m getting this error? It’s been driving me crazy!

I GOT IT! I GOT IT! I GOT IT!!!

My problem was seeing the definition of vertice for the index!!! The index points to a Vector, not each individual axis coordinate (as I was doing).

Ugh. Thank you guys for helping with this!