Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!