Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JBullet TriangleIndexVertexArray troubles  (Read 3141 times)
0 Members and 1 Guest are viewing this topic.
Offline elias4444

Junior Devvie





« Posted 2010-09-02 16:38:36 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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?

Offline elias4444

Junior Devvie





« Reply #1 - Posted 2010-09-02 17:32:33 »

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.

Offline jezek2
« Reply #2 - Posted 2010-09-02 19:43:13 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-09-02 21:20:36 »

Could someone please set me straight?

I'm going to say this as often as it takes:
  • Start small
  • Get it to work
  • Expand on it
  • 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...
1  
2  
3  
4  
5  
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline elias4444

Junior Devvie





« Reply #4 - Posted 2010-09-03 00:09:00 »

Quote
Please, if something doesn't work, simplify. Try to get it to work with a cube first. Work from there.
Actually, I AM trying just to get it to work with a cube.  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.
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.

Offline tom
« Reply #5 - Posted 2010-09-03 09:50:59 »

Here is the code I'm using. Haven't cleaned it up so it contains some mysterious code but I hope it still helps:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
            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.

Offline elias4444

Junior Devvie





« Reply #6 - Posted 2010-09-03 22:59:10 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
**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:
Quote
BvhTriangleMeshShape trimeshshape = new BvhTriangleMeshShape(vertArray,true);

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

Offline elias4444

Junior Devvie





« Reply #7 - Posted 2010-09-03 23:21:02 »

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!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!