Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Making Character Jump / Gravity

This thread is locked; no one can reply to it. rss feed Print
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;
int character_accel = 1;
int character_jumping = FALSE;

if (character_jumping == FALSE)
{

if(key[KEY_UP])
{
character_velocity_y = 40;
character_jumping = TRUE;

}

if (character_jumping == TRUE)
{
character_velocity_y = character_velocity_y - character_accel;
character_y_position = character_y_position - character_velocity_y;
}
if (collision == TRUE) /* Check to see if character has hit floor */
{
character_velocity_y = 0;
character_jumping = FALSE;
}
}

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. ;D

You can download what I have so far here.

http://h4x.hosted.nfoservers.com/Gravity.rar

Neil Black
Member #7,867
October 2006
avatar

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

#SelectExpand
1if(key[KEY_UP]) 2{ 3 if(state == ground) { 4 state = jump; 5 y_vel = -5; 6 } 7}//KEY UP

apply constant force aka (gravity) every frame

#SelectExpand
1y_vel += .15;

#SelectExpand
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;
int jumping = FALSE;

//loop
if(key[KEY_UP])
{
if(character_y_position == 620) { //jump if character is on ground
jumping = TRUE;
}
}//KEY UP

if (jumping == TRUE) //if character is jumping, add 1 to velocity
{
y_vel += 1;
character_y_position = character_y_position + y_vel;
if(character_y_position >= 620)
{
jumping = FALSE;
}
}
//loop end

Well anyway, getting tired of this, nothing is working. :-[

Here is what I have so far, just as bad as before. :P

http://h4x.hosted.nfoservers.com/Gravity.rar

someone972
Member #7,719
August 2006
avatar

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...)

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Alianix
Member #10,518
December 2008
avatar

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;
y = y + v_y*t + .5 *a*t^2;

v_x = v_x;
x = x + v_x*t;

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:
v_y = v_y + a;
y = y + v_y+ .5 *a;

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.

Go to: