### Author Topic: Game Physics  (Read 712 times)

0 Members and 1 Guest are viewing this topic.

#### lukaius

• Guest
##### Game Physics
« 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.

#### NGM88

• Full Member
• Posts: 154
##### Re: Game Physics
« Reply #1 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.

#### achpile

• Full Member
• Posts: 220
##### Re: Game Physics
« Reply #2 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>

#### lukaius

• Guest
##### Re: Game Physics
« Reply #3 on: December 15, 2017, 09:04:41 pm »
I don't follow on that. What do you mean jump power

#### achpile

• Full Member
• Posts: 220
##### Re: Game Physics
« Reply #4 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

#### Tigre Pablito

• Full Member
• Posts: 198
##### Re: Game Physics
« Reply #5 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()

« Last Edit: December 16, 2017, 07:40:58 pm by Tigre Pablito »

#### lukaius

• Guest
##### Re: Game Physics
« Reply #6 on: December 17, 2017, 07:32:56 pm »
Thanks tigre that really helped;
« Last Edit: December 20, 2017, 12:30:00 am by lukaius »

anything