# Creating a rotation offset based on camera position with XNA

I have an XNA project where characters have the ability to pick up and move objects around (in 3d). When an object is picked up by a character, I want ithe objects position to always be out in front of the character (as if the character was holding it in their arms). I'm having trouble creating the offest using the charactors forward vector. The main issue is that I don't know how to properly incorporate the characters rotation into the objects position. I'm hoping somebody can give me an example of how to accomplish this to help point me in the right direction. Thanks!

Currently I'm just matching the held object's world matrix with the characters as a work around:

``````grabbedSceneObject.World = Matrix.CreateScale(20F) * Matrix.CreateTranslation(viewPosition);
``````
-

It's important to know what sort of forward vector you're talking about. If you're using Vector3.Forward directly that's simply a unit vector looking down the Z axis. Let's suppose your 3D model is designed such that its also facing that direction by default.

In order to get the real forward direction of your character you'll need to tranform it using the character's rotation first. For instance if your character can only rotate around the Y axis (i.e. has a Yaw property), you can get the character's current forward vector like this:

``````Vector3 characterForward = Vector3.Transform(Vector3.Forward, Matrix.CreateRotationY(character.Yaw));
``````

Once you have that and you'd like to offset the object's position in front of your character, simply do something like:

``````grabbedSceneneObject.World = Matrix.CreateTranslation(character.Position + characterForward * horizontalDistance + Vector3.Up * verticalDistance);
``````

Where horizontalDistance and verticalDistance are floats controlling the offset in world units from the character's origin. You may also add the scaling operation as in your example before the translation.

And you might also want to change the object's rotation so that it remains fixed in relation to the character when being grabbed. For that try adding a rotation matrix before the translation with a magnitute of character.Yaw degrees or character.Yaw + 90 degrees or whatever fits your need.

-

A simplified version would be:

``````if(objectGrabbed)
{
grabbedSceneObject.World = grabbingCharacterWorld;
grabbedSceneObject.Translation += grabbedSceneObject.Forward * distanceInFontOfCharacter;//possibly offset the vertical height as well
}
``````
-
+1, this should work perfectly. –  Nic Foster Jan 18 '12 at 15:00