PDA

View Full Version : cube rotation troubles



Fallen_Angel
03-16-2002, 01:17 PM
I'm currently playing around with my first program, so far it's just a cube that you can rotate with the keyboard. The rotation itself is working fine. My problem is that when I release the key, the cube returns to the default state instead of staying in its current position. I'm not sure what this board is like when it comes to pasting lines and lines of code so I'll leave it out for now, but I can put up my keyboard and rotation code if it's required.

Thanks.

nexusone
03-16-2002, 03:08 PM
It has to be in your code somewhere....
Past away that is the only way we can see whats going on.

http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by Fallen_Angel:
I'm currently playing around with my first program, so far it's just a cube that you can rotate with the keyboard. The rotation itself is working fine. My problem is that when I release the key, the cube returns to the default state instead of staying in its current position. I'm not sure what this board is like when it comes to pasting lines and lines of code so I'll leave it out for now, but I can put up my keyboard and rotation code if it's required.

Thanks.

Fallen_Angel
03-16-2002, 03:28 PM
Very well, hope you can find it here http://www.opengl.org/discussion_boards/ubb/smile.gif
The following three functios are called from the three listed here...hope that makes sense :P

glutIdleFunc(spinDisplay);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);

static void spinDisplay (void)
{
nSpinX = nSpinY = nSpinZ = 0.0;
if (keys[1])
nSpinX = -1.0;
if (keys[0])
nSpinX = 1.0;
if (keys[3])
nSpinY = -1.0;
if (keys[2])
nSpinY = 1.0;
if (keys[5])
nSpinZ = -1.0;
if (keys[4])
nSpinZ = 1.0;
if ((nSpinX == 0.0) && (nSpinY == 0.0) && (nSpinZ == 0.0))
{
nSpinSpeed = 0;
Spin = 0.0;
}
else
nSpinSpeed = 2;
Spin += nSpinSpeed;
if (Spin > 360.0)
Spin -= 360.0;
if (Spin < 0.0)
Spin += 360.0;
glutPostRedisplay();
}

static void keyboard(unsigned char key, int x, int y)
{
if ((key == 'Q') &#0124; &#0124; (key == 'q'))
keys[0] = true;
if ((key == 'A') &#0124; &#0124; (key == 'a'))
keys[1] = true;
if ((key == 'W') &#0124; &#0124; (key == 'w'))
keys[2] = true;
if ((key == 'S') &#0124; &#0124; (key == 's'))
keys[3] = true;
if ((key == 'E') &#0124; &#0124; (key == 'e'))
keys[4] = true;
if ((key == 'D') &#0124; &#0124; (key == 'd'))
keys[5] = true;
}

static void keyboardUp(unsigned char key, int x, int y)
{
if ((key == 'Q') &#0124; &#0124; (key == 'q'))
keys[0] = false;
if ((key == 'A') &#0124; &#0124; (key == 'a'))
keys[1] = false;
if ((key == 'W') &#0124; &#0124; (key == 'w'))
keys[2] = false;
if ((key == 'S') &#0124; &#0124; (key == 's'))
keys[3] = false;
if ((key == 'E') &#0124; &#0124; (key == 'e'))
keys[4] = false;
if ((key == 'D') &#0124; &#0124; (key == 'd'))
keys[5] = false;
}

Old GLman
03-16-2002, 04:32 PM
Hi, say your holding down 'E'. Then keys[4] gets set to true. Then nSpinZ gets set to 1.0. Because its not zero the first if condition is false, and then proceeds to do nothing with the variable nSpinZ. The cube is only rotating because you set nSpinSpeed to 2. It looks like the keys are having no influence on spinning the cube. Is there any more code you need to post? It looks like there is something missing.

Old GLman

nexusone
03-16-2002, 05:18 PM
I will point a problem with spinDisplay, in the in the if/else statment you always set nSpinSpeed to two when a key is not pressed. I think this is your problem.


static void spinDisplay (void)
{
nSpinX = nSpinY = nSpinZ = 0.0;
if (keys[1])
nSpinX = -1.0;
if (keys[0])
nSpinX = 1.0;
if (keys[3])
nSpinY = -1.0;
if (keys[2])
nSpinY = 1.0;
if (keys[5])
nSpinZ = -1.0;
if (keys[4])
nSpinZ = 1.0;
if ((nSpinX == 0.0) && (nSpinY == 0.0) && (nSpinZ == 0.0))
{
nSpinSpeed = 0;
Spin = 0.0;
}
else
nSpinSpeed = 2;
Spin += nSpinSpeed;
if (Spin > 360.0)
Spin -= 360.0; //Why not set to zero?
if (Spin < 0.0)
Spin += 360.0; // Same here set to 360.
glutPostRedisplay();
}


Originally posted by Fallen_Angel:
Very well, hope you can find it here http://www.opengl.org/discussion_boards/ubb/smile.gif
The following three functios are called from the three listed here...hope that makes sense :P

glutIdleFunc(spinDisplay);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);

static void spinDisplay (void)
{
nSpinX = nSpinY = nSpinZ = 0.0;
if (keys[1])
nSpinX = -1.0;
if (keys[0])
nSpinX = 1.0;
if (keys[3])
nSpinY = -1.0;
if (keys[2])
nSpinY = 1.0;
if (keys[5])
nSpinZ = -1.0;
if (keys[4])
nSpinZ = 1.0;
if ((nSpinX == 0.0) && (nSpinY == 0.0) && (nSpinZ == 0.0))
{
nSpinSpeed = 0;
Spin = 0.0;
}
else
nSpinSpeed = 2;
Spin += nSpinSpeed;
if (Spin > 360.0)
Spin -= 360.0;
if (Spin < 0.0)
Spin += 360.0;
glutPostRedisplay();
}

static void keyboard(unsigned char key, int x, int y)
{
if ((key == 'Q') | | (key == 'q'))
keys[0] = true;
if ((key == 'A') | | (key == 'a'))
keys[1] = true;
if ((key == 'W') | | (key == 'w'))
keys[2] = true;
if ((key == 'S') | | (key == 's'))
keys[3] = true;
if ((key == 'E') | | (key == 'e'))
keys[4] = true;
if ((key == 'D') | | (key == 'd'))
keys[5] = true;
}

static void keyboardUp(unsigned char key, int x, int y)
{
if ((key == 'Q') | | (key == 'q'))
keys[0] = false;
if ((key == 'A') | | (key == 'a'))
keys[1] = false;
if ((key == 'W') | | (key == 'w'))
keys[2] = false;
if ((key == 'S') | | (key == 's'))
keys[3] = false;
if ((key == 'E') | | (key == 'e'))
keys[4] = false;
if ((key == 'D') | | (key == 'd'))
keys[5] = false;
}