Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5: Implementing Gravity into a 2D game

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Allegro 5: Implementing Gravity into a 2D game
Egghead
Member #15,371
October 2013

Hey Forum Dudes/Chicks, I was wondering if anyone could show me how to implement gravity into a 2-D game so that I can make the main character jump. I assume this question has been asked many times, so if there is a previous forum post with sufficient information on this subject, that will do just fine. Thanks in advance!

Also, if I need to attach code to allow you to further help me, I will gladly do so!

-Egghead

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Aikei_c
Member #14,871
January 2013
avatar

I'd say, just use real world formulas and make a simple physics engine of your own.
You need to implement the following: velocity vector - how much position of a character changes per tick; acceleration vector - how much velocity changes per second; force - thing, that constantly causes acceleration, measured in newtons (each newton causes 1 m/s2 acceleration for a body with a mass of 1 kg) so you could:

  ApplyForce(character,directionDownVector,9.8);

And now you have gravity. Gravity acceleration should stop on collision with ground.
Then you need to implement impulse: impulse is like force but only causes acceleration once.
So, to jump a character, you would, say:

  ApplyImpulse(character,directionUpVector,100);

Ariesnl
Member #2,902
November 2002
avatar

This will be of great help I think...

http://natureofcode.com/book/chapter-1-vectors/

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Egghead
Member #15,371
October 2013

Thanks a ton for the help everyone, I looked a little bit more into actual physics equations and values and got a sort of primitive gravity system working on a single object in Allegro, which is exactly what I was looking for! I greatly appreciate your responses to my question.;D

Paul whoknows
Member #5,081
September 2004
avatar

All you need is love, ehemm, I mean, all you need is this ->


608058

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Arthur Kalliokoski
Second in Command
February 2005
avatar

You guys make it all look so complicated... Just subtract some small constant from the y speed each loop, allowing for collision with the ground or something to stop his fall.

They all watch too much MSNBC... they get ideas.

Audric
Member #907
January 2001

Just as Arthur said... Look at this series of points, where the Y position moves by: -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6. Pretty parabolic.

#SelectExpand
1 xxxx 2 xx xx 3 4 xx xx 5 6 7 xx xx 8 9 10 11 xx xx 12 13 14 15 16 xx xx 17 18 19 20 21 22xx xx

Paragon
Member #15,335
October 2013

You might want to look into Chipmunk2D, http://chipmunk-physics.net/

It's a 2D physics engine that'll do gravity, friction, collisions etc.

Paul whoknows
Member #5,081
September 2004
avatar

Audric: that won't work for variable height jumps (like Mario's ones). Kinematics is the tool you need for this sort of problems and there is no good reason not to use it.

608058

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Aikei_c
Member #14,871
January 2013
avatar

I though OP already figured out how to do what he wanted...

Egghead
Member #15,371
October 2013

Guys guys! I've got it down, all I needed was a basic example of gravity using the allegro library, thanks again!

Paul whoknows
Member #5,081
September 2004
avatar

It doesn't really matter if you already solved your problem, we can still discuss about this topic, remember, we are at allegro.cc, where you never know when a thread will die :o

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

SiegeLord
Member #7,827
October 2006
avatar

all you need is this ->

I've written a large number of games with gravity, and not once did I have to use that equation.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Paul whoknows
Member #5,081
September 2004
avatar

Can you post the equations you used? I'm sure those have to be equivalent to the one I posted.

I'm talking about projectile motion, this is the motion we need in order to model a jumping character, just like Mario, you need a formula like the one I posted, unless you went beyond Newtonian physics and discovered some new formulas (which I really doubt) :D

{"name":"projectile-motion.gif","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/7\/977ced46f0dbc932118e4e65ccb1a236.gif","w":387,"h":281,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/7\/977ced46f0dbc932118e4e65ccb1a236"}projectile-motion.gif

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Vanneto
Member #8,643
May 2007

If you have a Vector class:

object.velocity += gravity;

Done!

In capitalist America bank robs you.

l j
Member #10,584
January 2009
avatar

Yes indeed, no need for such formulas.
They aren't even very useful for games, since they take time as an argument while you want your physics to be step based in almost every circumstance.

object.velocity += world.gravity * object.drag; (with drag 0 < drag < 1 lower = more)
Now you've got gravity + an extremely simplified drag model, terminal velocity and slowing down, can also be used for friction.

(I tend to simply use simple if statements to limit the maximum velocity in any direction instead of the above)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Edgar said:

And some handy equations for jump calculations :

h = max height
v = initial velocity
t = jump duration assuming level ground
a = accel of gravity

h = vt/4

v = 4h/t

t = 4h/v

a = -2v/t

h = -v^2/2a

v = sqrt[-2ah]

a = -(v^2)/2h

So now if you know the terms on the right, you can get the corrseponding h, v, t, or a.

Did anybody or rather, do you find these useful for setting up your physics constants?

Go to: