|
Making Character Jump / Gravity |
PricklyPoo
Member #10,913
April 2009
|
I've searched the forums and didn't find anything that really answered my question to where I could understand... The code I'm using is just a sample off of some vague tutorial kinda thing, but I can't really get it to work. I've been thinking about this, and really can't think of a good way to make my character jump. This is what I have so far. int character_velocity_y = 0; if (character_jumping == FALSE) if (character_jumping == TRUE) The problem is, the character doesn't go back down, it isn't a smooth jump, the character basically just appears up higher....And ya, it's really just a big mess, if someone could give me some tips I'd really appreciate it. You can download what I have so far here. |
Neil Black
Member #7,867
October 2006
|
The biggest problem that I see is the difference between character_velocity_y and character_accel. The first frame of the jump, your character goes up by 40. The second frame he goes up 39, the third frame he goes up 38, and so on. He'll go up by about 820 before he even begins to come back down, because you are only decreasing the character_velocity_y by one each frame, and it is so big to start with. If you set character_velocity_y to 8, and character_accel to 1, you'll get a jump height of around 36. try playing with the numbers a bit to get them just right for your game. But 40 is way too high for character_velocity_y. If you change the position by that many pixels a frame, it will look like your character is teleporting. Even 8 pixels can cause this problem, though if your character's dimensions are 32x32 it won't look too terribly bad. EDIT: Oh, yeah, and you stop the character when character_velocity_y is 0, which means that at the top of his jump he'll just stop, and won't fall back down.
|
PricklyPoo
Member #10,913
April 2009
|
Alright, I tried what you said and it still doesn't work. Honestly I think I'm doing it completely wrong, so if anyone could post some sample code I'd appreciate it. This is making me so frustrated, been messing with this code for hours.??? |
BlackShark
Member #9,796
May 2008
|
for the initial jump apply constant force aka (gravity) every frame 1y_vel += .15;
1if((y_vel < 0) && (block_collision(Up))) { //Hit his head on a roof
2 y_vel = 0;
3
4 }else if((y_vel > 0) && (block_collision(Down))) { //Landed on the floor
5
6
7 if(state == jump){state = ground;} //return state to on the floor
8 if(y_vel > 14) { kill(d_gibbed); } //kill the player if he's comming down to fast (you can get rid of this)
9
10 //apply a bit of friction if you touch the ground,
11 //this is to prevent bunny hopping
12 if(x_vel >= .4) {x_vel -= .4;}
13 else if(x_vel <= -.4) {x_vel += .4;}
14 else{x_vel = 0;}
15
16 y_vel = 0; //stop falling
17 }
Hope this helps -BlackShark |
PricklyPoo
Member #10,913
April 2009
|
Heh, thanks for the help, I twisted that code to try and make it work, then I realized I would have to rewrite everything with floats, etc...But I kinda tried something different and it still doesn't work...:-X??? int y_vel = -6; if (jumping == TRUE) //if character is jumping, add 1 to velocity Well anyway, getting tired of this, nothing is working. Here is what I have so far, just as bad as before. |
someone972
Member #7,719
August 2006
|
There are quite a few things wrong in there. First of all you have your timer set to 1000 beats per second. This should be more like 60. Once you change that to an acceptable value you also need to make your character go faster. It would be wise to change the velocity and y_position to floating point numbers so you can move less than a pixel. If you want a completely fixed program I or one of the other allegators can do it for you, which you can then look at. (There's some more problems too...) ______________________________________ |
Alianix
Member #10,518
December 2008
|
i think the worst is that your formula is wrong. your y coordinate is positive down wards because of the way graphics is drawn, u know this. So your speed is positive downwards too. Your acceleratiion is towards the ground, which is in this case will also be positive. So your formula should be: v_y = v_y + a*t; v_x = v_x; you can tweak t to be = 1 if you don't wanna use floating point, and play with 'a' to get the right gravity. if t=1: The way to work these is to set 'v' and 'a' to a large value then divide your coordinates by some large constant, if you don't wanna work with floating point coordinates.
|
|