# cube rotation troubles

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.

It has to be in your code somewhere…
Past away that is the only way we can see whats going on.

Originally posted by Fallen_Angel:
[b]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.[/b]

Very well, hope you can find it here
The following three functios are called from the three listed here…hope that makes sense

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

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

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:
[b]Very well, hope you can find it here
The following three functios are called from the three listed here…hope that makes sense

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;
}[/b]