non-static CollisionShapes and RigidBodies with BvhTriMeshs?

pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

non-static CollisionShapes and RigidBodies with BvhTriMeshs?

Post by pierreofthefrench »

Hello, been playing around with Bullet for quite a bit now but have come across something I'm having trouble figuring out.

I've been able to make static geometry with btBvhTriangleMeshShape(shape,false,true); but now I would like to make non-static objects using the same principle but am having troubles getting it to work.

I try to follow the tutorials closely and this is what I've managed to get set up for adding a dynamic object of any mesh:

Code: Select all

btCollisionShape *WorldColShape = new btBvhTriangleMeshShape(groundShape,false,true);

    m_collisionShapes.push_back(WorldColShape);
    btTransform groundTransform;
    groundTransform.setIdentity();
    groundTransform.setOrigin(btVector3(0,0,0));

	btScalar mass(6.f);
	btVector3 localInertia(0,0,0);
	WorldColShape->calculateLocalInertia(mass,localInertia);

	btTransform startTransform;
	startTransform.setIdentity();
	startTransform.setOrigin(btVector3(offset.x,
										offset.y,
										offset.z));

	btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,WorldColShape,localInertia);
	btRigidBody* body = new btRigidBody(rbInfo);
					
	m_dynamicsWorld->addRigidBody(body);
This code is basically taken just by one of the examples where a non-static object is used. When I try and run this code however, the code crashes at caculateLocalInteria(). Now, I've tried several methods, when not using the triangle mesh but instead using like a box, it works fine. My goal was to have a system set up where an object, like a chair or something, will be modeled as a high poly mesh, and then have a low poly also created as like a physics mesh.

What I've been able to look up is that there is a problem when trying to use calculateLocalInteria with a potential concave mesh? (Setting inertia to 0 just makes the object fall through everything and ignore collisions). I'm curious, do I have to essentially constrain boxes (or some other convex shapes) together to form an object I want? Or is there something relatively obvious that I am missing that allows using the Triangle Mesh to be created and used as an actual physical object? It's 3 AM so I'm not at my sharpest :) Fixing several convex shapes together wouldn't seem too bad, I guess I'd take a in-depth peak at the fork-lift demo or something but if I'm going to do it I would like to know it's the right way. Thanks!
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by Dr.Shepherd »

Code: Select all

void    btTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
{
         (void)mass;
         //moving concave objects not supported
         btAssert(0);
         inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
}
Is this the reason for your problem ? It cannot support the concave objects?
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by Dr.Shepherd »

And I also found this in the documentation for btBvhTriangleMeshShape
The btBvhTriangleMeshShape is a static-triangle mesh shape
User avatar
jarno
Posts: 57
Joined: Tue Mar 16, 2010 1:42 am

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by jarno »

You can't use a btBvhTriangleMeshShape for dynamic objects. Only for static and kinematic objects. The general purpose mesh shape for dynamic objects is btGimpactTriangleMeshShape.

---JvdL---
pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by pierreofthefrench »

Shepherd, I figured that was the case as I mentioned in the original post I did try to do a little homework, should not have missed the comments in the documentation though. :P

Jarno, thanks, I'll have to try this out right now!

Edit: I was able to create a btGImpactMeshShape but it doesn't seem interested in colliding with my static geometry, the calculateLocalInertia does work now though. Going to keep playing with it for a few hours.

Edit2: Still trying to figure this one out, I notice in most examples a btTriangleIndexVertexArray is used but I'm using:

Code: Select all

btTriangleMesh * groundShape = new btTriangleMesh(true,false);
// POPULATE TRIANGLE LIST HERE...
btGImpactMeshShape *WorldColShape = new btGImpactMeshShape(groundShape);
WorldColShape->setLocalScaling(btVector3(1.f,1.f,1.f));
WorldColShape->setMargin(0.07f);
WorldColShape->updateBound(); 
Any reason this code should not be working?
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by dphil »

You have to register the gImpact collision algorithm. In your world setup code, call:

Code: Select all

btGImpactCollisionAlgorithm::registerAlgorithm(myCollisionDispatcher);
pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by pierreofthefrench »

