Newbie question - Advice on collision shapes
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Newbie question - Advice on collision shapes
Hi guys im totally new to Bullet and have been trying to implement it into a racing game that I am making with directx. As far as I can tell there aren't many resources out there for help with integrating Bullet with directx but thats another problem for another time. As for now im trying to plan ahead a little, now for my car itself I figure I can just start off easy and do a btBoxShape. The problem lies in that the track I will be using will probably be in the region of 100,000 polys, so I have two questions. Firstly what form of collision shape should I use for my track mesh? I did notice the following btScaledBvhTriangleMeshShape, btBvhTriangleMeshShape and btConvexHullShape. I take it that (as the track might be scaled to a suitable size) btScaledBvhTriangleMeshShape would be best to use for the track and btConvexHullShape would be better for the car at a later date. The thing is wont using the btScaledBvhTriangleMeshShape for a 100,000 poly track severely slow things down? Which leads to my second question. I saw somewhere to also have a low poly verion of the track represented on the same space that doesnt get rendered and the collision is done on that. Is this a good way to go about things?
Any help and advice would be much appreciated.
Any help and advice would be much appreciated.
-
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: Newbie question - Advice on collision shapes
Use btBvhTriangleMeshShape for the track. Don't scale the track at run-time, that's dumb. Scale it in your modeller. Don't bother with low detail meshes in the physics world, it will just complicate things.
For the cars, the best thing to do is to chop them up manually in to convex hulls and use a btCompoundShape to hold the hulls. You can also put other primitives (spheres, cones, boxes, cylinders, etc) in the compound shape if they are a reasonable approximation of parts of the vehicle. Another approach is to use a btGimpactShape which is an arbitrary triangle mesh. This is actually very similar to just putting lots of btTriangleShapes into a btCompoundShape, but is probably faster. You will have to use a special physics mesh for the car though, for the following reasons:
Both approaches are somewhat of an art because you want to avoid individual parts of the compound being too small, for tunneling and stability reasons. For instance putting a small box / sphere / triangle on the front of the car will cause your car to get jammed inside walls if you ram them at any speed. When you get these sorts of bugs, it should be clear how to adjust your physics shapes to correct the behaviour. If you get performance problems, reduce the number of triangles / compound parts involved.
For the cars, the best thing to do is to chop them up manually in to convex hulls and use a btCompoundShape to hold the hulls. You can also put other primitives (spheres, cones, boxes, cylinders, etc) in the compound shape if they are a reasonable approximation of parts of the vehicle. Another approach is to use a btGimpactShape which is an arbitrary triangle mesh. This is actually very similar to just putting lots of btTriangleShapes into a btCompoundShape, but is probably faster. You will have to use a special physics mesh for the car though, for the following reasons:
Both approaches are somewhat of an art because you want to avoid individual parts of the compound being too small, for tunneling and stability reasons. For instance putting a small box / sphere / triangle on the front of the car will cause your car to get jammed inside walls if you ram them at any speed. When you get these sorts of bugs, it should be clear how to adjust your physics shapes to correct the behaviour. If you get performance problems, reduce the number of triangles / compound parts involved.
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
Thanks for the help I will be sure to use it when I cross that bridge.
I do have another question now though. So after alot of tedious work iv finally implemented Bullet into my game and its working! I just have 2 btBoxShape's and gravity is applied and I can tell they are in the scene because I have directx rendering meshes at their RigidBody->getWorldTransform() so can see the meshes falling. Also I have applied a very basic accelerator where the user presses the up key and I increase the rigidBody->setLinearVelocity which currently makes it move forward. Now, how do I rotate the rigid body and retrieve the direction it is facing to use with the accelerator?
Also please do tell me if im going about accessing these in the wrong way as im still learning and I do appreciate constructive criticism.
Thanks in advance.
I do have another question now though. So after alot of tedious work iv finally implemented Bullet into my game and its working! I just have 2 btBoxShape's and gravity is applied and I can tell they are in the scene because I have directx rendering meshes at their RigidBody->getWorldTransform() so can see the meshes falling. Also I have applied a very basic accelerator where the user presses the up key and I increase the rigidBody->setLinearVelocity which currently makes it move forward. Now, how do I rotate the rigid body and retrieve the direction it is facing to use with the accelerator?
Also please do tell me if im going about accessing these in the wrong way as im still learning and I do appreciate constructive criticism.
Thanks in advance.
-
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: Newbie question - Advice on collision shapes
It's better to apply impulses and torqueImpulses until you know what you're doing. If you want the impulse to be applied over several 'ticks' of the engine, which will be occuring within a single call to stepSimulation, then use applyForce and applyTorque instead of applyImpulse and applyTorqueImpulse.
In order to work out the direction, you need to learn about quaternions and transforms, but there are nice bullet classes for dealing with them that encapsulate all the tricky algorithms so ultimately you just need to call getOrientation() on your rigid body and multiply the returned quaternion against the vector in object space to rotate it into world space, for example.
In order to work out the direction, you need to learn about quaternions and transforms, but there are nice bullet classes for dealing with them that encapsulate all the tricky algorithms so ultimately you just need to call getOrientation() on your rigid body and multiply the returned quaternion against the vector in object space to rotate it into world space, for example.
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
Thanks sparkprime for all your help! The thing is I now have errors! I created two entities using btConvexHullShapes and they work perfectly and collide fine but then I made one of them as btBvhTriangleMeshShape and now when they collide I get:
'Unhandled exception at 0x008e4c83 in FutureRacer.exe: 0xC0000005: Access violation reading location 0x00000135.'
Its pointing at physicsWorld->stepSimulation(1/60.f,10);
any clues as to what could be happening?
Heres the code i use to create my static entity...
Just to say that I do make sure that I pass a mass of zero to static entities
'Unhandled exception at 0x008e4c83 in FutureRacer.exe: 0xC0000005: Access violation reading location 0x00000135.'
Its pointing at physicsWorld->stepSimulation(1/60.f,10);
any clues as to what could be happening?
Heres the code i use to create my static entity...
Code: Select all
StaticEntity::Create( D3DXVECTOR3 position, float mass, LPD3DXMESH mesh )
{
TriMeshData *data = new TriMeshData;
DWORD numVertices = mesh->GetNumVertices();
DWORD numFaces = mesh->GetNumFaces();
Vertex *v = 0;
mesh->LockVertexBuffer(0, (void**)&v);
// Extract vertices
data->vertices = new btScalar[numVertices * 3];
for(DWORD i = 0; i < numVertices; i++)
{
data->vertices[i*3+0] = v[i].position.x;
data->vertices[i*3+1] = v[i].position.y;
data->vertices[i*3+2] = v[i].position.z;
}
mesh->UnlockVertexBuffer();
// Extract indices
data->indices = new int[numFaces * 3];
WORD* ind = 0;
mesh->LockIndexBuffer(0,(void**)&ind);
//memcpy( &indices, &ind, sizeof(ind));
for(DWORD i = 0; i < numFaces; i++)
{
data->indices[i*3+0] = ind[i*3+0];
data->indices[i*3+1] = ind[i*3+1];
data->indices[i*3+2] = ind[i*3+2];
}
mesh->UnlockIndexBuffer();
int indexStride = 3 * sizeof(int);
int vertStride = sizeof(btVector3);
data->indexVertexArrays = new btTriangleIndexVertexArray(numFaces, data->indices, indexStride,
numVertices, (btScalar*) &data->vertices[0], sizeof(btScalar) * 3);
bool useQuantizedAabbCompression = true;
m_collisionShape = new btBvhTriangleMeshShape(data->indexVertexArrays, true);
btTransform trans;
trans.setIdentity();
trans.setOrigin(ConvertVector(position));
btVector3 inertia(0,0,0);
btDefaultMotionState* motionState = new btDefaultMotionState( trans );
btRigidBody::btRigidBodyConstructionInfo cInfo(mass,motionState,m_collisionShape,inertia);
m_rigidBody = new btRigidBody(cInfo);
if(data)
{
delete data;
data = NULL;
}
}
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: Newbie question - Advice on collision shapes
sparkprime wrote:Use btBvhTriangleMeshShape for the track. Don't scale the track at run-time, that's dumb. Scale it in your modeller. Don't bother with low detail meshes in the physics world, it will just complicate things.
For the cars, the best thing to do is to chop them up manually in to convex hulls and use a btCompoundShape to hold the hulls. You can also put other primitives (spheres, cones, boxes, cylinders, etc) in the compound shape if they are a reasonable approximation of parts of the vehicle. Another approach is to use a btGimpactShape which is an arbitrary triangle mesh. This is actually very similar to just putting lots of btTriangleShapes into a btCompoundShape, but is probably faster. You will have to use a special physics mesh for the car though, for the following reasons:
btBvhTriangleMeshShape is good for static world geometry indeed. It does support local scaling.
For concave moving objects it is best to use btCompoundShape with convex parts indeed. Try to avoid btGimpactShape whenever you can, the btCompoundshape should perform better and more reliable.
Thanks,
Erwin
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
That's good advice Erwin thanks for that. I'm still having problems with my last problem though if anyone could shed some light on that. To further explain the scene I should have described this better before. So for testing purposes I have two simple directx meshes both only about 1000 polys. One is made static with the btbvhTriangleMeshShape and 0 mass and the other made with convexhull and a weight of 1 and is placed above the static object. There are no errors on build or when running until the dynamic object, under gravity, collides with the static object below it. At which point it bugs out and throws me the error I described above while pointing to the stepSimulation. It must be something to do with how I create the static object... any ideas?
-
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: Newbie question - Advice on collision shapes
I can't remember but I think the triangle mesh might not actually have its own copy of the triangle data, so try not freeing the memory you allocated.
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: Newbie question - Advice on collision shapes
You the debug drawer to see if the physics mesh matches your graphics mesh.smuffer wrote:It must be something to do with how I create the static object... any ideas?
Also, using the btStridingMeshInterface (sharing the graphics and physics triangle mesh) can be error-prone, try using btTrangleMesh first.
Cheers,
Erwin
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
Sparkprime you beautiful man! Spot on! If I dont free the triMesh data then it collides! BUT... when it does collide now my frame rate drops from 60 to 10 but when the falling object is clear of the static object it shoots straight back up to 60fps again?? Whats happening?
Also how do I go about using the debug drawer? If its really simple dont bother im just being lazy because I dont have the time to look it up now but will check it out tomorrow.
Thanks for all the help so far guys.
Also how do I go about using the debug drawer? If its really simple dont bother im just being lazy because I dont have the time to look it up now but will check it out tomorrow.
Thanks for all the help so far guys.

