Comments and answers for "What is the best way to map the curve of a jump?"
http://answers.unity.com/questions/950561/what-is-the-best-way-to-map-the-curve-of-a-jump.html
The latest comments and answers for the question "What is the best way to map the curve of a jump?"Comment by AlwaysSunny on AlwaysSunny's answer
http://answers.unity.com/comments/951254/view.html
Very glad to be of assistance. Classical Physics was the closest thing to "fun" I took in college, despite the homework taking 8-12 hours a week. Fascinating stuff, and definitely useful in game design.Mon, 20 Apr 2015 23:42:38 GMTAlwaysSunnyComment by acorrow on acorrow's answer
http://answers.unity.com/comments/951238/view.html
Here's my final code for anyone interested. It was at one point WAY to big of an estimation, but now, this works FLAWLESSLY. I never have gotten anywhere near this accurate. I wish I could upvote your answer 100 times..
A few things to note here: My characters speed will be variable, so it was important to to have this scale on command. My gravity scale is much higher as well. Also, the original snippet you provided was for 3D physics. I had to switch this over and use vector2's and got it working. For anyone that looks at this example, I tried to comment this the best I could while rushing out the door. Thank you again!
The quick summary of this is: Input your initialVelocity, and the jumpForce, and estimate (assuming no drag) a perfect jump arc. Enjoy!
void Update()
{
Vector2 initialVelocity;
Vector2 jumpForceVector = new Vector2(0, jumpForce);
initialVelocity = m_Rigidbody2D.velocity + jumpForceVector.normalized * (jumpForceVector.magnitude / m_Rigidbody2D.mass);
//This "freezes" the line so that you can see the character move relative to the prediction
if (!m_Jumping)
JumpPath = Trajectory(50, 0.1f, initialVelocity);
for (int i = 0; i < JumpPath.Length - 1; i++)
{
//Draw every other line to give a dotted line effect, cause that's cool...
if (i % 2 == 0)
Debug.DrawLine(JumpPath[i], JumpPath[i + 1], Color.red);
}
}
Vector2[] Trajectory(int steps, float timeStep, Vector3 initial)
{
Vector2[] traj = new Vector2[steps];
for (int i = 0; i < steps; i++)
{
float t = timeStep * i;
traj[i] = initial * t;
traj[i] += 0.5f * Physics2D.gravity * (t * t);
traj[i] += new Vector2(transform.position.x, transform.position.y);
}
return traj;
}Mon, 20 Apr 2015 22:40:26 GMTacorrowComment by AlwaysSunny on AlwaysSunny's answer
http://answers.unity.com/comments/951235/view.html
The "big four" kinematic equations are used to predict the future position or velocity of an object experiencing a constant acceleration.
Time in the equation is the number of seconds in the future you are predicting. So you run the equation many times to ask, what would the displacement be after (t) seconds. After (t*2) seconds, after (t*3) seconds, etc. This builds a trajectory prediction.
In this formula, the "time" variable has nothing to do with Unity's time, deltaTime, time settings, or any of that. It's just saying "where would the object be after TIME seconds".
In a much simpler scenario, if you did not want to check for obstacles or draw the parabola, it would be sufficient to run the equation once with a big "time" value, to predict the "final" position.
So how many times you should run the equation (steps), and the (timestep), should be found experimentally based on how far into the future you want to predict. I would think 10-25 steps would be reasonable, and the timestep should be something like 0.2 or 0.3 seconds.Mon, 20 Apr 2015 22:25:09 GMTAlwaysSunnyComment by acorrow on acorrow's answer
http://answers.unity.com/comments/951014/view.html
This is awesome. I'm starting to get the feel for what i need to do. One last question. Sorry to be so daft here today, but what would steps and timestep be? Are we refering to the TimeStep in the Time settings for the project? Again, sorry to be such a pest on this one, but you definately have given me a GREAT deal of help so far! thanks again!Mon, 20 Apr 2015 16:14:52 GMTacorrowComment by AlwaysSunny on AlwaysSunny's answer
http://answers.unity.com/comments/950986/view.html
No problem, it's not often I get to stretch these mental muscles. Which also means I'm pretty rusty with this stuff, so this is a bit of guesswork:
// getting the initial velocity
initialVelocity = body.velocity + jumpForceVector.normalized * (jumpForceVector.magnitude / body.mass);
// the loop, returns array of sequential points
// representing a parabola of the projected trajectory
Vector3[] Trajectory(int steps, float timeStep, Vector3 initial) {
Vector3[] traj = new Vector3[steps];
for (int i = 0; i < steps; i++) {
float t = timeStep*i;
traj[i] = initial * t;
traj[i] += 0.5f * Physics.gravity * (t*t);
}
return traj;
}
If I've made a mistake here, hopefully you'll spot it. This should get you in the ballpark, anyway. You could optionally rewrite this to include the raycast check for a valid landing spot. This way though, it's re-usable for other situations.
Just loop over the returned array, raycasting between the current and next point (optionally drawing these rays with Debug.DrawLine() so you can visualize the trajectory yourself). If you hit something, it's either an obstacle or a valid landing zone. The normal of the first detected hit, if any, should give you a good idea of which is the case. No hit either means the steps are too few, the timestep is too small, or the jump may be into the unknown abyss...
Hope it's helpful, :)Mon, 20 Apr 2015 15:39:30 GMTAlwaysSunnyComment by acorrow on acorrow's answer
http://answers.unity.com/comments/950865/view.html
This is great, I'll get to work on this in a few. However, with TIME, would that be delta time, or a representation of the total time of the jump?
Any chance I could get you to write out a quick loop that would help explain how to best calculate this and display it in Scene view?
I really appreciate your help so far! Thank you againMon, 20 Apr 2015 13:29:12 GMTacorrowAnswer by AlwaysSunny
http://answers.unity.com/answers/950804/view.html
If you have sufficient knowns (you do) you can discover your unknowns.
Have a look at [the "big four" kinematic equations.][1]
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.
[1]: http://www.physicsclassroom.com/class/1DKin/Lesson-6/Kinematic-EquationsMon, 20 Apr 2015 11:43:27 GMTAlwaysSunny