• 10
• 9
• 12
• 14
• 13

# Creating Box RigidBody From AABB

This topic is 741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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