Center of mass for rigid bodies

Sheikh Dawood
Posts: 7
Joined: Tue Sep 19, 2006 3:14 am

Center of mass for rigid bodies

Post by Sheikh Dawood »

This has been bothering me for some time.
If I create an object around the origin (0,0,0) i.e. where the vertices of the object are around the origin and the center of mass is (0,0,0), then I transform is somewhere else using the "startTransform", the physics works fine.

However, when I add vertices a little further away from the origin let say
(0,0,0)
(0,0,1)
(0,1,0)
Here it seems that (0,0,0) is the center of mass.

So my question is, before translating, must I always create the vertices around a center of mass (0,0,0) then translate it to where I want.

Or, do I have to calculate the center of mass of each rigid body myself?

This is because I need to create many triangles where the vertices are based on a relative point and each triangles are part of a different rigid body. Hence they have different center of masses.

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

Re: Center of mass for rigid bodies

Post by Erwin Coumans »

The data for a collision shape is defined around its local origin: a box is defined by half extends, and the center is (0,0,0). Same for sphere, cylinder, cone and convex hull meshes. When you assign a collision shape to a rigidbody, it will be placed around the center of mass of that rigidbody. In other words, the vertices/triangles are all defined in local space of the collision shape.

However, if you really want, you can shift the collision shape, by adding it to a btCompoundObject. Each child shape has an additional offset relative to the btRigidBodies center of mass.

Hope this helps,
Erwin

Sheikh Dawood wrote:So my question is, before translating, must I always create the vertices around a center of mass (0,0,0) then translate it to where I want.

Or, do I have to calculate the center of mass of each rigid body myself?

This is because I need to create many triangles where the vertices are based on a relative point and each triangles are part of a different rigid body. Hence they have different center of masses.

Thanks
Sheikh Dawood
Posts: 7
Joined: Tue Sep 19, 2006 3:14 am

Post by Sheikh Dawood »

Yup, every shape created will have a default center of mass at the origin (0,0,0).

Therefore, to create a shape with vertices away from the origin, I have to translate it first to the origin then translate it back after creating the shape.

I can't use compound shape because I need each shape to be separated moving objects.

BTW, it will be nice if we can initialize each shape to have a custom origin instead of having the default (0,0,0).

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

Post by Erwin Coumans »

You should check out Bullet/Demos/ConvexDecompositionDemo more in detail.

It breaks an existing objects (loaded from a .obj file) into multiple convex pieces. Then the centroid of each piece is calculated, and the vertices are shifted around this local centroid. Right now, the demo just breaks the objects from the start and each piece is a separate rigidbody.

It would be easy to just add each piece to one rigidbody at the start, using an additional compoundshape, using the inverse centroid as 'center of mass' shift (child transform).

If I have time I will demonstrate such breaking objects, that first is a compound of pieces, and then breaks apart on a keypress.

Hope this helps,
Erwin

By the way, you can have 1 rigidbody, 1 compoundshape pointing to one other shape with a childtransform. You are not forced to add all shapes to one compound/rigidbody. However, as you can see above, its still best to defined each convex shape in its own local origin, so that you can easily simulate them as separate bodies. But when you combine them, you just add them to the same compound, using the centroid as center-of-mass shift.