Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with angles

Started by Ging Mar 18, 2005 at 8:29 AM 3 replies 1.1k views
Original Post
Ging
Ging
Update : I've solved the first problem - I was normalising the vectors that made up the rotation matrix a tad too early... go figure! However, I still have the issue of the angle not going over 180! I use this to extract the angle from the matrix:

float CPlayer::getOrientation()
{
	float trace = pBody.orientation.RotX.x + pBody.orientation.RotY.y + pBody.orientation.RotZ.z;
	float fcos = 0.5*(trace-1.0);
	float angle = acos(fcos);
	return angle;
}


and this to calculate the required forces:

	angle = player.getOrientation();
	angledeg = angle * RTOD;
	xforce = sin(angle) * 0.05;
	yforce = cos(angle) * 0.05;


The player can now move perfectly well up and down the screen, but not to the left - the angle seems to be constrained within 0 - 180. I've checked the value of trace and that starts at 3 (0 degrees) and goes to -1 (180 degrees) but again doesn't go any further. I assume it's something with the extraction of the angle from the matrix, but I'm not sure how I might fix it! if anyone has any ideas, I'd be most thankful to hear them! [Edited by - Ging on March 18, 2005 11:27:20 AM]
Zakwayda
Zakwayda
Just addressing the angle <= 180 issue, I think the problem is that you're only doing the 'angle' part of an axis-angle extraction. The angle by itself does't tell you much about what orientation the matrix represents. To get the 'full story' you need the axis as well.

I think acos() will only return a result in the range [0, 180] (actually in radians of course). However, any axis-angle combination with the angle in the range [180, 360] can be represented by an angle in [0, 180], with the axis negated. So if you extract both the axis and the angle from the matrix, you should have all of the information you need.

Let me know if you need any help with the code for extracting the axis.
jvsquare
jvsquare
I agree with jyk, I think your problem is that you're not computing the axis as well. Assuming your rotation is around z, you could rotate 90 degrees with the rotation vector pointing along +z, or 90 degrees with the rotation along -z, and the results will be different. Looking down on the xy-plane, this boils down to rotating counterclockwise or clockwise.

Again, assuming you're only rotating around z, and your angle lies between 0 and 180, you can determine which one it is by computing (pBody.orientation.RotY.x - pBody.orientation.RotX.y). If the result is positive, it's rotating one way; if it's negative it's rotating the other. I can't tell you definitely which is which because I don't know if you're using row or column vectors -- but it should be easy to figure out. If your angle is 0 or 180, the direction doesn't matter.

That's for your specific case. More info on general rotation matrices can be found on Dave Eberly's site, here.
JohnBolton
JohnBolton
It's probably not related to your question, but it is always worth noting. If you use acos() or asin() you must clamp the input parameter. Even with perfectly valid data in your case, it is possible for fcos to be > 1.0 due to floating-point imprecision. For example, if 1.0000000001 is passed to acos(), it will return NaN instead of 0.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Ging
Ging
It was indeed because I wasn't computing the axis, it works nicely now that I am! Cheers for the help, I don't know why I didn't think of it myself, think my brain had been fried from trying to figure out what was wrong in the first place.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.