## 3D Cube rotationg unexpectedly on touch

Problems with Canvas, OpenGL, etc...

### 3D Cube rotationg unexpectedly on touch

Hi everyone ,im new to this forum
hey buddies i am facing diffculty in solving some issues related to cube transformations

Im trying to rotate a 3D cube on touch. the distance moved to left or right is taken as the angle of rotation with respect to Y axis and the distance moved to up or down is taken as the angle of rotation with respect to X axis .
So these are the rotations im making
glrotate(angleXdistance, 0, 1, 0); // with respect to Y axis
glrotate(angleYdistance, 1, 0, 0); // with respect to X axis

so every time i add the angles with previous angles lik angleXdiatance + = angleXdiatance; in touch event

so the problem im facing is :
1.) when i rotate to right or left for 90 degree it rotates corectly ,and after when i rotate to up or down it rotates with respect to Z axis instead of Y axis
2.)when i rotate to right or left for 180 degree it rotates corectly ,and after when i rotate to up it rotates
down , to say in it rotates in opposite direction .

i could analyse that after 90/180 degree roataions the default axes changes , so it will roatate with respect to current axes , so i tried to chk the condition if it crosses 90 thn instead of rotating with respect to X rotate with respect to Z , and after 180 , reverse the rotation angle sign .. it works but nt always .. sm prblm occurs in sm other condition,,,

so i found out all posibilities , at certain angle of rotation , these are the current axes and thn passes those axes but still din work coz two rotations are happening so tht the number of possiblities increaes which is nt possible to find all conditions..

so if anyone has idea how to solve this issue plz help ... my cube and my head rotating unexpectedly plz help to rotate them correctly
i could see many examples or applications with such problem there also ...
keerthi
Junior Developer

Posts: 16
Joined: Thu Aug 12, 2010 6:40 am
Location: India

### Re: 3D Cube rotationg unexpectedly on touch

Hmm... I am not understanding what you mean by "The distance moved left or right is taken as the angle of rotation with respect to Y axis and the distance moved to up or down is taken as the angle of rotation with respect to X axis."

Could you please elaborate on this?

At any rate, you are correct in assuming that the axis are rotating along with the object. In OpenGL, there is what is called a "world coordinate system" and a "local coordinate system" The world coordinate system is the coordinate system of the scene. It is static unless manually transformed programmatically. Every geometry in the scene has its own local coordinate system.
jonbonazza
Master Developer

Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

### Re: 3D Cube rotationg unexpectedly on touch

oh sry for confusing you ;-p
ok let me be clear ..
im doing an app on android phone
'so i should be able to rotate a cube on touch
so when the user moves,or swipes .. i take the initial touch point and final release point ..tht is the distance i mean t..
so if the user moves his fingers to the right .. that means the cube should rotate to right side right ..
so tht distance im using to convert the distance into angle .. by scaling it to ..
angle =distance * touch scale factor( usually 180/320) where 180 is angle 320 the width of screen

so this angle i will be using to rotate with respect to Y axis similarly the distance moved up or down to rotate with respect to X axis ..

ya i agree with you about coordinates systems ,. but how achieve it plz help me ..
keerthi
Junior Developer

Posts: 16
Joined: Thu Aug 12, 2010 6:40 am
Location: India

### Re: 3D Cube rotationg unexpectedly on touch

Ah ok. I saw in your other thread that you were using an emulator. That is most likely your problem. As the other guy stated, the emulator suck for OpenGL because OpenGL is hardware specific. At any rate, if you want, you can send me a signed APK file and I will test it for you and let you know if it works on a real device.
jonbonazza
Master Developer

Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

### Re: 3D Cube rotationg unexpectedly on touch

Hey i got the solution for that rotation problem i multiplied the modelview matrix with previous model view matrix so that the transformations will be with respect to default axes
keerthi
Junior Developer

Posts: 16
Joined: Thu Aug 12, 2010 6:40 am
Location: India