Jumping in a 2d game

Started by
39 comments, last by Thrust 19 years, 3 months ago
Quote:Original post by Thrust
*** Source Snippet Removed ***


Still doesent work. This is so frustrating. I think I might just go back to my other way and get rid of the struct part of it. But I still have no idea about how to make my character sucked towards the ground, jump, etc...


Well you need to make an instance of that struct (a variable). First you define what the struct contains and the name of the struct. Then you can create a new variable of that type.

Once again:
// you may need to do some typedef stuff here if you use C and not C++struct player { //blaa}player my_player; my_player.x = 5; 


Also it would be a good idea to have a function to update the state of the player (and enemies etc.). See Derakon's pseudo code example how to implement jumping..
Ad: Ancamnia
Advertisement
Ok the struct works now, I just dont know how to use velocity or gravity.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
You don't have to have a gravity variable or any type of physics.
simply make to functions, one that starts the jump, the other handles the jump.

here is two functions as an example.

The start function:

void Jump(void){    Player.jump = true;    Player.yspeed = 10;}


and the handle function

void HandleJump(void){    Player.y -= Player.yspeed;    Player.yspeed--;    if(/* collision on feet */)       Player.jump = false;}



Then, you call the jump start button when the jump key is pressed and you only call the handle jump function when the player is jumping.

gravity is not necessary because the yspeed will decrease until it is negative, therefore the player will go up, then fall till he lands.
-----------------------------Play Stompy's Revenge! Now!
His code would make complete sense, if I knew how to use velocity and gravity. I dont understand those two, if I did jumping would be alot easier.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Quote:
Ok the struct works now, I just dont know how to use velocity or gravity.


Velocity is the speed and direction of an object: The change in it's position over time. To update an objects position based on velocity, every time segment do posx+=velx and posy+=vely, but also remember to do collision detection. You can think of gravity as a velocity vector too, where velx=0 and vely is variable because gravity causes acceleration. Look up the formula for acceleration due to gravity and use that to compute a velocity vector for gravity at every time segment and add that to any other velocity vectors that are in place on your character, provided your character is not on the ground.

basic physics lesson [smile]

ok position is affected by velocity and velocity is affected by acceleration.

so your player has position x which is (for now) 0.
he has a jumping velocity of 5 meters per second (5 m/s)
and gravity is 9.8 meter per second squared (9.8 m/s^2)
but you knew all that [smile]

so the code is, for moving:
struct player { int y_pos; int y_vel;};Player pl;pl.y_pos = 0;pl.y_vel = -5  // because the y-coordinates on the screen are positive (increasing) going down the screen.if (key_up)   y_pos = y_pos + y_vel;

i'll stop there, in case you have questions

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

 

Quote:Original post by Thrust
His code would make complete sense, if I knew how to use velocity and gravity. I dont understand those two, if I did jumping would be alot easier.


I don't think you're ready for this yet.. You need to know the most basic physics (forces), which includes gravity, acceleration etc. When you know that, you will also know exactly what to do in this case. It's quite simple if you understand how forces work.
Killers don't end up in jailThey end up on a high-score!
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.
I have no questions so far. Please continue with the lesson. Thanks for the help...
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com

All you have to do us subtract a value or values from your y coord a number of times then add the same number to your y coord, once you've got the height you desire till he's back at the value he started.


If you really want to avoid physics just use a look up table with number of minus values (the thrust up) to add to your y, and then an equal number of positive values to add to the y (the effect of gravity).

ie
int table[] = {-3,-2,-1,0,0,1,2,3}; // an array of 8 elements
int index = 0

if (doingJump){

yCoord += table[index];

//breaking it down for simplicity,

index++;
if (index >=8) doingJump = false;
}


Use an index to access the relevent table offset, that you then increment each frame......(very) basic but effective, though your jump will always look the same. Make sure you reset index when you set doingJump, and don't trigger a jump if doing one already.

Can't think of a simpler way to do it till you learn a buit more about the action of fources like gravity on objects.




This topic is closed to new replies.

Advertisement