- Home /
Interpolated Projectile in vertical arc and horizontal (blendable)
My goal is to interpolate a projectile position through 2 arc (one vertical and 2 other horizontal) being able to blend them together to give particular angle of approach. But i dont understand how to integrate the horizontal axis since the arc is in both the x and z axis depending of the target position. I try with Bezier but i was stuck with inconsistent speed.
private Vector3 CalculateNextPosition()
{
//Y Arc
Vector3 yZeroStartPos = new Vector3(startPosition.x, 0, startPosition.z);
Vector3 yZeroDestination = new Vector3(destination.x, 0, destination.z);
//Move straight forward
Vector3 nextPosStraigth = Vector3.MoveTowards(new Vector3(transform.position.x, 0,
transform.position.z), yZeroDestination, speed * Time.deltaTime);
float dist = Vector3.Distance(yZeroStartPos, yZeroDestination);
//Add in the arcs
float t = (nextPosStraigth - yZeroStartPos).magnitude / dist;
float baseY = Mathf.Lerp(startPosition.y, destination.y, t);
float arcY = arcYAxisHeight * (nextPosStraigth - yZeroStartPos).magnitude *
(nextPosStraigth - yZeroDestination).magnitude / (0.25f * dist * dist);
return nextPosStraigth + new Vector3(0, baseY + arcY, 0);
}
Answer by Eno-Khaon · Oct 09, 2020 at 01:00 AM
Your singular X-axis in a typical trajectory calculation is the combination of both the X and Z axes into a single value.
Vector3 flattenedAxes = new Vector3(xDistance, 0f, zDistance);
float combinedAxis = flattenedAxes.magnitude;
I took things a bit further with trajectory calculations in an older answer. It's focused around Rigidbody physics calculations specifically (and leaves the rest of the process up to that), though I also included (late in a comment chain) a basic trajectory prediction scheme in this other answer.
Thanks for the answer! I'm suppose to do the Y arc like i did and the horizontal separately to addition them to the final vector? I have difficulty to see how i can apply the arc on the "good" x or z axis.
(I converted your answer to a comment for you. Keep in $$anonymous$$d to "Add comment" to an answer, rather than using the prompt to add a new answer in this situation)
After you've made your calculations with the horizontal axis separated out, you can recombine it into the X/Z axes by multiplying by their normalized value:
// Simplified a bit
// First, the X/Z values from the to-from vector
Vector3 horizontalAxis = new Vector3(xDistance, 0f, zDistance);
// Normalize for a length of 1 between both axes
Vector3 rebuiltAxis = horizontalAxis.normalized;
// Extend to intended length of horizontal axis (float)
// in trajectory calculation
rebuiltAxis *= horizontalLength;
// Re-insert Y-axis
rebuiltAxis.y = yLength;
Ok so i redo the same thing with the x and y and multiplied the result with the rebuiltAxis ? like (BaseX + ArcX) * RebuildAxis.x
Ahh, whoops, looks like I misread your original question a bit. Depending on your intent for a "horizontal" arc in your trajectory, it should effectively be as straightforward as changing the direction of gravity in your calculations. What that would mean, however, is that you can't depend solely on replacing a single axis at a time.
Using my first link in my answer as an example, you would want to separate out the horizontal and vertical axes based on your intended "Gravity" (e.g. where the baseline gravity is Vector3(0, -9.81, 0)
you would factor in wind (or a similar element) to that vector, then apply that modification as a velocity change to the object in flight). From there, that example would aim to hit the target based on an applied "gravity" (which, in this case would be gravity + wind/etc.).
Then, using my second link as an example, you would iterate from the starting position and velocity, using the (gravity + wind/etc.) vector based on how many time steps forward you're looking to go.
That doesn't mean you have to iterate through in that manner, however. What it does provide is an accurate recreation of the path it would take using PhysX in Unity. If you want the close-enough outcome based on real-life predictive physics, however, you can still use the separated vertical/horizontal axes (since the final result in this case would still be a 2-D arc). Your position at any given time would look about like this:
float h; // The current horizontal position (linear)
float v; // The current vertical position (parabolic)
Vector3 hA; // the horizontal "Axis" perpendicular to "Gravity"
Vector3 vA; // the vertical "Axis" (normalized, inverted "Gravity")
Vector3 currentPosition = hA * h + vA * v;
No problem thanks for your help, it's me that was to vague. I reformulate my initial question. $$anonymous$$y goal is to interpolate a projectile position through 2 arc (one vertical and 2 other horizontal) being able to blend them together to give particular angle of approach.
Am able to do the interpolation of the individual arc but im stuck with the horizontal one (XZ) since the interpolation is and world position and not relative to the forward of the "shooter".
I would like to arc on the z axis relative to the shooter (perpendicular)
I guess at this point, it kinda boils down to what you're looking for in the horizontal axis.
If your goal is a fixed influence on velocity (such as wind), then the trajectory is still 2-Dimensional, but the axes would need to be clearly defined. The solution for that is what I've been describing so far, but if you need any more information, just let me know.
On the other hand, if your goal is a more free-form approach (such as using Bezier curves), then you may want to look at some information on traversing a Bezier curve at a fixed speed. This approach would result in the vertical axis of the trajectory being back to Y-axis only (as a general rule), and the horizontal axis would instead become the current position along a 2-D path (Where the position along the curve is, itself, a 1-Dimensional parameter).