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
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.
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.
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!
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
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!
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.
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'.
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
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.
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.
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());}