# What is the best way to map the curve of a jump?

Ok so the question is simple. If you know the jump force, you know the gravity, and you know the velocity, can you map out the curve of a jump?

This is for a really simple patrolling/jumping AI critter… Very simply put, he walks back and forth, and uses a simple raycast to see if there is ground in front of him. If there isn’t, flip and reverse direction. SIMPLE and awesome! Weeeee!

Now the kicker, I want to make him jump from platform to platform. I’ve done this pretty easily by treating him like any other moving character. Setup a speed multiplier, assign a jump force (using Impulse, FYI), etc etc.

So I “map” out my possible jump like this:

If there is no ground in front of him (normally would just flip) I draw a ray up to what I calculate to be the Apex of the jump, then back down identically to the other side. If the second ray detects ground, JUMP (you have something to land on!). However, I am just using 2 straight lines, and it doesn’t seem to be too accurate.

I’m hoping someone has a reasonably simple way to graph out the jump, using Debug.linecast (or something visual in the scene view) so that based on the current velocity/gravity magnitude/jump force (again IMPULSE, I"m not even sure how Force works now that I’ve been trying impulse) you can see the curve of what the jump will be. Should be easy since we have basically all the variables, I’m just not sure of the formula. Here is what I have so far:

``````        //The gravity magnitude
float grav = Physics2D.gravity.magnitude;
//our current velocity. Since there is no resistance, character should be able to move through the AIR at the same rate as on the ground right?
//So airborn velocity shoudl be the same as grounded velocity
jumpCheckDistance = Mathf.Abs(m_Rigidbody2D.velocity.x);
//How high can he jump? Basic formula (I think) of: Force squared * (gravity *2)
float jumpHeight = jumpForce * jumpForce / (2 * grav);
//Now set the points. jumpCheck (Vector3) shoudl be the APEX of the jump...
//NOTE: currentSpeed is the velocity, we simply multipy it by -1 if < 0 since we will be going the OPPOSITE way (flips the direction of the raycast)
//Also NOTE: foot is an empty game object i place at the bottom of the character to measure from the ground (for grounding and what not).
jumpCheck = foot.transform.position + (new Vector3 ((jumpCheckDistance/2) * (currentSpeed > 0 ? 1 : -1), jumpHeight));
//jumpCheckGround is the position of where he shoudl land based on the jump. Basically an identical line as the last, drawn from the apex down... the second half of the "bell curve"...
jumpCheckGround = jumpCheck + new Vector3 ((jumpCheckDistance/2) * (currentSpeed > 0 ? 1 : -1), (jumpHeight) * -1);
``````

So this creates a fairly accurate representation of the jump, but it’s not totally accurate. Is there anyway to get this closer? Or rather, can anyone show me a formula to calculate the true apex of the jump, and the position of the landing (or really, I woudl like the whole curve, so I can tell where he will collide, etc)?

Let me know if this isn’t very clear, I’m a little rushed this morning. Thanks everyone!

If you have sufficient knowns (you do) you can discover your unknowns.

Have a look at the “big four” kinematic equations.

You’ll need to run the equation several times in a loop with different TIME values to build a prediction. The more iterations (with smaller time steps) the greater the fidelity of the prediction. Each discovered displacement will be a point in the parabola you are creating.

Your goal is the DISPLACEMENT, so methinks

d = (v’ * t) + (1/2)(a)(t^2)

is the formula you need. Your ACCELERATION is Physics.gravity. The INITIAL VELOCITY is going to be the velocity your object would have if he jumped during that frame. This can be worked out by knowing the direction and magnitude of the force vector you would apply and the object’s mass.

At each iteration, you may want to also know the FINAL VELOCITY, which would give you the direction of the ray you’d cast to check for a valid landing. There’s a formula for that there too. Note this step is somewhat optional. A raycast “down” should be sufficient, perhaps with a “suggestion” of the jump’s X direction for added insurance.

This approach will not help you discover any obstacles in the path; for that, you’d have to ray- or sphere-cast along each segment of the parabola. For anything more sophisticated like prediction of reactions to obstacles, you’d need a recursive function that tests for obstacles with each iteration.