Jump to content
  • Advertisement
Sign in to follow this  
acid2

Struggling with rotation.

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
Well.. I'm passing degrees and converting them to radians if thats what you mean?

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
yeah, your function is RadiansToDegrees() instead :)

So basically, the angle step is HUGE!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Weird, recompile and it all works fine o.O I wonder what I changed! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!