Tie Models to Physics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Lyrical
Posts: 7
Joined: Fri Mar 19, 2010 4:40 pm

Tie Models to Physics

Post by Lyrical »

I've looked through the forum and got a little confused on how to make the model and the physics connect to each other.
I understand the model and the physics model are two seperate entities and the worldmatrix will need to be got from the
physics model and passed to the display model.

I've done the following

cubeModel is a plane Box in .X format

Code: Select all

Box pBox = new Box(new Vector3(0.25f, 1.5f, 1.5f), 1, 1, 1, 1f);
DisplayModel cubeDM = new DisplayModel(cubeModel);
entityDrawer.displayModels.Add(cubeDM);
space.add(pBox);

This shows the DisplayModel which is my .x file in the center of the map and the pBox for the physics as the physics entity which moves about.

If i update the DisplayModel's worldMatrix in the Update of my game the box does move with the physics box but it is maked by the rendered physics box.

Hope this made sense.
I'm presuming i'm missing a step to tie the imported .x model to the physics box model.

I will keep looking through the forum as i'm sure this will of been asked before, i just can't find it right now.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Tie Models to Physics

Post by Norbo »

(This is going to be a little bit of a broad explanation for other people who might be reading this later, I just wanted to get it all in one spot)

In the beginning, it's good to remember that the physics has no concept of rendering. It just spits out data which some other systems will presumably make use of. Additionally, at the low level, rendering has no concept of physics.

Once the physics is updating each frame, the data it spits out (positions, orientations) will change. A graphical model is just a bunch of vertices floating out in space that doesn't care about the physics, and so you need to tell it specifically where to go to match up with the physics.

In XNA, the BasicEffect class (which is available through the Model when you're doing the rendering) has a World transform that can be set. There's nothing super special about the BasicEffect or Model class; XNA just provides them to help with setting up some basic scenes without having to write your own version of a Model class or shader.

When you set the World transform, it will render the model at that position. The simple DisplayModel class contained in the BEPUphysicsDemos updates the BasicEffect World transform each frame according to an entity's state (such as entity.worldTransform).

After you are updating the graphical world transform each frame according to the physics, you may notice that the graphics still don't quite match the physics. This is particularly noticeable if you make a graphical model that's offset from the origin; if you draw that model according to just an entity's world transform, the position of the model will be offset from the entity by the same amount it was offset from the origin.

To address this, you can move it around in the model editor, offset it in the content pipeline, or take the offset into account when setting the Effect's world transform. If the model is 5 units above the origin, you could use a matrix that translates it down 5 units before multiplying by the entity's world transform. For example:

Code: Select all

worldTransformToUse = Matrix.CreateTranslation(new Vector3(0,-5,0)) * entity.worldTransform;
Lyrical
Posts: 7
Joined: Fri Mar 19, 2010 4:40 pm

Re: Tie Models to Physics

Post by Lyrical »

ok thanks for the broad overview,

The bottom line is that i need to update the world transform in the update function to render the .x model in the correct place.
or should i say render the .x model in the same location and orientation as the physics representative model.

Thats they standard way i expect to do this.

cheers
Post Reply