# Creating Box RigidBody From AABB

## Recommended Posts

Hi Guys,

I have one entity which contains many static meshes, I'm trying to create a box RigidBody for each mesh using Bullet Physics (btBoxShape)

Here is how I calculate the AABB for each mesh:

D3DXVECTOR3 boxMin(FLT_MAX, FLT_MAX, FLT_MAX);
D3DXVECTOR3 boxMax(-FLT_MAX, -FLT_MAX, -FLT_MAX);

for(UINT i = 0; i < NumOfVertices; i++)
{
D3DXVECTOR3 vertexPosition = vertices[i].position;
// Transform to the entity space
D3DXVec3TransformCoord(&vertexPosition, &vertexPosition, &Entity->WorldMatrix);

D3DXVec3Minimize(&boxMin, &boxMin, &vertexPosition);
D3DXVec3Maximize(&boxMax, &boxMax, &vertexPosition);
}

Then I create btBoxShape by using:

D3DXVECTOR3 boxHalfExtent = (boxMax - boxMin) * 0.5f;
btBoxShape* BoxShape = new btBoxShape(btVector3(boxHalfExtent.x, boxHalfExtent.y, boxHalfExtent.z));
// ...

For the box transform that I use to create the box, I multiply the mesh world matrix with the entity world matrix:

btTransform transform = ConvertWorldMatrixTobtTransform(MeshWorldMatrix * Entity->WorldMatrix);

Now, the problem is that when I debug draw the physics, I see the box is not adjusted correctly over the mesh, half of the box over the actual mesh and the other half is under the mesh (see the attached screenshot).

How do I get the box to appear correctly over the mesh?

[attachment=31508:Box0.png]

Edited by Medo3337

##### Share on other sites
Run the debugger?

##### Share on other sites

@Khatharr: What debugger? I'm using debug draw.

The world matrix that I use to draw the mesh is the same world matrix I use to create btTransform for the physics.

Edited by Medo3337

##### Share on other sites
Okay, we're not in For Beginners (I scrolled up) so it's time to learn how debugging works. This is more or less a perfectly ideal case.

Are you using Visual Studio, or something "fun"?

##### Share on other sites

@Khatharr: No, I'm not actually a beginner.

I was asking if you mean VS Debugging, PIX, Bullet Debug Draw, etc...

Yes, I'm using Visual Studio

I actually mentioned the code that I'm using above.

##### Share on other sites

Okay, so you know about debuggers already. Fantastic. Let me just make sure that we're on the same page here.

You've already got bullet debug view on - so clearly we're not talking about that. Let's cross that off the list.

You didn't post shader code because it sure doesn't seem like a shader issue, so digging up pix from the depths of history probably isn't what we're after. Let's go ahead and cross that off the list too.

You did post application code, because that's where the issue probably is.

The code you posted is relatively short and not very complex.

Your issue is an IPO (input-process-output) problem where the process is the prime suspect.

So of our remaining option, what tool can generally identify and isolate that kind of problem in about 30 seconds?

##### Share on other sites

@Khatharr: I found that the problem gets fixed if I set the alignment to "Center to Object" in 3Ds Max (Hierarchy Tab -> Pivot -> Affect Pivot Only -> Center to Object).

1. Do you have any idea how do I adjust the box (btBoxShape) correctly over the mesh without having to center the pivot in 3Ds Max?

2. Generally, do I need to recalculate the AABB each frame?

Edited by Medo3337

##### Share on other sites

Bullet box shape expects a center of mass (center of object) at local zero point to box, so you can imagine that instead of your box going from from 0,0,0->1,1,1 it goes from -0.5,-0.5,-0.5 -> 0.5,0.5,0.5 . I think you'll find using your box much easier in general if you think of it centered that way.

You do not need to recalculate the AABB each frame (bullet will do it for you based on the rotation of the object). if you're using the btMotionState to link your model and the rigidbody you should be able to just update your models transform based on that.

##### Share on other sites

@xexuxjy: I want to be able to handle both situations, If the mesh is centered then I will just create btBoxShape the way I do now

But if it's not centered (for example a door mesh with pivot not centered) how do I create btBoxShape out of it?

Perhaps, I need to read the pivot matrix from the DAE file and multiply it with the mesh world matrix?

Edited by Medo3337

##### Share on other sites

In that case you still need your door mesh to be a centered box, but then have a hinge constaint on it to anchor it to another mesh / rigid body and let the contraint do the work for you. The bullet constraint demo has good examples of this.

##### Share on other sites

@xexuxjy: I was just trying to give an example, I want to be able to create btBoxShape no matter if the pivot is centered or not.

Is there is any way to extract the mesh world matrix with respect to the pivot?

##### Share on other sites

Sure. Was trying to remember if bullet allowed you to change the centre of mass  easily and it doesn't though there are ways to work around it using compound shapes but wouldn't recommend it. One solution Erwin mentions below in :

http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=2209#p10314

is to let your motion state do the graphics offset for you. I'd still probably recommend trying to keep the pivot centred though.

##### Share on other sites

You really need your coordinate systems to share the same origin if they're supposed to correspond. The typical solution to this is to employ an offset, which can be metadata with your model (just marking the position of the barycenter/CoM in model space) or you can calculate it at load time if it's just barycenter. It would be easier in the short-run to center the pivot, but if that's restrictive for you then building that offset system is a one-shot solution that will remove the restriction and let you do your thing (not counting bugs, etc).

The basic idea is to transform the CoM along with the object and then use the resulting position as the offset for aligning the spaces.

For example, in the picture you showed we can say that the bottom-center of the model is at 0,0,0 and the barycenter is at 0,1,0. So just add 0,1,0 to to the object position for bullet and the AABB should align correctly.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982384

• 10
• 9
• 15
• 24
• 11