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.
Using java Syntax Highlighting
- /**
- * Here we do our drawing
- */
- public void onDrawFrame(GL10 gl) {
- //Clear Screen And Depth Buffer
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity(); //Reset The Current Modelview Matrix
- //Drawing
- gl.glTranslatef(0.0f, 0.0f, -20.0f); //Move 5 units into the screen
- if (cubeMoving>0) {
- //Use a nice smooth sine to provide a hop
- cubeMovingStep++;
- if (cubeMovingStep>9) {
- switch (cubeMoving) {
- case CUBE_MOVING_UP:
- //Y position +1, X rotation -1
- cubeYPos+=1;
- cubeXState-=90.0f;
- break;
- case CUBE_MOVING_DOWN:
- //Y position -1, X rotation +1
- cubeYPos-=1;
- cubeXState+=90.0f;
- break;
- case CUBE_MOVING_LEFT:
- //X position -1, Y rotation -1
- cubeXPos-=1;
- cubeYState-=90.0f;
- break;
- case CUBE_MOVING_RIGHT:
- //X position +1, Y rotation +1
- cubeXPos+=1;
- cubeYState+=90.0f;
- break;
- }
- cubeMoving=0;
- cubeMovingStep=0;
- } else {
- //'Bump' the cube so that it appears that it is turning on a corner instead of 'floating' through the board
- gl.glTranslatef(0.0f,0.0f,(float) (0.1*Math.sin((0.3*cubeMovingStep)*2)));
- }
- //Translate cube to current position
- gl.glTranslatef(2.0f*(float)cubeXPos,2.0f*(float)cubeYPos,0.0f);
- switch (cubeMoving) {
- case CUBE_MOVING_UP:
- //Translate cube to correct position in movement and rotate on X negatively
- gl.glTranslatef(0.0f,2.0f*((float)cubeMovingStep/9.0f),0.0f);
- gl.glRotatef(-((float)cubeMovingStep*10.0f),1.0f,0.0f,0.0f);
- break;
- case CUBE_MOVING_DOWN:
- //Translate cube to correct position in movement and rotate on X positively
- gl.glTranslatef(0.0f, -2.0f*((float)cubeMovingStep/9.0f),0.0f);
- gl.glRotatef((float)cubeMovingStep*10.0f,1.0f,0.0f,0.0f);
- break;
- case CUBE_MOVING_LEFT:
- //Translate cube to correct position in movement and rotate on Y negatively
- gl.glTranslatef(-2.0f*((float)cubeMovingStep/9.0f), 0.0f, 0.0f);
- gl.glRotatef(-((float)cubeMovingStep*10.0f), 0.0f, 1.0f, 0.0f);
- break;
- case CUBE_MOVING_RIGHT:
- //Translate cube to correct position in movement and rotate on Y positively
- gl.glTranslatef(2.0f*((float)cubeMovingStep/9.0f),0.0f,0.0f);
- gl.glRotatef((float)cubeMovingStep*10.0f,0.0f,1.0f,0.0f);
- break;
- }
- } else {
- //Cube is not moving, translate cube to current position
- gl.glTranslatef(2.0f*(float)cubeXPos,2.0f*(float)cubeYPos,0.0f);
- }
- //Perform rotation on cube (cubeXState*90 on X axis, cubeYState*90 on Y axis)
- gl.glRotatef(cubeXState, 1.0f, 0.0f, 0.0f);
- gl.glRotatef(cubeYState, 0.0f, 1.0f, 0.0f);
- //Render the cube
- cube.draw(gl);
- }
Parsed in 0.090 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?


