Sign in to follow this  

Smooth jumping... Not what you would think...

This topic is 4691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have currently tried rewritig my jumping in my 2d plat-fighter so that it uses one function instead of about three. Anyways here it is:
void Jump()
{
    p1.y_vel = 50;
    p1.y_pos = p1.y_pos-p1.y_vel;
    p1.y_vel--;
}

What I cant figure out is how to give it a smoth jump up instead of a straight jump up 50 pixels. I am not to good at math so I think its pretty good I knew how to do that period. Anyways is there different math equasions I can use to make the character move up by say 2 pixels at a time, until it reaches a peak then stops? I have a gravity function running every frame so I dont need to worry about coming down.

Share this post


Link to post
Share on other sites
The code seems about right (i.e. physically correct) except that you shouldn't reset the velocity each frame of the animation.
void BeginJump() {
y_vel = -FORCE;
}

void UpdateJump() {
y_pos += y_vel;
y_vel += GRAVITY;
}
Also, to avoid jumping 50 pixels in one frame you obviously need to use a smaller initial force. Possibly even floating point or fixed point numbers.

Share this post


Link to post
Share on other sites
Your gravity function should be all you need to get it to stop
When you jump just set the velocity to 50 like you are doing, and then add the velocity to the position each frame...
If your gravity function decreases the velocity then this should get the effect you want

Share this post


Link to post
Share on other sites
Thank you all for your help. It now functions good. Im going to have to work on my gravity some other time to make that look smoother (possiably just changed jump to work backwords :) ) but other than that im done. Here check out the new code:


//Simulates a jump
void Startjump()
{
p1.y_vel = 15;
p1.jump = true;
p1.inair = false;
}

void Jump()
{
p1.y_pos -= p1.y_vel;
p1.y_vel--;
if (p1.y_vel == 0)
{
p1.jump = false;
p1.inair = true;
}
}
//Simulates Gravity
void Gravity()
{
if(map[(((p1.y_pos+spriteh)+1)/32)][(p1.x_pos/32)] != 0)
p1.y_pos+=1;
if(map[(((p1.y_pos+spriteh)+1)/32)][(p1.x_pos/32)] != 0)
p1.y_pos+=1;
if(map[(((p1.y_pos+spriteh)+1)/32)][(p1.x_pos/32)] != 0)
p1.y_pos+=1;
if(map[(((p1.y_pos+spriteh)+1)/32)][(p1.x_pos/32)] != 0)
p1.y_pos+=1;
}



Thanks for all of your help, now feed me the good ratings :) j/k (please? ::blushes::)

Share this post


Link to post
Share on other sites

This topic is 4691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this