How to model a jump?

Started by
9 comments, last by ridefast42 17 years, 8 months ago
Let me start by saying that I'm not great in math, alas. I'm working on it but it means that every now and then I'm stuck with something. Something like jumping. I created a camera class that can both be used for a flight simulator as for an FPS and the last addition I want to make to its FPS part is to let the camera 'jump'. I thought it to make sense to use sinus for a jump as you can easily alter the height of the jump by altering the amplitude A. Thus, the function I'm looking at would simply be: A*sin(x). I'd then let the program check when the position of the camera was back to where it was before. So far so good. However, I'm a bit stuck with x. How would I implement that function in a program? After all, x has to be variable. A few seconds I thought of taking the forward speed for x but that doesn't make sense as it's constant and will thus always yield the same jump-height. How do I get this jump going?
Advertisement
Accumulated forward distance, or elapsed time (equivalently, since velocity is constant).
The simpliest thing would be to have x depend on time coming into the system.

Assuming that you have recieving the time for each frame in a variable called deltaTime.

old_x = new_x + deltaTime / timeForTheJump;

Then set A according to how fast you are moving.
Or, according to physics, you would determine the velocity with which the character is jumping, and then constantly accelerate downwards:

<code>
//Declarations in class or global context, depending on language
//and implementation.
double x,y;
double vx,vy;
double gravity=-9.8;

//Returns the height of the ground at a specific point.
double getGroundHeight(x,y);

void updateJump() {
if(y>getGroundHeight(x,y)) {
y+=vy;
vy+=gravity;
} else {
vy=0;
}
}
</code>

This implementation would let you have a ground of varying heights, and you can change the tests in the updateJump() method/function to cause damage in a fall or kill the player when s/he falls down a bottomless pit and whatnot.

EDIT: Could someone tell me how to use the source code feature of this site?
Thanks all of you for your answers. I went for the sinus approach to start with as I'm only jumping on flat terrain, but when I get to do some terrain rendering I suppose I better had the physics-way-of-doing-things implemented because I wouldn't want to end up floating in mid-air after jumping off a mountain :)
Just note that you could easily never 'get back to zero' due to floating point imprecision, so you might want to test whether it is actually less than or equal to 0, and make it equal to 0 if it is.
Using a sine function for a jump, even on a flat terrain, is not physically correct. The path of the camera should be a parabola (neglecting air resistance).
.
Quote:Original post by Mastaba
Using a sine function for a jump, even on a flat terrain, is not physically correct. The path of the camera should be a parabola (neglecting air resistance).


Naturally. (I think the rest of us were assuming that "jump" was an imprecise word choice.)
But when standing still, would it really matter that much whether it was a sine or a parabola? After all, it's only about going up and down again (from a camera's point of view).
Only when there was a forward speed this could make a difference, I think. But when I'm really going to have to make a realistic jump I think I'd have to go with the physics model anyway. As in, let gravity take part in things...
Yes, even standing still it makes a big difference, because the vertical acceleration of the camera would not be constant, which would be the case with a camera in projectile motion.
.

This topic is closed to new replies.

Advertisement