Jumping in a 2d game

Started by
39 comments, last by Thrust 19 years, 3 months ago
Hi,

Physics is just a little math. Small physics class:

a. Objects are located in space so they have a position x,y.

b. Objects have a movement speed. The speed is a measure of how many units the object moves in a given time. So if a car is moving at 30km/h then the car moves 30km in one hour. If you consider that a kilometer is 1000 meters then athe car is moving at 30000m/h. Also if you consider an hour has 3600 seconds then you can say the car is moving at 8.33m/s. Now, in your game a meter may be represented by 10 pixels (or 5 or 100, you choose the scale). Then you may say your car moves 83.3 pixels/sec.

c. Basically that example means that you can define speed as:
speed = distance_moved / time_used_to_move_that_distance

or, with a simple multiplication:
distance_moved = speed * time_used_to_move_that_distance

or simply put:
v = d / t or
d = v * t

So this little formula will help you getting the third value if you have two of the parameters. This is the basic for physics.

Still with me?

c. Example: Your game takes about N milliseconds for each frame. So you can use the formula and know that in the previous example, your car moved at 83.3pixels/sec. N msecs means that each frame takes N/1000 seconds. So you know that you car moves:
d = v * t
d = 83.3pix/sec * N/1000 seconds
d = 83.3 * N / 1000 pixels.

Where N is the milliseconds from frame to frame. N may vary in each computer, its not a fixed value. It should be computed each frame.

Ok, now i need to know if you got it. Try to program this concept in your game. Then write another post and I'll try to help you with acceleration.

Luck!
Guimo


Advertisement
//jumpsvoid Jump(void){    p1.jump = true;    p1.y_vel = 96;}void HandleJump(void){Player.y -= Player.yspeed;Player.yspeed--;if(map[(((p1.y_pos+2)+spriteh)/32)][(p1.x_pos/32)] == 1 && map[(((p1.y_pos+2)+40)/32)][((p1.x_pos+spritew)/32))Player.jump = false;}



 if ( keys[SDLK_UP] )       {              Jump();       }



Now where would I implement HandleJump?
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
During the game loop, simply put:

if(Player.jump)   HandleJump();
-----------------------------Play Stompy's Revenge! Now!
Here's a previous thread that does a pretty good job of explaining this, IMNSHO.
You know what, on second thought, maybe you aren't quite ready for this...

I would recommend going back and working on structure of programming before getting into all this.

[Edited by - JustHeath on January 6, 2005 4:21:50 PM]
Yes!!! It works. The only problem is when im coming down I go through the floor to much some times. Wow that sucks. Now to be stuck again.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Check to see if you're past the floor before you draw. If you are, then set your height to the appropriate magnitude before you display the frame.
You need to write a routine that pushes the player up when the player has sunk into the floor. This routine will activate when the player is not jumping. This way, when you land, the routine will push your character up to the surface, and the player will not know the difference. Here is an example of a function to do this.

void PushUp(void){    while(/* collision at pixel below feet. */)        Player.y--;}


then you can implement this function in the gameloop like this.

if(Player.jump)   HandleJump();else if(!Player.jump)   PushUp();


Try it, it most likely will work. I've used this method and it worked.





-----------------------------Play Stompy's Revenge! Now!
sidenote (not aimed at Thrust but for people particpating in the thread):

when jumping shouldn't check collision with the top of the character
and falling with the bottom of the character? so that code reflects this concretely?
if (falling) {  player_bottom += 10; // player_bottom is really player_xpos + 32}// and if (jumping) {  player_xpos -= 10;  // starting at top-left of character sprite; assume it's a rectangle}

right?

Beginner in Game Development?  Read here. And read here.

 

Top collision during jumping is optional, but yeah, I see what your saying.
-----------------------------Play Stompy's Revenge! Now!

This topic is closed to new replies.

Advertisement