Quote:Original post by Thrust
His code would make complete sense, if I knew how to use velocity and gravity. I
don’t understand those two, if I did jumping would be a lot easier.
I see you say this over and over but the fact is you don't really need to understand velocity or gravity all that well for 2D. Let me see if I can break it down using Stompys code as an example.
First, your character should now contain an x_velocity and a y_velocity. This will control the speed that they move at on either the X(left and right) axis or the Y(up and down) axis.
If you add negative velocity to the X position of the character they will appear to move left on the screen. Likewise, add positive velocity to the X axis and your character will appear to move to the right. This also carries over to the Y axis, positive velocity moving down the screen, negative velocity moving up the screen.
Now here is where I am guessing you are running into trouble. You don't quite know what to set the velocity to initially. Depending on how you do it, it really doesn't matter. Just play around with some values for now. When it comes time for you to calculate anything relating to real physics of velocity you will have a lot of this groundwork already done.
So back to the point. Disregard gravity entirely. The concept is a little bit over kill for a basic 2D game that it seems like you are describing. We will be applying the "fake" gravity that Stompy described.
void Jump(void){ Player.jump = true; Player.yspeed = 10;}
This is good but I would make one alteration just so we don’t get unexpected behavior.
void Jump(void){ if(Player.jump == true) return; Player.jump = true; Player.yspeed = 10;}
Now there are no infinite jump chains. :)
Now looking at this jump we can see a couple things. The player enters a state of jumping if he is not already in one. His yspeed here is also assigned a value of 10 to start with.
Let’s say our character is standing at the bottom of the screen. For simplicity lets say our resolution is 640x480 and our character is standing on line 300.
With that established, lets look at the effect of the jumping state of the character when he goes through his update loop.
void HandleJump(void){ Player.y -= Player.yspeed; Player.yspeed--; if(/* collision on feet */) Player.jump = false;}
Our character is jumping as evidenced by Player.Jump, so we know to run the HandleJump function. Otherwise he would just sit there and do nothing.
So when we enter the HandleJump we immediately subtract the speed of the jump (Player.yspeed) from the Players position on the screen. This makes him move upward slightly.
This is where we finally simulate gravity. We subtract one from the yspeed here. So the next time around, the jump won’t carry the character as high. And after that even lower, until the Player.yspeed is a negative number being subtracted from our Player.y. And you don’t need to be a Astrophysics major to know that subtracting a negative number from a positive number makes the positive number bigger.
This will all continue until we detect our character back at ground level. Once he’s back on the ground we set Player.Jump to false and Player.yspeed to zero and our jump is done.
Hope this all made sense.