•      Sign In
• Create Account

## Combined Rotation around Global Axis

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Schlabbermampf  Members

111
Like
0Likes
Like

Posted 15 May 2013 - 03:07 AM

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

### #2Álvaro  Members

20254
Like
0Likes
Like

Posted 15 May 2013 - 05:09 AM

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.

### #3Schlabbermampf  Members

111
Like
0Likes
Like

Posted 16 May 2013 - 10:45 AM

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

### #4Álvaro  Members

20254
Like
0Likes
Like

Posted 16 May 2013 - 04:58 PM

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...

### #5Schlabbermampf  Members

111
Like
1Likes
Like

Posted 22 May 2013 - 03:10 AM

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)).

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?

Edited by Schlabbermampf, 22 May 2013 - 03:13 AM.

### #6Álvaro  Members

20254
Like
0Likes
Like

Posted 22 May 2013 - 08:47 AM

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.

Edited by Álvaro, 22 May 2013 - 08:48 AM.

### #7Schlabbermampf  Members

111
Like
0Likes
Like

Posted 23 May 2013 - 06:42 AM

hehe, thanks

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);


### #8Álvaro  Members

20254
Like
0Likes
Like

Posted 23 May 2013 - 08:44 AM

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.

Edited by Álvaro, 23 May 2013 - 08:45 AM.

### #9Schlabbermampf  Members

111
Like
0Likes
Like

Posted 29 May 2013 - 02:39 AM

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.

Edited by Schlabbermampf, 29 May 2013 - 08:21 AM.

### #10Álvaro  Members

20254
Like
0Likes
Like

Posted 29 May 2013 - 08:09 AM

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.

Edited by Álvaro, 29 May 2013 - 08:09 AM.

### #11Schlabbermampf  Members

111
Like
0Likes
Like

Posted 29 May 2013 - 08:20 AM

HA! yes, nice, not this reversing of the rotation also makes sence! thank you so much!

and also you're really a wizard Álvaro, your code works perfectly in every letter

(Gum stands for grafic unit matrix or so and provides functions for matrix handling on the psp)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.