Page 1 of 1

Collision box not aligning up correctly

Posted: Fri Jan 27, 2012 7:36 pm
by Dukajuke
All,

In my project I have simple cubed shaped models. For some reason the BEPU collision box is about 50% offest from the model position on one of the axis. I thought I could fix this by creating a simple offset for my scene entities in my update method, but can't seem to get it to work correctly. I did see some posts related to this but I have not been able to apply any of the solutions to my project. Here is the current method for updating my scene entities. If anyone has any suggestings I would greatly apprecate it!

Vector3 scale = Vector3.Zero;
Quaternion rotate = Quaternion.Identity;
Vector3 position = Vector3.Zero;
sceneObject.World.Decompose(out scale, out rotate, out position);
Vector3 Offset = new Vector3(0, 25, 0);
Matrix worldMatrix = Matrix.Identity;
Vector3 normalizedUp = Vector3.Normalize(box.CollisionInformation.WorldTransform.Matrix.Up);
worldMatrix = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(obj.BufferedStates.InterpolatedStates.Orientation) *
Matrix.CreateTranslation((obj.BufferedStates.InterpolatedStates.Position - (normalizedUp * box.HalfHeight)) + Offset);
sceneObject.SetWorldAndWorldToObject(worldMatrix, Matrix.Invert(worldMatrix));

Re: Collision box not aligning up correctly

Posted: Fri Jan 27, 2012 7:49 pm
by Norbo
If you haven't already seen it: http://bepuphysics.codeplex.com/wikipag ... ecentering

The key is that the graphical offset needs to be applied in local space. The order of the transformation applications matters. In your posted sample code, it's using a mixture of transforms together. The problem is conceptually much simpler.

Instead of:
World = orientation * WorldAndLocalTranslationCombined;

it should be:
World = LocalOffsetTransform * Orientation * WorldPosition.

Note that the Orientation * WorldPosition part is equivalent to a rigid WorldTransform.

LocalOffsetTransform should be whatever is needed to bring the graphic into alignment with the collision shape in local space. For example, if the graphic is sitting on the origin instead of being centered on the origin, this offset used to generate the LocalOffset transform would be (0, -GraphicHeight / 2, 0).