0
\$\begingroup\$

according to this picture, I want to move my yellow object, smoothly to the red one. And I want to have an eliptic trajectory in relation to my center point.

enter image description here

So for now I'm having this code:

yellowPoint.position = Vector3.SmoothDamp(yellowPoint.position, redPoint.position, ref currentVelocity, dampingMove);

How can I change it to add the elipse, depending on a third center point ? the itinial code can change of course.

Thanks you !

\$\endgroup\$
  • \$\begingroup\$ You may want to look at bezier curves instead. Could probably give you the result you want. Or do you want the movement to follow the curvature of the planet? In that case it's "just" moving along a section of a circle. \$\endgroup\$ – Morten Andersen Jan 23 '19 at 15:59
  • \$\begingroup\$ What defines the ellipse that you want to follow? Do you possibly want to rotate from one point to the other while smoothly increasing/decreasing the radius? If so, this curve may correspond to an Archimedean spiral rather than an ellipse. Or are you trying to simulate orbital mechanics for a trajectory around a gravity well, or a maneuver to change the orbit? If so, it would help to know how scientifically accurate you're aiming to be — whether you just want a simple C1 smooth curve or need to simulate real acceleration/deceleration effects on the orbit. \$\endgroup\$ – DMGregory Jan 23 '19 at 16:26
2
\$\begingroup\$

There are multiple ways to do that, depending on what kind of curve you want.

For example bezier curves:

enter image description here

The logic behind them is that, you take three points that define your curve. The first point is the beginning of the curve, and the last one is the end of the curve. The other is a "control point" meant to give the curve its shape.

To calculate this, you need to be familiar with a function like Lerp, which is used to find a point that lies between two other points. Following that, you end up with this:

// The points of the curve
Vector3 P0, P1, P2;

// time going from 0.0f to 1.0f
float t;

Vector3 pointInCurve =
    Vector3.Lerp(Vector3.Lerp(P0, P1, t), Vector3.Lerp(P1, P2, t), t);

The logic behind it is: Assuming you have a variable t that says at which position on the curve you currently are, find the point between P0 and P1, depending on t, so that in the beginning it's closer to t (so closer to P0) and as time passes, it goes closer to 1 (so closer to P1). Do the same for another point that goes from P1 to P2. These are the two green points illustrated on the gif.

Then do the exact same thing, for finding your desired point, which is now between the two green points. The result is the black dot on the gif, following the red curve/path.

You can follow the same logic to do quadratic bezier curves:

enter image description here

Just take 4 points instead of 3, calculate all the in-between points, until you end up with 3 new points, which then you can treat as a regular bezier curve and follow the steps above. In fact this way you can have as many points as you want.

There's also the Catmull-Rom curves:

enter image description here

They follow a similar logic, they have 4 points, but here the path travels from the second to the third point, using the first and last as control points. I've used this one before to simulate a "path" a flying creature is following, as when it reaches the end of that curve, you can create a new curve using points 2, 3 and 4, and generate one last point towards the direction that you want, so the path is smooth.

Unfortunately my familiarity with how these curves work behind the scenes is limited, but you can read more about them in Wikipedia - Centripetal Catmull–Rom spline.

Once you get the hang of how curves work, you can experiment with how fast t is updated to create effects like in easings.net.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.