Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Gravity  (Read 12518 times)

0 Members and 1 Guest are viewing this topic.

GrislyGames

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Gravity
« on: April 06, 2013, 04:09:36 am »
So I'm fairly new at programming in c++, and I've been trying to get some sort of gravity working in my 2D side-scroller. I am using SFML 2.0, and it would be great if someone could please explain to me how to implement gravity using an equation or whatever (an example would be nice as well). Also- I apologize if I am posting this in the wrong section of the forums. Thanks! ^__^

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Gravity
« Reply #1 on: April 06, 2013, 09:01:36 am »
Gravity is a constant which needs to be applied to your vertical velocity while in the air. You could do something like this:

const float maxY = 50f;
const sf::vector2f gravity(0.f, 5.f);
sf::vector2f velocity(2.f, 5.f);
 

And in your update loop:

if(playerInAir)
{
    player.move(velocity);
    if(velocity.y < maxY) velocity += gravity;
}
 

I've just made the values up as an example, you'll have to match those to your own timescale.
HTH.

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Gravity
« Reply #3 on: August 05, 2016, 10:38:43 pm »
Hi all

If the character is in the air, it can be jumping or falling.

It is needed to have something like the code below, where difs is a vector that contains the values that should be subtracted to the Y coordinate from 0 up to 29 (jumping up) indices and then added from 30 up to 59 (jumping down); if the character lands on the floor before reaching the index 59, then it is not in the air any more and the jump (let's say JumpAndFall) process ends; if the character reachs the index 59 and there's no floor under its feet, then it will continue falling (adding a constant to the Y coordinate) up to it finds the floor, or, in the worst case, it falls to the void and loses a life. 

When you press the jump key, AND if you are on the ground, then you need to set index = 1 and position = Positions.Jumping; if you walk and you are not steping on the floor any more, then you just need to set position = Positions.JumpAndFall (index is 0 and then you just fall).

I took this from my Mario game and adapted to be generic.

// this loads into the difs vector the values referred above - they could be stored and loaded from a file if preferred
for (a = 1; a < 31; a++)
       difs[a - 1] = (int)(Math.Round(Math.Sin(a * 3 * Math.PI / 180.0) * jumpMaxHeight - Math.Sin((a - 1) * 3 * Math.PI / 180.0) * jumpMaxHeight));
for (a = 31; a < 61; a++)
       difs[a - 1] = difs[60 - a];
 


// (suppose that jump starts with index = 1 and set position to Positions.JumpAndFall, which makes the below method to execute)
private void JumpAndFall()
{
 if (index > 0 && index < 31)
 {
        if (!Ceiling())
             Y -= difs[index - 1];
        else
             index = 60 - index;
 }
        else if (index > 30)
             Y += difs[index - 1];
        else if (index == 0)
             Y += 8;
        if (Floor() && (index > 30 || index == 0))
        {
             index = 0; position = Positions.Stand;  // the hero landed
        }
        if (index > 0 && index < 60)
             index++;
        else
             index = 0;
}
 

Hope this helps

 

anything