I'm developing a 2d fighting game, using time steps based on frames ie, each call to Update() of the game represents a frame so, no variable time at all.
My jump physics code doesn't need to consider delta time, since each Update() corresponds exactly to the next frame step. Here how it looks:
double gravity = 0.78;
double initialImpulse = -17.5;
Vector2 actualPosition;
double actualVerticalVelocity;
InitializeJump() {
actualVerticalVelocity = initialImpulse;
}
Update() {
actualPosition.Y += actualVerticalVelocity;
actualVerticalVelocity += gravity;
}
It works great and results in a smooth arc. However, it's hard to determine the values for "gravity" and "initialImpulse" that will generate the jump that I want.
Do you know if it's possible to calculate the "gravity" and "initialImpulse", if the only known variable is the "desired time to reach max height" (in frames) and "desired max height"?
This question should lead to the answer I'm looking for, but its current answer does not fit my needs.
UPDATE:
As @MickLH figured me out, I was using an inefficient Euler integration. Here is the correct Update() code:
Update() {
actualVerticalVelocity += gravity / 2;
actualPosition.Y += actualVerticalVelocity;
actualVerticalVelocity += gravity / 2;
}
Only the very first step of the Update() will change and will move the object by initialVelocity plus the half of the gravity, instead of the full gravity. Each step after the first will add the full gravity to the current velocity.
Answer
Short answer:
gravity = -2*desiredMaxHeight/(desiredTimeInTicks*desiredTimeInTicks)
impulse = 2*desiredMaxHeight/desiredTimeInTicks
Long answer: (pre-calculus required)
Define your jump function which is a simple integral over time:
Now to find the peak of this function we solve for where the derivative equals zero:
- simplified:
We have built a system of equations which:
- represent the relationship of t (time), h (height), impulse, and gravity
- constrain the height to its global maxima
The solution to this system yields the the variables you are interested in:
No comments:
Post a Comment