Proper convex hull creation

Please don't post Bullet support questions here, use the above forums instead.
Post Reply
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Proper convex hull creation

Post by ca$per »

Hi!
It seems i just can't get it right.
Here is a snippet:

Code: Select all

btConvexHullShape *shape = new btConvexHullShape(&vertex->x, vertexCount, 12); //doesn't work
//btBoxShape *shape = new btBoxShape(btVector3(5.0f, 5.0f, 5.0f)); //works as expected

btVector3 localInertia(0.0f, 0.0f, 0.0f);
shape->calculateLocalInertia(1.0f, localInertia);
btRigidBody *body = new btRigidBody(1.0f, new btDefaultMotionState(), shape, localInertia);

btTransform transform;
transform.setFromOpenGLMatrix(bufNode->GetNodeTransform().Get());
body->setWorldTransform(transform);

TheBulletMgr->GetWorld()->addRigidBody(body);

btStaticPlaneShape *groundShape = new btStaticPlaneShape(btVector3(0.0f, 0.0f, 1.0f), 1.0f);
btCollisionObject *groundBody = new btCollisionObject();
groundBody->setCollisionShape(groundShape);
TheBulletMgr->GetWorld()->addCollisionObject(groundBody);
And here is a video of what happens when using btConvexHullShape: http://www.youtube.com/watch?v=BqeroHasaY4
And this is what happens when using a btBoxShape: http://www.youtube.com/watch?v=G4Wg_lTQ-QY
The first one falls incorrectly. And by just replacing the shape to box it starts to work as it should. All rendering is done using debug drawing. I also noticed, that there is no red-green-blue cross in the convex hull version. The one that shows the center (of mass?). Box shape has it thought.
What did i forget to add?
User avatar
jarno
Posts: 57
Joined: Tue Mar 16, 2010 1:42 am

Re: Proper convex hull creation

Post by jarno »

It looks like the centre of mass is off. The convex hull box is acting like it is at the corner. What are your vertex coordinates like? Bullet will use the origin of the shape as the centre of mass location.

---JvdL---
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Proper convex hull creation

Post by ca$per »

Hmm... I actually don't know what the coordinates would be, because i'm reading them from file (a previously saved mesh).
And how to change the center of mass of a shape? A rigid body has a function named setCenterOfMassTransform() but i didn't help. Perhaps because it's not a shape's function.
I couldn't find any in shape class.

EDIT:
Checked the coordinates and indeed they are all positive. Meaning that center will be in the corner just like you said. But how can i change that? How can i move the origin of a shape to the center of the mesh regardless of it's coordinates? I mean is there any built-in functionality for this?
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Proper convex hull creation

Post by ca$per »

Anyone?
User avatar
jarno
Posts: 57
Joined: Tue Mar 16, 2010 1:42 am

Re: Proper convex hull creation

Post by jarno »

You'll need to define or calculate a location for the centre of mass, and then subtract that from the vertex positions that you feed the convex hull.

A quick and easy way is to use the centre of the bounding box of the object. Works good enough for many objects. If not, then you could have a look at this code (PDF) if your meshes are triangulated.

---JvdL---
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Proper convex hull creation

Post by ca$per »

Thanks. The PDF looks a little complicated to me, thus i've tried a bounding box trick and it seems that it works.
Thank you!
Post Reply