I actually tried this at one point, didn't change anything :( are there any other things that need to be done? Thanks for the help though!
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by dphil »

Not sure.... I use btTriangleMesh too, by the way. Although I use the parameters (false, true) whereas you have (true, false). You could try playing with those if you haven't already.
pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by pierreofthefrench »

No luck with the arguments, do you also use groundShape->addTriangle(vec1,vec2,vec3, true);? Then I use like:

Code: Select all

btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,WorldColShape,localInertia);
	btRigidBody* body = new btRigidBody(rbInfo);

	m_dynamicsWorld->addRigidBody(body);


I can't tell why it's not working, a bit of a bummer.
Bonbon
Posts: 4
Joined: Fri Feb 25, 2011 5:40 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by Bonbon »

Hi pierreofthefrench, going back to your original post. I was just wondering how you were creating the static geometry. What are you passing in as the first parameter?
What i would ideally want is a way for bullet to convert a simple mesh from maya into a collision shape.

Thanks
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by dphil »

Hm...from a glance it looks like your setup code is ok. You're not doing anything weird like setting its mass to 0, or setting custom collision flags that could disrupt normal collision detection? (either for the gimpact shape or for your static geometry; the flags I mean, mass should be 0 for static)
pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by pierreofthefrench »

bonbon,
I use the btBvhTriangleMeshShape and not the btGImpactMeshShape, but all I do is get the triangles inside the code itself (I use Ogre3Ds meshs) so they had code snippets to do this, and add them to the Bullets object using addTriangle. So you will need to find a Maya API or something I would guess.

dphil,
Wait, is it okay to use btBvhTriangleMeshShape for static geometry and btGImpactMeshShape for dynamic? I was under the impression that this would be fine, could this be why the collisions between the dynamic and static geometry are not working?

Thanks.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by dphil »

pierreofthefrench wrote: Wait, is it okay to use btBvhTriangleMeshShape for static geometry and btGImpactMeshShape for dynamic? I was under the impression that this would be fine
Yes that is fine, and is what you should be doing.

Have you tried a simple test, where you instantiate one simple static object (say a box or sphere) at the origin, instantiate the gimpact shape so it is overlapping the static shape, and watch closely for any jitter or anything that would indicate a collision response? It's usually helpful to use 0 gravity too, so you know that any motion you see is only from the collision.

Keep in mind as well that meshes are concave shapes so, for example, if your gimpact shape is entirely inside the bvh shape, or vice versa, you won't/shouldn't get a collision response.
pierreofthefrench
Posts: 9
Joined: Sat Jun 18, 2011 11:07 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by pierreofthefrench »

I found out what was causing the error, I should have started with less complicated scenarios than the one I was doing, the problem was with creating the object with the offset vertices vs. offset after creation. Was a stupid mistake :oops:

I have just one question now that it's working, I noticed my fps drops dramatically when using this method, is it much less efficient than using a compound shape with several primitives? For example would it be better to use 5 boxes to represent a table (1 top 4 legs) or to create a shell of the same table?

I only ask because when I was playing around with modding with Valves Source engine, you made a separate physics model to use with your graphics model, I would assume this would be much less efficient but there was no creating compound primitives to represent the shape. Thanks again.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: non-static CollisionShapes and RigidBodies with BvhTriMe

Post by dphil »

pierreofthefrench wrote:I noticed my fps drops dramatically when using this method, is it much less efficient than using a compound shape with several primitives?
It can be; all depends on how complex the mesh actually is, and how simple an approximation by a compound of primitives would be.
pierreofthefrench wrote:For example would it be better to use 5 boxes to represent a table (1 top 4 legs) or to create a shell of the same table?
Yes. And for something like a table, it's a good approximation. Still, I'd be surprised if a gimpact shape from a table mesh would cause much/any noticeable difference in performance, unless the simulation has a lot more interacting objects. Anyway, it *is* usually recommended to use compounds of primitives in place of meshes, especially for dynamic objects.
pierreofthefrench wrote:I only ask because when I was playing around with modding with Valves Source engine, you made a separate physics model to use with your graphics model, I would assume this would be much less efficient but there was no creating compound primitives to represent the shape.
It is quite common to have separate graphics/physics models, and especially to improve performance.