Sign in to follow this  
acid2

Struggling with rotation.

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,
		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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this