[SOLVED] Length, width, and height of a rigid body.

Post Reply
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

[SOLVED] Length, width, and height of a rigid body.

Post by Crayder »

I know how to get the collision sphere radius and offset using getBoundingSphere, but is there a way to get the XYZ sizes of a rigid body model? That would include the XYZ lengths and center offsets.
Last edited by Crayder on Sun Aug 23, 2015 4:46 pm, edited 1 time in total.
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: Length, width, and height of a rigid body.

Post by anthrax11 »

If you mean the axis-aligned bounding box and the center of mass, then yes.
For the AABB, there is btCollisionShape::getAabb.
The center of mass is always (0,0,0) in local coordinates of any collision shape. In world coordinates, this is the world transform of the body (see btRigidBody::getCenterOfMassPosition). An exception to this is compound shapes, where child shapes have an offset.
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

Re: Length, width, and height of a rigid body.

Post by Crayder »

anthrax11 wrote:If you mean the axis-aligned bounding box and the center of mass, then yes.
For the AABB, there is btCollisionShape::getAabb.
The center of mass is always (0,0,0) in local coordinates of any collision shape. In world coordinates, this is the world transform of the body (see btRigidBody::getCenterOfMassPosition). An exception to this is compound shapes, where child shapes have an offset.
Not exactly what I'm looking for.

About the center of mass being (0,0,0), that is false. Not every rigid body's center is (0,0,0).

I'll have to get a picture of what I'm trying to say to explain about the lengths, I'll edit this in a sec.

EDIT:Image

Say that the (0,0,0) of that world was the center of the object by definition. The center offset would not be (0,0,0), it's be like (1.5,0.75,-1.0). The lengths would be the total length of each side. Basically the lengths would be the edges of the bounding box shown in that picture.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Length, width, and height of a rigid body.

Post by Basroil »

Crayder wrote: About the center of mass being (0,0,0), that is false. Not every rigid body's center is (0,0,0).
By definition the center of mass for any rigid body is at the local coordinates of (0,0,0). As anthrax11 stated, you can find the bounding box of the object from it's AABB, and that will give you the effective size. Simply load your mesh at the default orientation and calculate the AABB, and you'll have the dimensions you are requesting. If your mesh is not defined with the center of gravity at (0,0,0), you'll have to either redefine the mesh or recalculate the dimensions using an offset.
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

Re: Length, width, and height of a rigid body.

Post by Crayder »

Basroil wrote:
Crayder wrote: About the center of mass being (0,0,0), that is false. Not every rigid body's center is (0,0,0).
By definition the center of mass for any rigid body is at the local coordinates of (0,0,0). As anthrax11 stated, you can find the bounding box of the object from it's AABB, and that will give you the effective size. Simply load your mesh at the default orientation and calculate the AABB, and you'll have the dimensions you are requesting. If your mesh is not defined with the center of gravity at (0,0,0), you'll have to either redefine the mesh or recalculate the dimensions using an offset.
Ok I guess I understand now. Now though, I've run into a new problem.

getAabb is returning nothing but 0's...

Here are two functions I have. The sphere one works perfectly fine, but the box is returning 0's (as I said above, it's because of getAabb).

Code: Select all

int ObjectManager::getBoundingSphere(uint16_t modelid, btVector3& center, btScalar& radius)
{
	uint16_t colindex = ModelRef[modelid];


	if (colindex != 65535)
	{
		colObjects[colindex]->getCompoundShape()->getBoundingSphere(center, radius);
		return 1;
	}
	return 0;
}


int ObjectManager::getBoundingBox(uint16_t modelid, btVector3& min, btVector3& max)
{
	uint16_t colindex = ModelRef[modelid];
	btTransform t;


	if (colindex != 65535)
	{
		colObjects[colindex]->getCompoundShape()->getAabb(t, min, max);
		return 1;
	}
	return 0;
}
Off topic: colObjects is the array of shapes. ModelRef is the reference indexes to colObjects for model id's.
anthrax11
Posts: 72
Joined: Wed Feb 24, 2010 9:49 pm

Re: Length, width, and height of a rigid body.

Post by anthrax11 »

The transform should be initialized to the identity transform, meaning no rotation (or translation).

Code: Select all

btTransform t;
t.setIdentity();
Just to clarify, the fact that (0,0,0) is the center of mass (COM) is an assumption that bullet makes. The actual COM could be different, but then you'd need to either adjust the model so that the COM matches the model origin or use a compound shape. There is no function in bullet to calculate the exact COM of an arbitrary user-defined shape, so you have to find it yourself. The center of the AABB is usually a good estimate.
See also: http://www.bulletphysics.org/Bullet/php ... php?t=2209
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Length, width, and height of a rigid body.

Post by drleviathan »

And if you want to compute the center of mass of an arbitrary mesh shape yourself here is some code for doing that (from this thread).
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

Re: Length, width, and height of a rigid body.

Post by Crayder »

Thanks guys, adding setIdentity solved the problem.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Length, width, and height of a rigid body.

Post by Basroil »

anthrax11 wrote: Just to clarify, the fact that (0,0,0) is the center of mass (COM) is an assumption that bullet makes.
Pretty sure most realtime physics engines do that, it's much cleaner mathematically as all linear velocity changes are simple additions and almost the same with angular velocity (rather than having to do a full matrix multiplication for each and every change). :wink:

Defining objects with a geometric mean different than it's center of mass is a drag, but at least it's easier to keep track of both if you define one as (0,0,0)
Post Reply