# Struggling with rotation.

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

## Recommended Posts

Im having some really bizzare problems with my rotation code. If I rotate at a constant one degree around yaw, the object seems to rotate 180 degrees, then go back the other way, and so on (like a metranome). If I rotate around pitch the object seems to 'wave.' I can probably provide some videos if needed. Here is the relevant code, I'm pretty certain I've got everything correct. Creating the rotation quaternion:
public static QuaternionF RotateAxis(Vector3F axis, float angle)
{
QuaternionF ret = QuaternionF.Identity;

float halfAngle = angle * 0.5f;
float sinA = MathFunctions.Sin(halfAngle);

ret.X = axis.X * sinA;
ret.Y = axis.Y * sinA;
ret.Z = axis.Z * sinA;
ret.W = MathFunctions.Cos(halfAngle);

return ret;
}


Turning it into a matrix:
public static Matrix4F RotationQuaternion(QuaternionF quat)
{
Matrix4F ret = new Matrix4F();

//
// Copied nicely from Axiom :)
//

float tX = 2.0f * quat.X;
float tY = 2.0f * quat.Y;
float tZ = 2.0f * quat.Z;

float tWX = quat.W * tX;
float tWY = quat.W * tY;
float tWZ = quat.W * tZ;

float tXX = quat.X * tX;
float tXY = quat.X * tY;
float tXZ = quat.X * tZ;

float tYY = quat.Y * tY;
float tYZ = quat.Y * tZ;

float tZZ = quat.Z * tZ;

ret.M11 = 1.0f - (tYY + tZZ);
ret.M12 = tXY - tWZ;
ret.M13 = tXZ + tWZ;

ret.M21 = tXY + tWZ;
ret.M22 = 1.0f - (tXX + tZZ);
ret.M23 = tYZ - tWX;

ret.M31 = tXZ - tWY;
ret.M32 = tYZ + tWX;
ret.M33 = 1.0f - (tXX + tYY);

ret.M44 = 1;

return ret;
}


And finally, setting the rotation:
public void Rotate(Vector3F rotation)
{
QuaternionF xAxis = Maths.Quaternions.RotateAxis(Vector3F.XAxis,
QuaternionF yAxis = Maths.Quaternions.RotateAxis(Vector3F.YAxis,
QuaternionF zAxis = Maths.Quaternions.RotateAxis(Vector3F.ZAxis,

orientation = yAxis;
this.rotation = rotation;
BuildMatrix();
}

void BuildMatrix()
{
world = Maths.Matrices.Scale(scale) *
Maths.Matrices.RotationQuaternion(orientation) *
Maths.Matrices.Translation(location);
}

{
return degrees * (float)(180.0f / Sharp3D.Math.Core.MathFunctions.PI);
}


[Edited by - acid2 on July 8, 2005 5:28:16 AM]

##### Share on other sites
Just a guess - maybe you've simply mistook angle measures (radians and degrees) ?

Cheers!

##### Share on other sites
Well.. I'm passing degrees and converting them to radians if thats what you mean?

##### Share on other sites
Quote:
 Original post by acid2Well.. I'm passing degrees and converting them to radians if thats what you mean?

Not really, this is the correct way:

##### Share on other sites
I had it like that before (I edited the post above because according to a googled article it was 180 / pi.

Either way, its still behaving very weirdly.

##### Share on other sites

So basically, the angle step is HUGE!

##### Share on other sites
Weird, recompile and it all works fine o.O I wonder what I changed! :)

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×