Struggling with rotation.

Started by
5 comments, last by GameDev.net 18 years, 9 months ago
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,
		Maths.Functions.DegreeToRadians(rotation.X));
	QuaternionF yAxis = Maths.Quaternions.RotateAxis(Vector3F.YAxis,
		Maths.Functions.DegreeToRadians(rotation.Y));
	QuaternionF zAxis = Maths.Quaternions.RotateAxis(Vector3F.ZAxis,
		Maths.Functions.DegreeToRadians(rotation.Z));
		
	orientation = yAxis;
	this.rotation = rotation;
	BuildMatrix();
}

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

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


[Edited by - acid2 on July 8, 2005 5:28:16 AM]
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
Advertisement
Just a guess - maybe you've simply mistook angle measures (radians and degrees) ?

Cheers!
___Quote:Know where basis goes, know where rest goes.
Well.. I'm passing degrees and converting them to radians if thats what you mean?
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
Quote:Original post by acid2
Well.. I'm passing degrees and converting them to radians if thats what you mean?


Not really, this is the correct way:

radians = degrees * (PI/180)
-----------------Always look on the bright side of Life!
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.
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
yeah, your function is RadiansToDegrees() instead :)

So basically, the angle step is HUGE!

Everything is better with Metal.

Weird, recompile and it all works fine o.O I wonder what I changed! :)

This topic is closed to new replies.

Advertisement