# Jumpy movement

This topic is 2756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I use the following code to move my ship across the screen on one axis:

 if(abs(mVelocity) < 50 && mMoving) { mVelocity += 10 * (deltaTime / 100.0f); } else if(!mMoving && mVelocity != 0.0f) { mVelocity -= mVelocity/abs(mVelocity) * (12 * (deltaTime / 100.0f)); if(mVelocity < 0.01f) { mVelocity = 0.0f; } } mX += (mDir*mVelocity) * (deltaTime / 100.0f);

However, movement can get a jumpy at times. It'll jump forward a few pixels for a frame then jump back to where it should be. Am I missing something?

##### Share on other sites
To figure out problems like this, printf's are your friend. Add something like printf("%f %f %f %f\n", mx, mVelocity, deltaTime, mDir); after the loop. That will make it much clearer which part is causing the problem.

##### Share on other sites
Are all the variables involved of a floating-point type?

Also, what are the units of deltaTime? Is there any particular reason you're dividing it by 100?

##### Share on other sites
All values are floats until I pass the coordinates to OpenGL. I get deltaTime from SDL_GetTicks(), which is in milliseconds. I divide to get a decimal so I can use smaller numbers.

I'll have to look into getting the console up...SDL hides it.

##### Share on other sites
Dividing by 100 is somewhat arbitrary; it would make more sense, I think, to divide by 1000 to yield a value in seconds.

Also, instead of scaling deltaTime every time it's used, just scale it once at the beginning of the game loop.

Finally, although the right conversions may happen automatically, using the correct variable and literal types consistently will make things clearer and remove potential sources of error.

Not compiled or tested, but here's your code cleaned up and with some comments added (I don't know if you're using C or C++, or some other language, but this will be C, more or less):

float dt = (float)deltaTime / 1000.f; float absVelocity = abs(mVelocity); if (absVelocity < 50.f && mMoving) { mVelocity += 10.f * dt; } else if (!mMoving && mVelocity != 0.f) { // The logic here seems questionable. Here you have a potential // division by zero or near-zero, so you'll probably want to // rearrange this so the '.01' check is performed first. Also, // you probably want to be checking the absolute value of the // velocity against .01, not the velocity. (Or so I'm guessing.) mVelocity -= (mVelocity / absVelocity) * (12.f * dt); if (mVelocity < .01f) { mVelocity = 0.f; } } mX += mDir * mVelocity * dt;
I think there are at least a few things there that could be causing incorrect behavior, so I'd start by cleaning up the code a bit, and then see where that leaves you.

##### Share on other sites
Okay, it looks like my problem was with my code that decremented the velocity. I should have just put a flat value instead of dividing. No idea what I was thinking

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 18
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632604
• Total Posts
3007370

×