Collision with box and mesh

AshMcConnell
Posts: 29
Joined: Sat Sep 23, 2006 1:35 pm
Location: Northern Ireland

Collision with box and mesh

Post by AshMcConnell »

Hi Folks,

I am having trouble getting a mesh and a box to collide. When i change to a box / box it works fine.

I have used a btTriangleIndexVertexArray object which I put into the contructor of the btBvhTriangleMeshShape object.

I checked the AABB and it seems reasonable, If i replace the collision shape in the collision object with a btBoxShape a collision occurs correctly, but normally absolutely no collision is detected.

Any idea what I could have done wrong?

Thanks for your help
All the best,
Ash
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

It is best to first use btTriangleMesh, instead of btTriangleIndexVertexArray.

The btTriangleIndexVertexArray is more complicated to setup: with btTriangleMesh you can easily add one triangle at a time.

Performance is the same, both use the same interface and can be added to btBvhTriangleMeshShape.

Erwin
AshMcConnell
Posts: 29
Joined: Sat Sep 23, 2006 1:35 pm
Location: Northern Ireland

Post by AshMcConnell »

Hi Erwin,

Thanks for the quick reply.

I should have mentioned I initially started using the btTriangleMesh, but then switched to use the btTriangleIndexVertexArray when it didn't work. I must have done something very silly. I just can't quite track it down :(

Thanks anyway!
All the best,
Ash
AshMcConnell
Posts: 29
Joined: Sat Sep 23, 2006 1:35 pm
Location: Northern Ireland

Post by AshMcConnell »

Are there any collision flags I need to set for a mesh by any chance? (that would be set by default on the box shape)

Ash - grasping at straws ;)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

If btTriangleMesh works, please use that. No special flags are needed for mesh.
AshMcConnell
Posts: 29
Joined: Sat Sep 23, 2006 1:35 pm
Location: Northern Ireland

Post by AshMcConnell »

Hi Erwin,

As suspected, such a stupid mistake :)

I was loading in a "Track" mesh, whereas the car was landing on the "Grass" mesh :oops:

I think i'll retire from coding and become a goat herder ;)

It's always the obvious things one misses :)

Thanks for your help, sorry for wasting your time.
All the best,
Ash
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Re: Collision with box and mesh

Post by nomad »

AshMcConnell wrote:Hi Folks,


I have used a btTriangleIndexVertexArray object which I put into the contructor of the btBvhTriangleMeshShape object.
I had a few problems setting up a btTriangleIndexVertexArray myself. If you've got a working btTriangleIndexVertexArray mesh, I'd be interested to know what values you used in the btIndexedMesh struct, especially the stride values.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision with box and mesh

Post by Erwin Coumans »

nomad wrote:
AshMcConnell wrote:Hi Folks,


I have used a btTriangleIndexVertexArray object which I put into the contructor of the btBvhTriangleMeshShape object.
I had a few problems setting up a btTriangleIndexVertexArray myself. If you've got a working btTriangleIndexVertexArray mesh, I'd be interested to know what values you used in the btIndexedMesh struct, especially the stride values.
There is a working example in Bullet:
Demos\ConcaveDemo\ConcavePhysicsDemo.cpp

The 3 indices for a single triangle are expected to be in contiguous memory, and the indexstride points to the start index for the next triangle.
Typically this is 3 * sizeof(int), if each triangle has its own indices, and no stripping/fanning happens. Remember that btTriangleIndexVertexArray just points to mesh (vertex/index) data, so don't delete this mesh data until the simulation ends.

But for most cases it's better to just stick with btTriangleMesh, instead of re-using the graphics data. Compare the much simpler btTriangleMesh setup:

Code: Select all

	btTriangleMesh* trimesh = new btTriangleMesh();

	for ( i=0;i<NUM_VERTS_X-1;i++)
	{
		for (int j=0;j<NUM_VERTS_Y-1;j++)
		{
			trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[j*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i+1]);
			trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[(j+1)*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i]);
		}
	}
To the advanced and more complex graphics re-use in:

Code: Select all

	int vertStride = sizeof(btVector3);
	int indexStride = 3*sizeof(int);

	const int NUM_VERTS_X = 50;
	const int NUM_VERTS_Y = 50;
	const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
	
	const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1);

	btVector3*	gVertices = new btVector3[totalVerts];
	int*	gIndices = new int[totalTriangles*3];

	int i;

	for ( i=0;i<NUM_VERTS_X;i++)
	{
		for (int j=0;j<NUM_VERTS_Y;j++)
		{
			gVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,
				//0.f,
				2.f*sinf((float)i)*cosf((float)j),
				(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
		}
	}

	int index=0;
	for ( i=0;i<NUM_VERTS_X-1;i++)
	{
		for (int j=0;j<NUM_VERTS_Y-1;j++)
		{
			gIndices[index++] = j*NUM_VERTS_X+i;
			gIndices[index++] = j*NUM_VERTS_X+i+1;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;

			gIndices[index++] = j*NUM_VERTS_X+i;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i;
		}
	}
	
	btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
		gIndices,
		indexStride,
		totalVerts,(float*) &gVertices[0].x(),vertStride);

	btCollisionShape* trimeshShape  = new btBvhTriangleMeshShape(indexVertexArrays);
