An easy way to set the geometric center position of objects?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
jbosch
Posts: 43
Joined: Sat May 29, 2010 4:42 pm
Contact:

An easy way to set the geometric center position of objects?

Post by jbosch »

Hi,

I'm trying to work with the physics engine. The main problem I'm experiencing is how to relate 3D models with physical entities. To make it clear, look at the attached picture. At left there is a Cylinder object (from Bepu engine), and at the right we have a 3d model of a coke (please use your imagination :) ). If i want to add a "physical" behavior to the coke, what I do is this:

1- Load the coke model, transform its world matrix to set into the desired position
2- Create an instance of a cylinder, and add it to a bepu "space" instance, transform its world matrix to set into the desired position
3- Add the cilinder to the bepuphysicsdrawer, so I can alternate the render of the physical entities with the 3d models to check the positions and transformations are being correctly used
4- at the game Update method, update the space instance

This is cool, and works, I don't know if it is the "correct way", but works (if there is a better way please tell me :) ). The problem is that there is always an "offset" between the 3d model and the physical entity. So I supose their geometric center is different. In my 3d model I can control the geometric center in 3dmax, setting the pivot before generating the FBX file. In Bepu, I supose it is needed to set the "centerPosition" property of the Cylinder instance. ¿Wich is the best way to do it? I supose a way could be to do a lot of tests and alternate the render of the physical entities with the 3d models to find the position. But this method is not so scientific...

¿Is there a better way to set the center position of an object?
Attachments
coke.png
coke.png (4.11 KiB) Viewed 4185 times
Jesús Bosch
XNA BLOG: http://geeks.ms/blogs/jbosch
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: An easy way to set the geometric center position of objects?

Post by Norbo »

Finding a good graphics offset always requires a little arbitrary work.

I'm hesitant to include a feature on physical entities for purely graphical reasons, so my usual recommendation is to do the equivalent externally. For example, if your bottle is Y units too high, then you can apply a transformation that translates the bottle mesh down first before applying the entity's worldTransform.

To actually determine what "Y" is, there's a couple of options. The manual guess and check approach works well for one or two types of objects, but gets overwhelming when you need to manually specify many different shapes.

The 'automatic' approach involves coding something that calculates the Y. If your models are created with Y=0 being the floor, you can do this fairly easily. Call your physics entity's getExtremePoint method with a direction of Vector3.Down. The margin parameter allows you to specify the margin to include in the calculation. For graphics positioning, it's recommended that you use the entity's collisionMargin.

The method will output a position at the bottom of the shape. It's not necessarily in the bottom-middle of the shape, so you'll have to only look at the Y component. Compare the internalCenterPosition.Y to the extremePoint.Y and you have your offset!
Post Reply