Combined Rotation around Global Axis

Started by
9 comments, last by Schlabbermampf 10 years, 10 months ago

Hi there!

i am currently programming a rubiks cube for the playstation portable for my studies. so far it all works well except the rotation-control of the whole cube, so i'll explain it shortly:

the analog stick gives his X coordinate to the rotation function which adds this value with a small multiplicator to the total rotation angle around the global Y axis. the same with the analog stick's Y coordinate that lets the cube rotate around the global X axis.

to create the rotation matrix, the psp-api has a function sceGumRotateXYZ(float x, float y, float z) wich obviously uses euler angles.

so now my problem: the first rotation around the X-axis works well because the camera is fixed, but the second around the Y-axis rotates the cube around his local Y-axis, but i want it to rotate around the global Y-axis! for the first rotation it is similar if its around the global-world axis or the cube's local axis because they are the same, but for the second one the needed rotation-axis is transformed by the first one.

i don't know how to handle this situation. i just converted the Y-axis and the angle to euler angles with help of this article, but with the same fail-result. so i ask for your help.

greetings and best wishes

fabian

Advertisement

If you do the two rotations (around the X axis and around the Y axis) yourself, reversing the order of the rotations will probably fix your problem.

no, the reversed order lets the cube rotate around the Y-axis correctly, but the X-rotation is in the cube's local not in the global coordinate system. in other words: the problem is also reversed xD

You may have to explain the problem in more detail, perhaps with an example. It is possible you are just confused by the nature of 3D rotations themselves...

okay, i painted an image and will try to explain:

there is a coorinate system of the world in which the cube and the camera never change their position, i called it the global coordinate system.
then there is the coordinate system of the cube, which natually rotates by the rotation of the cube.
i want to rotate the cube along the global X and Y axis like a model viewer. so in the code, i used a rotationmatrix to rotate the cube around the first axis (image (1)). so now when i use a second rotation matrix, this one lets the cube move in his already transformed local coordinate system (image(2)). the desired rotation is orientated on the unchanged global coordinate system (image (3)).

14566849sq.jpg

i think the second rotation in global like in (3), is a combination of two or three euler-angles in local space. i try the work with an algorithm that convertes an axis-angle rotation to euler-angles. first i rotate the cube in xAngle by eulerrotation. then i take the y-axis (0, 1, 0) and turn it the x-rotation backwards (0.0f, cos(-xAngle), -sin(-xAngle)) to get from this axis and the yAngle the euler-angles for the second rotation. but then the problem returns? for now i do not have any correct solution...i cant believe that this is so difficult...maybe there is an easy way?

Nice diagrams! I go back to my original post. Whether you get (2) or (3) depends on the order in which you perform your rotations. Perhaps you can post the relevant code? Explain how you tried to reverse the order of the rotations.

hehe, thanks :D

okay, so here is the simple way i get (2) as code:


rotationAngle.y += analogStickX;
rotationAngle.x += analogStickY;

sceGumPushMatrix();
sceGumRotateX(rotationAngle.x);

sceGumPushMatrix();
sceGumRotateY(rotationAngle.y); 

this code has the same result so i think this funktion also do the same:


rotationAngle.y += analogStickX;
rotationAngle.x += analogStickY;

sceGumPushMatrix();
sceGumRotateXYZ(rotationAngle.x, rotationAngle.y, 0.0f); 

by reversing the order i simply understand this (the effect is still equal, just that the Y rotation is global and the X rotation is in local space):


rotationAngle.y += analogStickX;
rotationAngle.x += analogStickY;

sceGumPushMatrix();
sceGumRotateY(rotationAngle.y);

sceGumPushMatrix();
sceGumRotateX(rotationAngle.x);

I see what you are trying to do. Ditch Euler angles. Now. Did you ditch them yet? Good.

Now, use either a 3x3 orthogonal matrix or a unit-length quaternion to represent the attitude of your object. Don't keep track of an accumulator of how much you have rotated in the x and y directions, because those are Euler angles in disguise, and you'll get your current behavior, no matter what representation you are using. Then change this attitude by composing small rotations around the x and y axes in response to user input.

If you can't figure out some of these steps by yourself with the help of the web and a bit of work, feel free to ask and I'll try to help you.

thank you so far! i ditched eulers and this is what i tried:


sceGumLoadMatrix(&rotationMatrix); //load rotation matrix into "current"-register
sceGumRotateX(analogMoveY); //changing cube's attitude by small rotations depending on user input
sceGumRotateY(analogMoveX);
sceGumStoreMatrix(&rotationMatrix); //store rotation for next use
sceGumPushMatrix(); //push "current" matrix on matrix stack
 

i am not shure if this is what you meant, because the result has the reverse effect: the cube is always rotated in his local space and the controlling feels very confusing. thinking about it logically, the effect is also very predictable.

Back to my first suggestion: Reverse the order of the rotations.


sceGumLoadIdentity();
sceGumRotateX(analogMoveY);
sceGumRotateY(analogMoveX);
sceGumMultMatrix(&rotationMatrix);
sceGumStoreMatrix(&rotationMatrix);
sceGumPushMatrix();

I have no idea what this sceGum stuff is, so I am a wizard if that code works. :)

This topic is closed to new replies.

Advertisement