Create a dynamic jump http://www.allegro.cc/forums/view/618593 Allegro.cc Forum Thread matthew@allegro.cc (Matthew Leverton) Sat, 22 Jan 2022 14:46:05 +0000

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

]]>
no-reply@allegro.cc (MegaRubber) Tue, 18 Jan 2022 23:42:02 +0000

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.

]]>
no-reply@allegro.cc (Scooter) Wed, 19 Jan 2022 01:04:10 +0000

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.

]]>
no-reply@allegro.cc (DanielH) Wed, 19 Jan 2022 03:52:34 +0000
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 ]]>
no-reply@allegro.cc (MegaRubber) Wed, 19 Jan 2022 09:42:37 +0000

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!

]]>
no-reply@allegro.cc (Scooter) Wed, 19 Jan 2022 18:59:18 +0000

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.

]]>
no-reply@allegro.cc (Edgar Reynaldo) Wed, 19 Jan 2022 23:37:40 +0000

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'.

]]>
no-reply@allegro.cc (RmBeer2) Thu, 20 Jan 2022 07:51:58 +0000

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

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.

]]>
no-reply@allegro.cc (Chris Katko) Thu, 20 Jan 2022 22:00:52 +0000

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

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

]]>
no-reply@allegro.cc (amarillion) Fri, 21 Jan 2022 14:24:53 +0000

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