Having troubles with rotations, strrange troubles

Problems with Canvas, OpenGL, etc...

Having troubles with rotations, strrange troubles

Postby Seidr » Thu Sep 09, 2010 9:01 pm

Heya,

Long time lurker, first time poster. Really appreciate the forum - got some great tips from here on a variety of subjects to do with Android/Java!

I've started working on a little project, the first part of which I thought I had sorted earlier today. The first part involves being able to have a cube move around a playing field by clicking in one of four places (top left bottom right). The animation appeared to be fine so I changed about the texture and UV map to ensure it was infact rotating correctly. It all seems to work ok UNTIL the animation portion is over - and even then only for certain situations.

Example: in the video linked below you will see me moving the cube up, down, left and right. As you can see, that renders fine. Then I move it up one square and left one, then right one. On both these movements the rotation goes a little bit..squiffy. Instead of rotating on the x and y axis, it appears to be applying rotation to the z axis (note that the direction the number faces changes). The same happens if I move it down one square and move it to the left or right.



Here is the code from my draw loop. This is based off of the NeHes tutorial #6 ported to the Android - the only changes to the cube class being a modified UV map and texture.

cubeMoving: an integer that acts as a flag to determine which direction the cube is moving in
cubeMovingStep: an integer that acts as a progress marker for the animation
cubeXPos/cubeYPos: integers that determine the position of the cube based off of the default orientation
cubeXState: a float that stores the incremented rotations gained from cube movements

If anyone can provide any insight as to why this might be happening, I'd be greatly appreciated. If you need any further code I can provide it.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.          * Here we do our drawing
  3.          */
  4.         public void onDrawFrame(GL10 gl) {
  5.                 //Clear Screen And Depth Buffer
  6.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);       
  7.                 gl.glLoadIdentity(); //Reset The Current Modelview Matrix
  8.                
  9.                 //Drawing
  10.                 gl.glTranslatef(0.0f, 0.0f, -20.0f); //Move 5 units into the screen
  11.  
  12.                 if (cubeMoving>0) {
  13.                         //Use a nice smooth sine to provide a hop
  14.                         cubeMovingStep++;
  15.                         if (cubeMovingStep>9) {
  16.                                 switch (cubeMoving) {
  17.                                 case CUBE_MOVING_UP:
  18.                                         //Y position +1, X rotation -1
  19.                                         cubeYPos+=1;
  20.                                         cubeXState-=90.0f;
  21.                                         break;
  22.                                 case CUBE_MOVING_DOWN:
  23.                                         //Y position -1, X rotation +1
  24.                                         cubeYPos-=1;
  25.                                         cubeXState+=90.0f;
  26.                                         break;
  27.                                 case CUBE_MOVING_LEFT:
  28.                                         //X position -1, Y rotation -1
  29.                                         cubeXPos-=1;
  30.                                         cubeYState-=90.0f;
  31.                                         break;
  32.                                 case CUBE_MOVING_RIGHT:
  33.                                         //X position +1, Y rotation +1
  34.                                         cubeXPos+=1;
  35.                                         cubeYState+=90.0f;
  36.                                         break;
  37.                                 }
  38.                                 cubeMoving=0;
  39.                                 cubeMovingStep=0;
  40.                         } else {
  41.                                 //'Bump' the cube so that it appears that it is turning on a corner instead of 'floating' through the board
  42.                                 gl.glTranslatef(0.0f,0.0f,(float) (0.1*Math.sin((0.3*cubeMovingStep)*2)));
  43.                         }
  44.                         //Translate cube to current position
  45.                         gl.glTranslatef(2.0f*(float)cubeXPos,2.0f*(float)cubeYPos,0.0f);
  46.                         switch (cubeMoving) {
  47.                         case CUBE_MOVING_UP:
  48.                                 //Translate cube to correct position in movement and rotate on X negatively
  49.                                 gl.glTranslatef(0.0f,2.0f*((float)cubeMovingStep/9.0f),0.0f);
  50.                                 gl.glRotatef(-((float)cubeMovingStep*10.0f),1.0f,0.0f,0.0f);
  51.                                 break;
  52.                         case CUBE_MOVING_DOWN:
  53.                                 //Translate cube to correct position in movement and rotate on X positively
  54.                                 gl.glTranslatef(0.0f, -2.0f*((float)cubeMovingStep/9.0f),0.0f);
  55.                                 gl.glRotatef((float)cubeMovingStep*10.0f,1.0f,0.0f,0.0f);
  56.                                 break;
  57.                         case CUBE_MOVING_LEFT:
  58.                                 //Translate cube to correct position in movement and rotate on Y negatively
  59.                                 gl.glTranslatef(-2.0f*((float)cubeMovingStep/9.0f), 0.0f, 0.0f);
  60.                                 gl.glRotatef(-((float)cubeMovingStep*10.0f), 0.0f, 1.0f, 0.0f);
  61.                                 break;
  62.                         case CUBE_MOVING_RIGHT:
  63.                                 //Translate cube to correct position in movement and rotate on Y positively
  64.                                 gl.glTranslatef(2.0f*((float)cubeMovingStep/9.0f),0.0f,0.0f);
  65.                                 gl.glRotatef((float)cubeMovingStep*10.0f,0.0f,1.0f,0.0f);
  66.                                 break;
  67.                         }
  68.                 } else {
  69.                         //Cube is not moving, translate cube to current position
  70.                         gl.glTranslatef(2.0f*(float)cubeXPos,2.0f*(float)cubeYPos,0.0f);
  71.                 }
  72.                 //Perform rotation on cube (cubeXState*90 on X axis, cubeYState*90 on Y axis)
  73.                 gl.glRotatef(cubeXState, 1.0f, 0.0f, 0.0f);
  74.                 gl.glRotatef(cubeYState, 0.0f, 1.0f, 0.0f);
  75.                
  76.                 //Render the cube
  77.                 cube.draw(gl);
  78.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Many thanks,
Seidr

edit: thinking through it, I assume it has to do with how I'm applying the rotations at the bottom of the draw loop. is there another way that I should be combining these two rotations?
Seidr
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 09, 2010 8:43 pm

Top

Re: Having troubles with rotations, strrange troubles

Postby Seidr » Thu Sep 09, 2010 11:53 pm

After alot of thinking, and scratching my head I think this is more to do with how I'm tracking the rotation of the cube. Due to it moving around the board it is not a simple matter of keeping track of and restoring the rotations. As the attached scribble shows, this is more of a logic problem than a problem with the OpenGL code.

Image

Maybe some sleep will help.
Seidr
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 09, 2010 8:43 pm

Re: Having troubles with rotations, strrange troubles

Postby Seidr » Fri Sep 10, 2010 11:53 am

After further deliberation, I believe the above problem arises due to the fact when you rotate with glRotatef any further rotations are subject to any prior transformations/rotations. Due to this constraint, I must apply a single rotation that accounts for both the X and Y rotation. This I think must be done using Quaternions.

I've spent time looking into 'Quaternions', with some success but mostly befuddlement. I'll keep trying.
Seidr
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 09, 2010 8:43 pm

Re: Having troubles with rotations, strrange troubles

Postby Seidr » Fri Sep 10, 2010 1:36 pm

Huzzah! Thankfully I've resolved this problem - using Quaternions. Below is a video of the cube in its current state.



When I get home I'll clean up the code and post the solution! For anyone looking to use Quaternions for something like this, check out this StackOverflow post!

http://stackoverflow.com/questions/2758501/rotating-in-opengl-relative-to-the-viewport
Seidr
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 09, 2010 8:43 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest