|
|
| 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
|
I posted a bunch of equations for jumping in this old reply of mine : Ignore the code there, that's junk. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Aikei_c
Member #14,871
January 2013
|
I'd say, just use real world formulas and make a simple physics engine of your own. ApplyForce(character,directionDownVector,9.8);
And now you have gravity. Gravity acceleration should stop on collision with ground. ApplyImpulse(character,directionUpVector,100);
|
|
Ariesnl
Member #2,902
November 2002
|
This will be of great help I think... Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard) |
|
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
|
All you need is love, ehemm, I mean, all you need is this -> ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
|
Arthur Kalliokoski
Second in Command
February 2005
|
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. 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
|
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.
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
|
Aikei_c
Member #14,871
January 2013
|
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
|
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 ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
|
SiegeLord
Member #7,827
October 2006
|
Paul whoknows said: 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 |
|
Paul whoknows
Member #5,081
September 2004
|
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) {"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"} ____ "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
|
Yes indeed, no need for such formulas. object.velocity += world.gravity * object.drag; (with drag 0 < drag < 1 lower = more) (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
|
Edgar said: And some handy equations for jump calculations : h = max height 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? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
|