SFML community forums

Help => General => Topic started by: lukaius on December 15, 2017, 06:00:23 pm

Title: Game Physics
Post by: lukaius on December 15, 2017, 06:00:23 pm
Hello, I am trying to make a platforming game, I have all of the other physics set up BUT jumping.
To put it simply , I want to know how to make an entity go up for a little bit then down.
               
 
Title: Re: Game Physics
Post by: NGM88 on December 15, 2017, 06:43:54 pm
I'm sure if you google "c++ how to make sprite jump" or something similar you can figure it out.
Title: Re: Game Physics
Post by: achpile on December 15, 2017, 09:02:21 pm
Hello, I am trying to make a platforming game, I have all of the other physics set up BUT jumping.
To put it simply , I want to know how to make an entity go up for a little bit then down.
               
 

just set acceleration to -<jump power>
Title: Re: Game Physics
Post by: lukaius on December 15, 2017, 09:04:41 pm
I don't follow on that. What do you mean jump power
Title: Re: Game Physics
Post by: achpile on December 16, 2017, 08:27:19 am
Oh sorry. Not acceseration, but speed.
For example i have gravity acceleration 1000.0f, so if player jumps - I just set his speed.y to -375
Title: Re: Game Physics
Post by: Tigre Pablito on December 16, 2017, 07:37:03 pm
Hi lukaius

Maybe the most precise way is to use the quadratic function to get a parable effect

Let's say maximum jump height is 'maxHeight'

Then we can have the formula -x^2 + maxHeight  (-x^2 is the same as -(x^2), NOT (-x)^2)

Suppose the way from the floor to maxHeight lasts 30 frames (you can change this)

Your 'x' should start in -Sqrt(maxHeight) + Sqrt(maxHeight) / 30

When x == -Sqrt(maxHeight) you are about to take off

When x == 0 you are at maxHeight

When x == Sqrt(maxHeight) you have landed (*)

(*) It may happen that the land where you are traveling is not flat at all, so you may land before reaching the Y position from where you took off or after that (in which case you just need to stop the jump or continue it until you reach the floor, respectively)

What concerns to X scrolling is another issue that has nothing to do with this

The code


when (jump key is pressed)
[ if (player.Floor()) { n = 1; floor = y; player.position = Positions.Jump; } ]
// n is the number of frames from take off

// this should be into your player method
if (player.position == Positions.Jump)
{
    y = floor -(-pow(-Sqrt(maxHeight) + n * Sqrt(maxHeight) / 30, 2) + maxHeight);
    if (player.Ceiling())
        n = 30 * 2 - n;  // bounce with the ceiling and starts descending with the same speed
    if (player.Floor())  // you have landed
    {
        player.position = Positions.Stand;  // or whatever you want
    }
    n++;
}

// define Ceiling() and Floor()
 

Title: Re: Game Physics
Post by: lukaius on December 17, 2017, 07:32:56 pm
Thanks tigre that really helped;