-
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: Newbie question - Advice on collision shapes
The debug drawer would have come in useful a lot of times but I never bothered looking into it. One day...
To increase performance for the same set of simulated objects, you have to decrease the simulation accuracy by doing one or more of:
1) Reduce collision shape complexity (fewer vertexes, fewer triangles)
2) Reduce frequency of simulation by increasing the step size (time interval) that defines the amount of projected motion per tick
3) Reduce number of solver steps (there are many iterations of the solver for each tick)
You will have to experiment with all of these to find out how far you can push your simulation in each case.
To increase performance for the same set of simulated objects, you have to decrease the simulation accuracy by doing one or more of:
1) Reduce collision shape complexity (fewer vertexes, fewer triangles)
2) Reduce frequency of simulation by increasing the step size (time interval) that defines the amount of projected motion per tick
3) Reduce number of solver steps (there are many iterations of the solver for each tick)
You will have to experiment with all of these to find out how far you can push your simulation in each case.
-
- Posts: 13
- Joined: Sat Dec 09, 2006 8:10 am
Re: Newbie question - Advice on collision shapes
It's an interface you need to hook up to with your rendering system, so it depends on what system you are using. If you are using raw OpenGL, look at the bullet demos. If you're using Ogre, have a look at:smuffer wrote:Also how do I go about using the debug drawer? If its really simple dont bother im just being lazy because I dont have the time to look it up now but will check it out tomorrow.
http://www.ogre3d.org/wiki/index.php/BulletDebugDrawer
Otherwise, use these as a guide and make your own. In my experience, time spent in getting this to work pays you back really soon when you start working on the actual simulation, and trying to debug constraints without it is just silly...
-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
Im actually using directx and have never touched either of them, no worries though I will look into that later.tp wrote:
It's an interface you need to hook up to with your rendering system, so it depends on what system you are using. If you are using raw OpenGL, look at the bullet demos. If you're using Ogre, have a look at:
http://www.ogre3d.org/wiki/index.php/BulletDebugDrawer
Otherwise, use these as a guide and make your own. In my experience, time spent in getting this to work pays you back really soon when you start working on the actual simulation, and trying to debug constraints without it is just silly...
Right now im having a framerate problem and also a problem with the btBvhTriangleMeshShape I created for my track. Ok so I have done a little fiddeling and found that some things collide together better than others but if you look at this screenprint:

I've currently got the two ships with a btBoxShape and the track is about 13k polys and done with btBvHTriangleMeshShape. I figure to solve the frame rate I will just make a low poly version of the track and mess around with the attributes sparkprime mentioned but whats confusing me, and you can see by the picture is that the ships are colliding and stopping in the space in the center of my track(which in turn is where the frame rate plummits). This wont be a problem in the end since they will obviously be positioned on the track but surely that isn't right? Are unneccesary collision points being created?
-
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: Newbie question - Advice on collision shapes
That is weird. Is it 0,0,0 by any chance? Do you have a zillion degenerate triangles at 0,0,0 due to getting some sizes wrong?
Now would be a good time to get the debug drawer working
Now would be a good time to get the debug drawer working

-
- Posts: 16
- Joined: Fri Dec 04, 2009 7:14 pm
Re: Newbie question - Advice on collision shapes
Yeah its at (0,0,0) its really wierd the frame rate plummits aswell, then if i position the car on the track the frame rate is relativley ok but driving it is a nightmare there seems to be wierd collision points everywhere. Debug drawer is a must I think, I presume what it does is render the actualy collision shapes right? If anyone has any tips on how to use this with DirectX I would be very grateful.