Positioning a sprite if frame rate is low

I use this parabolic logic when the sprite jumps. When the user clicks on the jump button, I set mVY = -300

int mVY = 0;   
public override void Update(GameTime theGameTime)
{
    Vector2 speed = Vector2.Zero;
    Vector2 direction = Vector2.Zero;

    if (mVY < 0)
    {
        speed.Y -= mVY;
        direction.Y = -1;
    }
    else
   {
        speed.Y += mVY;
        direction.Y = 1;
    }

    mVY += 8; 
    if (mVY > 300)  mVY = 300; // Limit maximum speed if falling

    Position += direction * speed * (float)theGameTime.ElapsedGameTime.TotalSeconds;

    ...
}

This code works ok when my game is running at 60fps, however when it’s less than that the jumps are very high and slow. I thought that multiplying by theGameTime.ElapsedGameTime.TotalSeconds would fix the problem, but I must be missing something because it doesn’t work properly.

Any ideas?

probably because you are accelerating your accelleraton.

mVY += 8;
speed.Y += mVY;
Position += … speed …blahablah

try mVY = 8 // so its a Force; += there is probably messing you up.

eg a object has
A vector2 motion or velocity if you like.
Or it has a speed which is a scalar so its a float.
A scalar * a direction is a velocity.

so

A position changes by its own velocity plus a acceleration.
A position change maybe described by its direction times its own speed + any other direction * force applied to it as a acceleration.
Such that a cumulative velocity over a amount of time is its motion.

Try to always define and think of a direction as a normalized unit length vector.
Think of velocity’s as a amount of speed or force if you like multiplyed onto a direction influencing it if you will to increase each of its component x and y magnitudes.
Think of motion in regards to elapsed time which is variable, as a additive to positions.

public override void Update(GameTime theGameTime)
{
    // if keyboard keys down we set ints or maybe ui_thrust_up = keys.ArrowUp ?

    direction = Vector2.Zero;
    if (ui_thrust_Up)
        direction.Y = -1;
    if (ui_thrust_Down)
        direction.Y = 1;
    if (ui_thrust_Left)
        direction.X = -1;
    if (ui_thrust_Right)
        direction.X = 1;

    direction.Normalize();

    // basically you keep your objects current velocity saved too.

    gameObjectsVeclocity +=  (itsThrustDirection * itsThrustForce);
    gameObjectsPosition += gameObjectsVeclocity * elapsedTime;

    when you apply other forces they are additive to each other.

    gravityDirection = postionOfGravityObject - gameObjectsPosition;
    gravityDirection.Normalize(); 
    gameObjectsVeclocity +=  (itsThrustDirection * itsThrustForce) + ( gravityForce / Distance * gravityDirection );
    gameObjectsPosition += gameObjectsVeclocity * elapsedTime;

.
}

1 Like

willmotil, thanks so much for your detailed answer. I understand much better the concept now.
You are right, what I was doing didn’t make too much sense.

I’m trying to use your code but I’m having some trouble about postionOfGravityObject and Distance. Where do I get those values? Sorry about that newbie question…

Thanks.

I think I found the way. I found this article and it’s easier than I thought. :slight_smile: