My question is; I am developing a game for XNA and I am trying to create a weapon attachment for my player model. My player model loads the .md3 format and reads tags for attachment points. I am able to get the tag of my model's hand. And I am also able to get the tag of my weapon's handle. Each tag I am able to get the rotation and position of and this is how I am calculating it:

Model.worldMatrix = Matrix.CreateScale(Model.scale) * 
                    Matrix.CreateRotationX(-MathHelper.PiOver2) *

Pretty simple, the player model has a scale and its orientation(it loads on its side so I just use a 90 degree X axis rotation, and a Y axis rotation to face away from the camera). I then calculate the torso tag on the lower body, which gives me a local coordinate at the waist. Then I take that matrix and calculate the tag_weapon in the upper body. This gives me the hand position in local space. I also get the rotation matrix from that tag that I store for later use. All this seems to work fine.

Now I move onto my weapon:

Matrix weaponWorld = Matrix.CreateScale(CurrentWeapon.scale) *
                            Matrix.CreateRotationX(-MathHelper.PiOver2) *
                            TagRotationMatrix *
                            Matrix.CreateTranslation(HandTag.Position) *
                            Matrix.CreateRotationY(PlayerRotation) *
                            Matrix.CreateTranslation(CollisionBody.Position) *

You may notice the weapon matrix gets rotated by 90 degress on the X axis as well. This is because they load in on their sides. Once again this seems pretty simple and follows the SRT order I keep reading about.

My TagRotation matrix is the hand's rotation. HandTag.Position is its position in local space. CreateRotationY(PlayerRotation) is the player's rotation in world space, and the CollisionBody.Position is the player's world location. Everything seems to be in order, and almost works in game. However when the gun spawns and follows the player's hand it seems to be flipped on an axis every couple frames. Almost like the X or Y axis is being inversed then put right back. Its hard to explain and I am totally stumped. Even removing all my X axis fixes does nothing to solve the problem. Hopefully I explained everything enough as I am a bit new to this! Thanks!

  • 2
    \$\begingroup\$ Have a video example? Could be an issue with gimbal lock \$\endgroup\$
    – House
    Apr 17 '12 at 0:44
  • \$\begingroup\$ I don't have a video, but I could explain it like this (Barrel-Stock-Body-Buttstock) to (Buttstock-Body-Stock-Barrel). If that makes sense. I can try to take a video if need be. I did also try to decompose the TagRotationMatrix and then use it in my Weapon's matrix like this: Matrix.CreateFromQuaternion(rotate). It didn't seem to help but I am not sure if that even avoids Gimbal lock? \$\endgroup\$
    – Tron86
    Apr 17 '12 at 0:48

It might be a trigonometric discontinuity where the rotation matrixes Matrix.CreateRotationY(PlayerRotation) or TagRotationMatrix flip from one quadrant of a circle to another. That might result in a value that instantly changes from +pi/2 to -pi/2, (or the like) creating the symptoms you describe. You might try putting all your matrices and vectors into debug output, to try to isolate which link in the chain of multiplication is flipping.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.