Advertisement

Best way to caclulate a jump in 3D?

Started by October 28, 2002 07:10 PM
5 comments, last by DevLiquidKnight 22 years, 3 months ago
I was wondering what is the best algorithim or even basic mathmatical procedure to caculate when a user jumps how high they jump and when they will fall back down mainly to calculate the speed in a 3 dimensional world? Anyone know? Don''t click me! Killer Eagle Software
use physics basic kinetics equation:

s(t) = 1/2 * a * t^2 + v * t + s;

s - last position
v - last velocity
a - last acceleration
t - time since last calculation
s(t) - position at time t

s(t) = new postion

run that equation for each of the 3 coordinate directions (x,y,z) to get your new x,y,z coordinate.

you''ll need to store an acceleraion, velocity and position vector for your object. each of those vectors holds an x,y,z value corresponding to the respective coordinate direction of the property.

no makey sense? google around for basic physics tutorials or check out the articles & resources section of this site and troll the physics section

-me
Advertisement
So as not to drive yourself nuts in the future you should integrate basic Newtonian physics into your game engine. If you''ve taken a physics course in high school it will be easier for you to implement one. Basically, each of your objects should to have mass and velocity as part of its state. Velocity is the change in your object''s position in every frame. And the mass can be though of as your object''s resistence forces being exerted on it.

In order to make any use of velocity, however, you must also keep of accumulated force which is then used to calculate your object''s acceleration. Acceleration is the rate at which your object''s velocity changes. You can think of a car accelerating. It''s speed doesn''t just suddenly become 60, it has to accelerate to that speed at a given rate. The power from the engine is transfered to the cars wheels, which makes them spin. The resulting friction created by the contact of the tires with the asphalt create a FORCE that pushes the car. In order to get your acceleration you must take the force and divide it by your object''s mass. Ever notice how heavier things are harder to get moving? This is the mathematical equivalent. You''ll probably see it around as:

F = ma
FORCE = mass * acceleration

OR

a = F/m
acceleration = FORCE / mass


class GameObject {  private:   Vector3 position;   Vector3 velocity;   Vector3 accumForce;   float   mass;  public:   void setPosition(float x, float y, float z);   void setVelocity(float x, float y, float z);   void exertForce(float x, float y, float z) {      accumForce.x += x;      accumForce.y += y;      accumForce.z += z;      }   /**   * Call in every frame.   */   void update() {      Vector3 accel;            // make a copy of the accumulated force in accel      accel.copy(accumForce);      // a = F/m      accel.scale(1.0f / mass);      // modify velocity by ac      velocity.add(accel);      position.add(velocity);            // reset accumulated force to zero.      accumForce.set(0, 0, 0);      }   }; // ends class GameObject


Now... to create gravity you must exert a force on all of your objects equal to the object''s mass time some constant. This is because oddly, gravity is a constant acceleration for everything. In order to make someone jump, first test to see if they are on the ground, then call exert force on their object with a high force value in the up direction (usually positive Y axis). Since you''ve got a physics simulation going, the jump does itself.

void inputEvent(int key) {   if(key == KEY_JUMP) {      if(objectList[OBJ_PLAYER].isOnGround())         objectList[OBJ_PLAYER].extertForce(0, JUMP_FORCE, 0);      }    else if(key == KEY_SHOOT) ...    else if(key == KEY_CROUCH)   }void WorldUpdate() {   for(int i = 0; i < objectCount; i++) {      // apply gravity.      objectList.exertForce(objectList.getMass() * GRAVITY);<br>      objectList.update();<br><br>      }<br><br>   }<br></pre><br><br>I must admit, things will get a bit complicated when you throw collision detection and resolution into the mix, but it will give you a much more robust game engine.<br><br>Hope this helps a bit.<br>  </i>   
besides the physics behind the jump a _very_ big component is how the character animates during this time and how the camera behaves while the player is in the air. slight changes to any will have major impacts on how the character feels while jumping.

we (myself, our lead designer, lead art director, and our character animator) spent a solid month tuning the camera, animation, as well as some physics hacks to get the jump feeling the way we wanted.
Perhaps the easiest and simplist way is to animate the entire thing - ie translate the character in your modelor. That way you always know how far they will jump.

But i hate simple...if you have time spend it on a decent physics engine.
There''s an art to getting character motions that look right, such as for Tomb Raider. You can play around with spring and viscosity equations and metaball systems and linear solvers all you want, but you really have to trust your instincts if you''re gonna get Lara Croft''s breasts to bounce right.


Don''t listen to me. I''ve had too much coffee.
Advertisement
Our physics only describe things that happen in the real world. That''s why they can''t describe Lara Croft''s breasts!

This topic is closed to new replies.

Advertisement