# rotating box keeps spinning

I have a box which will speed up its rotation depending on which arrow key you press. The longer you hold it, the faster it will go, and when you let go, it gradually slows down.
When I have it spin to the left or down, it works fine. When I have it spin to the right or up, it will speed up, slow down, but never completely stop. This only happens if I hold the key down for a while, if I just tap it or hold it for a second, it works as I intended it to.
When it does keep spinning to the right, it fixes itself if I have it spin to the left for an equal or greater length of time as I had it spin to right.

Does all that make sense? If it does, take a look at my code please.

these declarations are global:
GLfloat rcubeX = 0.0f;
GLfloat rcubeY = 0.0f;
GLfloat Xspeed = 0.0f;
GLfloat Yspeed = 0.0f;
GLfloat slower = 0.05f;
GLfloat faster = 0.1f;

here’s what I have for my left and right rotations, the up and down rotations are the same.

if(keys[VK_RIGHT])
{
Yspeed+=faster;
}
else
{
if(Yspeed >= 0.0f)
{
Yspeed-=slower;
}
}

if(keys[VK_LEFT])
{
Yspeed-=faster;
}
else
{
if(Yspeed <= 0.0f)
{
Yspeed+=slower;
}
}

here’s what draws and rotates the cube.

glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f, 0.0f, -6.0f);

glRotatef(rcubeX, 1.0f, 0.0f, 0.0f);
glRotatef(rcubeY, 0.0f, 1.0f, 0.0f);

DrawCube();

rcubeX+=Xspeed;
rcubeY+=Yspeed;

[This message has been edited by skorman00 (edited 06-07-2003).]

why is the speed up and slow down rates different?

I want it to slow down at a slower rate than it speeds up. Like when you spin a pinwheel, it’s easy to get it going fast, but takes a while to slow it down unless you touch it.

Tell me if this makes sense…
I tried making them both 0.1f, and it happened in all directions.
Then I changed it back, and there were no problems!
I changed it back to both 1.0f just to see what happened, and it kept spinning again.
Then I went back to 0.05f and 0.1f, and it kept spinning!

It worked for a brief moment in time…I have no clue what is going on, It’s seems to be on a random basis. I kept playing with the numbers, sometimes it would work, sometimes it wouldn’t. Even with the same numbers the results varied.

a little example:
Yspeed = 0.05
keys[VK_RIGHT] = FALSE
keys[VK_LEFT] = FALSE

it does:
Yspeed-=slower; // because !keys[VK_RIGHT] && Yspeed>=0.0
and

Yspeed+=slower; // because !keys[VK_LEFT] && Yspeed<=0.0

=> Yspeed=0.05 !!!

you’re correct…I should have used just > and <.

Even with that change though it still doesnt work. I’m trying to learn how to handle fonts and displaying bitmap fonts as we speak so I can find out exactly what my numbers are doing.

I found out what is happening to my Yspeed. With the numbers 0.1 and 0.05, it should never turn up as a number that isn’t divisible by 0.05 right? Well somehow it does. When the box spins for a while, the numbers will come up to be something like 1000.7 instead of 1000.1 or 1000.05 like I expect it to.

I don’t know if it’s has to do with the precision of a GLfloat or what…but by adding an if statement:
if(Yspeed > 0.0f && Yspeed < 0.1f)
{
Yspeed = 0.0f;
}
it works around it.

I’d figure I’d post that for anybody who would like to know what I found.