From 3D Matrices, Euler angles and Quaternions

Eule is the German word for Owl, but Euler ? .
OK I have to admit, I was never really good at maths and whatever I learned, now I struggle already calculating percentages. And maybe in the past week I understood why maths can be important in programming or scripting….
So what I am trying to do ?
Well I created a globe in Papervision3D and I made it so you can use the mouse to turn it around. And I created this function from scratch, logical thinking which I was really proud of. But it did not work quite right. It worked, but if you had rotated the globe already more than one complete turn it would suddenly turn in the complete opposite direction. I tried to solve this by saying it should “reset” once it is turned completely but it did not work. So I had to go out and search for a solution.
And Federico Calvo did have one. One which I would never come up with. He uses 3d Matrix multiplication for the rotation instead of the normal rotation in x y z.
Now I already know a little about it and this is how it works:
Every object in Papervision3d has a transformation matrix. It looks something like this
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

And it defines the pitch, roll and yaw of an object. If someone is really interested how it exactly works here is a great tutorial
So the rotation is down by multiplying a rotation matrix to the existing matrix, Because the 3d matrix works globally and not locally the rotation works like it should! Amazing solution…but.
Now the rotation is not as straight forward as “rotate 20 degress in x” It looks more like
1 0.342 2. 342 0
0.93 3 2.422 0
1 6 0.746 2
0 0 1 4

(just fictional values).
So now I wont to create another rotation based on x y and z values. And this has been driving me crazy for the past week. Seriously I woke up at 4 in the morning and thought I had dreamed the solution.
I will spare out the exact details of my journey through all possible options as this would just take to long but a quick summary:
There is something in Papervision called euler to matrix conversion and the other way round which should convert a 3d Matrix into Euler angles . – To clear up this definition question, it has something to do with the tangents but the euler angles represent the x y and z rotation values. -

This is one of my conversion attempts:

		var mousepointrot:Number3D = Matrix3D.matrix2euler(mouse3D.transform);
		var globerot:Number3D = Matrix3D.matrix2euler(_globe.transform);
		var tempmatrix:Matrix3D = _globe.transform;

		var mousematrix:Matrix3D = Matrix3D.euler2matrix(mousepointon);


		//var centrepoint:Number3D = new Number3D (0, 0, 500);

	//	trace(mousepointon.transformation);

		//var centrematrix:Matrix3D = Matrix3D.euler2matrix(centrepoint);


	//	trace(mouse3D.transform);

		_globe.transform.n11 -= mousematrix.n11;
		_globe.transform.n12 = mousematrix.n12;
		_globe.transform.n13 = mousematrix.n13;

		_globe.transform.n21 = mousematrix.n21;
		_globe.transform.n22 = mousematrix.n22;
		_globe.transform.n23 = mousematrix.n23;

		_globe.transform.n31 = mousematrix.n31;
		_globe.transform.n32 = mousematrix.n32;
		_globe.transform.n33 = mousematrix.n33;

It SEEMS from what I can tell, that the euler conversion in Papervision is not accurate and missing out the correct rotation in z . So everytime you do a new conversion the z rotation will be slightly off and the more you “use” the function the worse the result gets. Or the greater the value for Z, the more inaccurate the calculation gets.

So if you can’t count on Euler, what about Quaternions? Well maybe you could, and again there are some nice tutorial about this topic. ( But for me, I tried some of the suggestions but I have to admit I don’t really understand how it works, and I am far to tired and have spent too much time on this matter to go deeper into the subject.

In the end, I created a “workaround”: Instead of rotating the globe, I am rotating the camera and resetting it again before the user makes a new move. So to the user it looks like I solved the problem ( though the user never knew there was one ;-) but it is still a bit unsatisfying after all this work and thought to not have a solution… So I have to say to myself: “I resign. And I believe Papervision3D is pretty much dead…So maybe next year I can explore Sandy and see how they go about this issue”

Print Friendly

Leave a Reply

+ four = 6