AshMcConnell
Posts: 29
Joined: Sat Sep 23, 2006 1:35 pm
Location: Northern Ireland

Re: Collision with box and mesh

Post by AshMcConnell »

nomad wrote:
AshMcConnell wrote:Hi Folks,


I have used a btTriangleIndexVertexArray object which I put into the contructor of the btBvhTriangleMeshShape object.
I had a few problems setting up a btTriangleIndexVertexArray myself. If you've got a working btTriangleIndexVertexArray mesh, I'd be interested to know what values you used in the btIndexedMesh struct, especially the stride values.
I had a look at the code here: -

http://continuousphysics.com/Bullet/php ... .php?t=769

I am using Ogre + Bullet Collision + a friend's physics
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Post by nomad »

I'm working on a Bullet - Irrlicht demo. It is a basic framework that loads meshes and attaches the main Bullet collision shapes to them, hopefully it will be helpful to Irrlicht users.
I thought it would be more efficient to re-use the Irrlicht vertex and index data, when it didn't work I assumed I'd got the btTriangleIndexVertexArray wrong. The problem was the way the Irrlicht index data is arranged, and how I was using it.
I'd have to re-organise the index data to use it in a btTriangleIndexVertexArray, so it is better for me to use a btTriangleMesh.
Sorry, didn't realise there was a btTriangleIndexVertexArray example (bangs head repeatedly against wall chanting "search the source code" :oops: )

Thanks.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

I'm curious how the index-layout is of Irrlicht? Can you describe what the incompatibility is?

Did you know you can add several subparts to the btTriangleIndexVertexArray? This might help if there are multiple sets of indices.

Thanks,
Erwin

nomad wrote:I'm working on a Bullet - Irrlicht demo. It is a basic framework that loads meshes and attaches the main Bullet collision shapes to them, hopefully it will be helpful to Irrlicht users.
I thought it would be more efficient to re-use the Irrlicht vertex and index data, when it didn't work I assumed I'd got the btTriangleIndexVertexArray wrong. The problem was the way the Irrlicht index data is arranged, and how I was using it.
I'd have to re-organise the index data to use it in a btTriangleIndexVertexArray, so it is better for me to use a btTriangleMesh.
Sorry, didn't realise there was a btTriangleIndexVertexArray example (bangs head repeatedly against wall chanting "search the source code" :oops: )

Thanks.
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Post by nomad »

Yes, there are multiple sets of indices and using subparts would solve my problem, i.e. instead of having to extract all the index / vertex data to temporary buffers. There should be no reason why the Irrlicht index / vertex data cannot be used directly. I'll give it a go now I have btTriangleIndexVertexArrays working.
Regarding the data layout, there are 3 unique vertices for each triangle, no vertex re-use (when loading 3ds files... not sure about other formats) i.e. same number of vertices and indices. Somewhat inefficient, but not a compatibility problem.

Thanks.
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Post by nomad »

Erwin Coumans wrote:I'm curious how the index-layout is of Irrlicht? Can you describe what the incompatibility is?
There does seem to be an incompatibility with the index layout in Irrlicht.
Irrlicht indices are stored as arrays of unsigned shorts, and Bullet won't accept an index array of 16 bit values. I think there is a byte-alignment issue here.
So to use the data in a btTriangleIndexVertexArray I need to first copy the index values to an int array, which works fine. But it's easier to just use btTriangleMeshes instead.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

nomad wrote:
Erwin Coumans wrote:I'm curious how the index-layout is of Irrlicht? Can you describe what the incompatibility is?
There does seem to be an incompatibility with the index layout in Irrlicht.
Irrlicht indices are stored as arrays of unsigned shorts, and Bullet won't accept an index array of 16 bit values. I think there is a byte-alignment issue here.
So to use the data in a btTriangleIndexVertexArray I need to first copy the index values to an int array, which works fine. But it's easier to just use btTriangleMeshes instead.
It's best to use btTriangleMesh indeed. Bullet used to support 16 bits indices before, it was never used and probably disappeared during refactoring. We can add it back in, if desperately needed.
nomad
Posts: 32
Joined: Sun Jun 18, 2006 10:22 pm

Post by nomad »

Erwin Coumans wrote: It's best to use btTriangleMesh indeed. Bullet used to support 16 bits indices before, it was never used and probably disappeared during refactoring. We can add it back in, if desperately needed.
Thanks, but I'll just use btTriangleMeshes - if I need btTriangleIndexVertexArrays, it would probably just be easier to change the index layout in Irrlicht anyway. I'm really just working out how best to use Bullet at this stage.