# Sturggling with rotation

This topic is 5059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I've just found out that my rotation code is sooo wrong and I'm having some trouble understanding why. Here is a video illistrating the worst case of the rotation: Clicky (DivX required). The video the is me jsut rotating +1 on pitch. The camera has been rotated about 240° on the yaw axis aswell. I'm using quatenions to represent the rotation in this way:
public void Turn(float pitch, float yaw, float roll)
{
Quaternion X = Quaternion.RotationAxis(new Vector3(1, 0, 0), Geometry.DegreeToRadian(pitch));
Quaternion Y = Quaternion.RotationAxis(new Vector3(0, 1, 0), Geometry.DegreeToRadian(yaw));
Quaternion Z = Quaternion.RotationAxis(new Vector3(0, 0, 1), Geometry.DegreeToRadian(roll));

orientationQuat *= (Y * X * Z);
OnRotate(this, null);
}

public void Rotate(float pitch, float yaw, float roll)
{
Quaternion X = Quaternion.RotationAxis(new Vector3(1, 0, 0), Geometry.DegreeToRadian(pitch));
Quaternion Y = Quaternion.RotationAxis(new Vector3(0, 1, 0), Geometry.DegreeToRadian(yaw));
Quaternion Z = Quaternion.RotationAxis(new Vector3(0, 0, 1), Geometry.DegreeToRadian(roll));

orientationQuat = Y * X * Z;
OnRotate(this, null);
}

Here is how I'm calling the rotations:
if(Left.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Turn(0, -1, 0);

if(Right.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Turn(0, 1, 0);

if(Up.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Move(0, 0, 1f);

if(Down.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Move(0, 0, -1f);

if(CamDown.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Turn(1, 0, 0);

if(CamUp.CurrentState != KeyState.Idle)
camMain.SceneGraphNode.Turn(-1, 0, 0);

camMain.SceneGraphNode.Rotate(camMain.SceneGraphNode.Rotation.X, camMain.SceneGraphNode.Rotation.Y, 0);

Any ideas?

##### Share on other sites
I can't view the video, so I don't know exactly what problem you're having. Here are a couple of observations about your code though. First of all, it doesn't look like you're gaining anything by using quaternions. You're essentially using Euler angles, so you're still going to have all the problems inherent to that representation.

It's often said that 'quaternions prevent gimbal lock', and this may be why you're using them. But as I understand it this is an oversimplification and can be misleading. I think what might be more accurate is that *incremental rotation* prevents gimbal lock. This can be accomplished with quaternions, matrices, and in a certain sense even with Euler angles.

There are some other things in your code I'm not sure about, but without knowing more about the context I can't really say anything specific about it. I will say that rotation in 3d is not easy, and can take a while to get a handle on. Here is some advice, totally IMHO, that you can take or leave. Almost everything (in fact, perhaps everything - I'd have to do some checking) that can be done with quaternions can be done with matrices. A better learning path might be to get a handle on the material using matrices first. Once you have that down, the reasons for choosing quaternions over matrices will be more clear, and your decision to use them will be more informed and probably have better results.

All IMO.

##### Share on other sites
Just for kicks, try this in Turn

orientationQuat = (Y * X * Z)*orientationQuat;

I've found, that I have to put my incremental rotation before my orientation quaternion. I'm not sure why, except that quaternion multiplies are not communitive.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634112
• Total Posts
3015580
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!