Need bulletproof angle extraction from matrix!

Blitz3D Forums/Blitz3D Programming/Need bulletproof angle extraction from matrix!

fredborg(Posted 2003) [#1]
Hi,

In my effort to write a 3ds importer I've really hit the head on the wall! I can't seem to find any method of extracting Euler angles from the transformation matrix that is 100% bulletproof. The one I'm currently using, is the closest I've come, it looks like this:
xrot# = ASin(-m(1,2))
If xrot<90.0
	If xrot>-90.0
		zrot# = ATan2(m(0,2),m(2,2))
		yrot# = -ATan2(m(1,0),m(1,1))
	Else
		zrot# = -ATan2(-m(0,1),m(0,0))
		yrot# = 0.0
	End If
Else
	zrot# = ATan2(-m(0,1),m(0,0))
	yrot# = 0.0				
End If

It produces the exact same angles as Blitz' LoadAnimMesh command, but at 90 and -90 degrees Pitch (xrot), it utterly fails, if one of the other axes have been rotated. It's a real pain, so if anyone has got a solution ( Mark ? :) ), I would really appreciate it! Perhaps it's possible to do some sneaky align to vector stuff???

PS: it's a 3x3 rotation+scale matrix, that has been unified.

Fredborg


JoshK(Posted 2003) [#2]
When using 4x4 matrices, I found that I had to write special cases for when the rotation was only about one axis.


JoshK(Posted 2003) [#3]
I think your matrix commands are buggy. I just wrote some that work all the time, without checking if pitch>90 or anything like that. Check my other matrix post.


JoshK(Posted 2003) [#4]
Hey, there are quarternion conversion routines in the archive. How are quarternions and matrices related?


FlameDuck(Posted 2003) [#5]
but at 90 and -90 degrees Pitch (xrot), it utterly fails
That's because Atan (Arcus Tanget) returns NaN in cases of 90 or -90. Usually you would need to get cotan for that (ofcourse there isn't such a function in Blitz). You can emulate it tho'.


fredborg(Posted 2003) [#6]
I know about ATan, and as you can see from the code I'm not using ATan to extract the Pitch. I'm thinking I need to convert the matrix to quaternion first, and then to Euler angles to get a reliable result...

Fredborg


FlameDuck(Posted 2003) [#7]
I know about ATan, and as you can see from the code I'm not using ATan to extract the Pitch.
Right. You're using ASin. ASin (as it looks like you might know) only returns angles between -90 and 90 (a 180 degree arc) regardless of what the angle *actually* was. A "real" angle of 135 would be returned as a 45 degree angle as illustrated by this short program:
myangle=135
mynewangle=ASin(Sin(myangle))
Print myangle
Print mynewangle
WaitKey
End



fredborg(Posted 2003) [#8]
Yes, but then pitch, can only be between -90 and 90, so no problems there... But I think I've finally got my head around how to do it. Thanks, for the help anyway :)

Fredborg


JoshK(Posted 2003) [#9]
.