Skip to main content
added 158 characters in body
Source Link

You should Maintain 3 vectors: Position, Velocity and Acceleration

Initially set acceleration -ve in y-axis to simulate gravity

And Update these vectors at every frame like this

Position += Velocity
Velocity += Acceleration

(But if you are on ground, don't update the y component of velocity and position)

Now when you need to jump, simply set the Y component of Velocity to some +ve value. And you will automatically observe a smooth jump.

What actually happens here, when you set the velocity's Y to positive value, the player starts to rise up in space but since there is also some -ve acceleration, the velocity will also decrease, and at a particular height, velocity will become 0 and starts to drop down. The best part of this approach is that, it gives you a great feel of realistic gravity.

Note: (I am taking down left as (0,0) coordinate)

E.g

Vector2D Position;
Vector2D Velocity;
Vector2D Acceleration;

void init()
{
    Position.X = 20;
    Position.Y = 20;
    Velocity.X = 10;
    Velocity.Y = 0;
    Acceleration.X = 0;
    Acceleration.Y = -10;
}

void update(int time)
{
    if(!onGround())
    {
        Position.Y += (time/1000)*Velocity.Y;
        Velocity.Y += (time/1000)*Acceleration.Y;
    }
    Position.X += (time/1000)*Velocity.X;
    Velocity.X += (time/1000)*Acceleration.X;
}

void jump()
{
    Velocity.Y = 50;
}

void onGround()
{
    //Check for collision with ground
}

You should Maintain 3 vectors: Position, Velocity and Acceleration

Initially set acceleration -ve in y-axis to simulate gravity

And Update these vectors at every frame like this

Position += Velocity
Velocity += Acceleration

(But if you are on ground, don't update the y component of velocity and position)

Now when you need to jump, simply set the Y component of Velocity to some +ve value. And you will automatically observe a smooth jump.

What actually happens here, when you set the velocity's Y to positive value, the player starts to rise up in space but since there is also some -ve acceleration, the velocity will also decrease, and at a particular height, velocity will become 0 and starts to drop down. The best part of this approach is that, it gives you a great feel of realistic gravity.

Note: (I am taking down left as (0,0) coordinate)

E.g

Vector2D Position;
Vector2D Velocity;
Vector2D Acceleration;

void init()
{
    Position.X = 20;
    Position.Y = 20;
    Velocity.X = 10;
    Velocity.Y = 0;
    Acceleration.X = 0;
    Acceleration.Y = -10;
}

void update(int time)
{
    if(!onGround())
    {
        Position.Y += (time/1000)*Velocity.Y;
        Velocity.Y += (time/1000)*Acceleration.Y;
    }
    Position.X += (time/1000)*Velocity.X;
    Velocity.X += (time/1000)*Acceleration.X;
}

void jump()
{
    Velocity.Y = 50;
}

void onGround()
{
    //Check for collision with ground
}

You should Maintain 3 vectors: Position, Velocity and Acceleration

Initially set acceleration -ve in y-axis to simulate gravity

And Update these vectors at every frame like this

Position += Velocity
Velocity += Acceleration

(But if you are on ground, don't update the y component of velocity and position)

Now when you need to jump, simply set the Y component of Velocity to some +ve value. And you will automatically observe a smooth jump.

What actually happens here, when you set the velocity's Y to positive value, the player starts to rise up in space but since there is also some -ve acceleration, the velocity will also decrease, and at a particular height, velocity will become 0 and starts to drop down. The best part of this approach is that, it gives you a great feel of realistic gravity.

Note: (I am taking down left as (0,0) coordinate)

Source Link

You should Maintain 3 vectors: Position, Velocity and Acceleration

Initially set acceleration -ve in y-axis to simulate gravity

And Update these vectors at every frame like this

Position += Velocity
Velocity += Acceleration

(But if you are on ground, don't update the y component of velocity and position)

Now when you need to jump, simply set the Y component of Velocity to some +ve value. And you will automatically observe a smooth jump.

What actually happens here, when you set the velocity's Y to positive value, the player starts to rise up in space but since there is also some -ve acceleration, the velocity will also decrease, and at a particular height, velocity will become 0 and starts to drop down. The best part of this approach is that, it gives you a great feel of realistic gravity.

Note: (I am taking down left as (0,0) coordinate)

E.g

Vector2D Position;
Vector2D Velocity;
Vector2D Acceleration;

void init()
{
    Position.X = 20;
    Position.Y = 20;
    Velocity.X = 10;
    Velocity.Y = 0;
    Acceleration.X = 0;
    Acceleration.Y = -10;
}

void update(int time)
{
    if(!onGround())
    {
        Position.Y += (time/1000)*Velocity.Y;
        Velocity.Y += (time/1000)*Acceleration.Y;
    }
    Position.X += (time/1000)*Velocity.X;
    Velocity.X += (time/1000)*Acceleration.X;
}

void jump()
{
    Velocity.Y = 50;
}

void onGround()
{
    //Check for collision with ground
}