XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Wierd Rotation Problem

Last post 4/19/2007 11:03 PM by digitalml. 6 replies.
  • 4/15/2007 8:32 PM

    Wierd Rotation Problem

    I have an object that I am trying to rotate with the xbox game pad. The rotation works as expected except for when the object is rotated upside down (on its x axis). When the object is rotated upside down the rotation along the y axis is reversed, but if the object is rotated back to its upright position the rotation is fine. I don't believe this is a gimble lock problem. I have also tried many other rotation functions such as Quaternions, but so far this simple implementation has given me exactly what I'm looking for except for the reversed rotation  How can I fix this?

    Here is some code that I am using...

    _yaw += gamepad.ThumbSticks.Left.X * 0.07f;
    _pitch -= gamepad.ThumbSticks.Left.Y * 0.07f;
    _roll = 0.0f;

    effect.World = transforms[mesh.ParentBone.Index] *
         Matrix.CreateRotationY(_yaw) *
         Matrix.CreateRotationX(_pitch) *
         Matrix.CreateRotationZ(_roll) *
         pos;


  • 4/16/2007 11:59 AM In reply to

    Re: Wierd Rotation Problem

    Answer
    Reply Quote

    If you think about it, this behavior is really what you would expect. When you combine several axis rotations one after another, the first rotation will move the position of the second axis. If that first rotation turns the second axis entirely upside down, that will flip which way you are rotating.

    If you don't want that behavior, you could just detect when the object is upside down and reverse your input controls to match.

    If it was me, though, I'd avoid using roll/pitch/yaw rotation format entirely: this whole approach just has too many problems and never seems to end up doing what I want! I've always had more luck storing orientations as pairs of front and up vectors, or using quaternions, instead...

    XNA Framework Developer - blog - homepage
  • 4/16/2007 6:04 PM In reply to

    Re: Wierd Rotation Problem

    Agreed.  If you want to composite rotations against multiple absolute (world-space) axes, you'll need to do some extra work.  This sort of thing comes up a lot when implementing, say, an orbit-style camera that controls intuitively.  For example, to rotate about world-Y and then about world-X, you may need to do something like this (sorta pseudo-code, insofar as I haven't compiled it).

    Quaternion xform = Quaternion.CreateFromAxisAngle(Vector3.UnitY, yAngle);

    Vector3 worldXInLocalSpace = Vector3.Transform(Vector3.UnitX, xform);

    xform = xform * Quaternion.CreateFromAxisAngle(worldXInLocalSpace, xAngle);


  • 4/17/2007 3:15 AM In reply to

    Re: Wierd Rotation Problem

    Well I have no problem using any other type of method except I am not so sure how.. I have been trying all night to implement this rotation and can't seem to get it right. I've even tried implementing the above code with some minor corrections to no avail. The functionality that I am looking for is that the object rotate around the world axis and not the local axis of the object. This is where I run into some problems. For instance, say i have a box and i press down on the game controller, i always want that box to rotate toward the camera downwards on the x axis even if th objects x axis is rotated in a different direction. The effect I am seeing right now is that if i rotate the object up or down along the x axis and then rotate left or right along the y axis the rotation is not as expected since the model is rotating on is local cords. Could someone please point me in the right direction on how to get this rotation working, a code example would be really beneficial.

    Thank you! :)
  • 4/18/2007 8:47 AM In reply to

    Re: Wierd Rotation Problem

    Hi There,

    I think that if you look closer there is more weirdness than you think, in that when you point your object up, it rolls instead of turning.

    You need to rotate on the Z axis as well as the X and Y, because when facing up, you need to turn on the z axis to give the feeling of turning left or right on your own axis.

    You can use sine and cosine to decide how much each rotational axis (Y and Z) needs to be rotated, and (believe it or not) this will fix your rotation totally(ie: no more craziness in the land of the upside-down).

    Howeber it will screw up your current sine cosine code for apllying drectional motion to your ship, which I imagine looks something like this:

    public Vector3 cosine3D(Vector3 thisrot){ //recognize this? (conert rotaton X & Y to a position vector)

    Vector3 newVec = Vector3.Zero;

    newVec.X = -(float)Math.Sin(thisrot.Y);

    newVec.Z = -(float)Math.Cos(thisrot.Y);

    newVec.Y = (float)Math.Sin(thisrot.X);

    newVec.X = (float)Math.Cos(thisrot.X) * newVec.X;

    newVec.Z = (float)Math.Cos(thisrot.X) * newVec.Z;

    return (newVec);

    }

    What we probably both need to be able to do (and I can't I'm afraid, I'm useleess at trig) is get the rot.Z into the equation.

    If you can re-figure out this equation with the new added rotational axis, I'll gladly write and post the code for applying the rotation properly (I know it works with the rotational bit, but deleted the code on discovering how it messed my translational information up). It also means ignoring Quaternions for a bit, which I would appreciate, as they seem hellishly long winded and intense.

    Cheers,

    James

  • 4/18/2007 1:01 PM In reply to

    Re: Wierd Rotation Problem

    Thanks for the post James, I understand what you are saying but I’m no trig guru either. I am actually not throttling my object anywhere as it will always be in a static location, so I am not too sure what you posted effects the rotation I’m looking for. :) . I was hoping to get an example for the rotational code based on what you were talking about, perhaps if you or anyone could post a code sample for the rotation it would be much appreciated. I've downloaded 20+ examples and everything relates to a first person or 3rd person camera and doesn’t give me the type of rotation I’m looking for.

    Thanks
    Matthew

  • 4/19/2007 11:03 PM In reply to

    Re: Wierd Rotation Problem

    Ok here is a better example of my problem I hope someone understands it. Basically i want to always rotate on the world x axis and not the objects x axis no matter the angle of rotation on the y axis. can someone please help with the code to achive this? 

    Fig1
    Fig2

    Thanks!
Page 1 of 1 (7 items) Previous Next