Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Create a dynamic jump

This thread is locked; no one can reply to it. rss feed Print
Create a dynamic jump
MegaRubber
Member #22,311
January 2022

ya
i was thinking to make my character jump with variable height, similar to platform games (e.g. super mario bros).

the easiest way that i know is to create two jump buttons (pressed and down), and use a min (-y goes to up) function when the character is falling.

i tried to use the keypressed() (not worked) and the key[] array (looks like a down button).

So my question as a beginner in allegro: which function/flag i could use to detect these two types of events? (pressed and down)

PS: i'm using allegro 4 and creating a platformer game

Scooter
Member #16,799
January 2018

Hi:
When I did my platformer I just used a series of images.
It worked very well. If you want, I can send you what I did.

DanielH
Member #934
January 2001
avatar

Look into position, velocity, acceleration equations

With pressing any key direction, all you're going to do is change the acceleration

Velocity is based on acceleration
v(t) = v(0) + a(t)

You can split this up for x and y
vx(t) = vx(0) + ax(t)
vy(t) = vy(0) + ay(t)

Position is a function of velocity and acceleration
P(t) = P(0) + v(t) + 1/2a(t^2)

When you press the jump key. Add a negative acceleration (assuming negative is up). There is always a positive acceleration due to gravity. The negative acceleration must exceed the positive to make him jump.

Gravity is a change in velocity of (9.8m/s) every second. You would need to adjust for frame rate and pixel size. How many pixels is one meter? How many frames are processed is one second?

Of course any collision would drop the velocity to zero. You could base damage if velocity was too great the player hit the wall.

MegaRubber
Member #22,311
January 2022

Scooter said:

Hi:
When I did my platformer I just used a series of images.
It worked very well. If you want, I can send you what I did.

If you could, I would be very grateful!

DanielH said:

Look into position, velocity, acceleration equations

With pressing any key direction, all you're going to do is change the acceleration

Velocity is based on acceleration
v(t) = v(0) + a(t)

You can split this up for x and y
vx(t) = vx(0) + ax(t)
vy(t) = vy(0) + ay(t)

Position is a function of velocity and acceleration
P(t) = P(0) + v(t) + 1/2a(t^2)

When you press the jump key. Add a negative acceleration (assuming negative is up). There is always a positive acceleration due to gravity. The negative acceleration must exceed the positive to make him jump.

Gravity is a change in velocity of (9.8m/s) every second. You would need to adjust for frame rate and pixel size. How many pixels is one meter? How many frames are processed is one second?

Of course any collision would drop the velocity to zero. You could base damage if velocity was too great the player hit the wall.

I finished the logic of variable jump and I didn't use these equations, but the logic of them, gravity, and the info about properties of the game were very helpful.

thx :)

Scooter
Member #16,799
January 2018

Hi Mega:

Above is 'mario.zip'. This is a very simple example of mario jumping. No physics!

Use Left and Right arrow keys to move mario. Press Space Bar to make mario jump up

and spin around and come back down.

This uses Allegro 5 and done on Linux. You should have no problem to make it run

on Allegro 4. If zip file does NOT work for you, let me know and I will send you

another one. Hope this helps. Have a great day!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

All you need for jumping is a few simple equations.

If you know the duration you want your player to be in the air, and you know the force of gravity, it's easy to predict it's max jump height.

h(t) = h0 + v0*t + a*t^2 / 2
h`(t) = v0 + at
h`(t/2) = v0 + at/2 = 0
a = -2v0/t
h(t/2) = h0 + v0*t/2 + a*t^2/4 / 2
h(t/2) = v0*t/2 + -2v0*t^2/t/4 = v0*t/4

t is the duration, h is the height, a is the acceleration of gravity, v0 is the initial jump velocity.

RmBeer2
Member #16,660
April 2017
avatar

In A4 you can use fixed numbers.

All you have to do is add each frame the value of gravity to the object that jumps to modify the force of fall.

grav = 0.2;

BY JUMP:
movy = -2;

BY FRAME:
movy += grav;
obj.y += movy;

You just have to adjust the values according to the game. And you may want to make it jump higher the longer the jump is held, it just adds or initializes again on 'movy'.

🌈🌈🌈 🌟 BlackRook WebSite (Only valid from my installer) 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021! Rm Beer for Ruinous Slave Drained 2022! Rm Beer for Traveler From The Future Warning Not To Enter In 2023! Rm Beer are building a travel machine for Go Back from 2023! Rm Beer in an apocalyptic world burning hordes of Zombies in 2024!

Chris Katko
Member #1,881
January 2002
avatar

Depending on the game, you don't even use proper physics.

Many games have "fixed velocity up" for X frames, then "fixed velocity down", instead of acceleration. Depends on what you want.

In that case it's like

#SelectExpand
1struct player{ 2float x, y, height; 3} 4 5//attempt to start jump here (because you pressed a jump key) 6if(!is_jumping && !is_falling) //if we're not falling or jumping up, we can start a jump 7 { 8 is_jumping == true; 9 is_falling == false; 10 height = 0; //reset our jump height counter. 11 } 12 13if(is_jumping && !is_falling) //if we're jumping, add height until we reach max 14 { 15 y += 5; 16 height += 5; //track how far we've gone up since we started 17 if(height >= 100){is_jumping = false; is_falling = true;} //if we've gone far enough, stop going up 18 } 19if(!is_jumping && is_falling) //if falling, fall until we hit a floor 20 { 21 y -= 5; 22 if( we_touched_a_floor() ) {is_jumping = false; is_falling = false;} 23 }

Many older/retro games used this style. It's "one speed up/down" jumping. Instead of accelerating and decellerating.

Basically what I've done above is create a finite state machine. It sounds big/complex but it's not. There are three modes or "states" that a person is in.
- Normal/Idle/Walking (not jumping), that's is_jumping==false, is_falling==false. We're walking around.
- Jumping, that's is_jumping==true,is_falling=false. We're moving upward.
- Falling, that's is_jumping==false,is_falling=true. We're falling.

Each state has its own set of code that runs. Whether moving you up, moving you down, or not moving you up or down. And in a real game, you'd also have in those sections code for making your character "look" like he's jumping or falling like in Mario Bros.

And, mathematically, binary numbers can have only two numbers. Since we have three states, we'd need a minimum of two binary numbers (2^2 = 4 states max) to fit them in. If we wanted five states, we'd need (2^3 = 8 states max) three boolean numbers to store those possibilities.

-----sig:
β€œPrograms should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

amarillion
Member #940
January 2001
avatar

Lot's of games don't actually use proper physics. There is a lot of good analysis on youtube

https://www.youtube.com/watch?v=yorTG9at90g
https://www.youtube.com/watch?v=hG9SzQxaCm8

Or a search for game+design+jump will show up tons of useful stuff.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I think we're all missing the OT. Which was how to detect keypress and keydown events in Allegro 4.

https://bitbucket.org/bugsquasher/unofficial-allegro-5-binaries/downloads/Allegro443.chm

That's a CHM manual for Allegro 4.4. See keypressed() and readkey(). Keep an array of keystates and when they change state that is a press or a release. When their state stays the same, that is a hold / down or open / up.

while(keypressed()) {command(readkey());}

Go to: