Essentially I have a moving object and four defined points (P1 - P4). I need the moving object to to follow the dotted blue path from P1 to P4. It needs to start from P1 with a starting velocity, and end at P4 with a larger velocity, hence it needs to accelerate uniformly as it performs this complex curved motion. The variables I need to be able to define at run-time are:
The number of points
The locations of the points
The height (or shallowness) of the arcs (R1 - R3 values)
Starting velocity
Ending velocity
Thus I need a solution that is very customisable, allowing the number of swerves and their distance to be varied. I do not expect a full coded solution but if anyone has any advice or ideas that I can try, that would be really appreciated.
I have tried experimenting with Bezier curves, but they seem to require control points and manually adjusting the control points to create the curve. I cannot do this as I need these paths to be automatically calculated at run-time. The variables I need to control are listed above. I have also tried implementing this as three separate arc motions but getting that to work with a uniform acceleration across the entire path is something I have struggled with.
Any advice or pointers would be highly appreciated!
Thanks for your suggestion Edy, it is something I will look into. It looks quite complex mathematically though and there may be a simpler solution. The code below allows an object to move in an arc:
// Assuming point[0] and point[2] are your starting point and destination
// and all points are Vector3.
// point[1] is a new point in between with some height, to define the curve.
point[1] = point[0] +(point[2] -point[0])/2 +Vector3.up *5.0f;
// Play with 5.0 to change the curve.
// Then to compute the curve in a script for an object to travel to it, do this:
float count = 0.0f;
void Update() {
if (count < 1.0f) {
count += 1.0f *Time.deltaTime;
Vector3 m1 = Vector3.Lerp( point[0], point[1], count );
Vector3 m2 = Vector3.Lerp( point[1], point[2], count );
myObject.position = Vector3.Lerp(m1, m2, count);
}
}
Do you know how I might be able to tweak this to enable the speed to accelerate whilst it is lerping? If I can do this successfully, I should be able to join multiple arcs with discrete accelerations to create the overall path.
N.B. The points in the code do not relate to the figure in the original post.
Similarly I have another block of code that moves an object along a bezier curve within a specified time:
public IEnumerator Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float time)
{
float t = 0;
while (t < 1f)
{
t += Time.deltaTime / time;
float oneMinusT = 1f - t;
Vector3 pos =
oneMinusT * oneMinusT * p0 +
2f * oneMinusT * t * p1 +
t * t * p2;
mobileBody.position = pos;
yield return null;
}
}
This moves the object along with curve with uniform velocity. I am unable to get it to move along the curve with a uniform acceleration. Any help would be really appreciated.