Skip to content

Instantly share code, notes, and snippets.

@jselstad
Created June 7, 2018 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jselstad/1b25477d2a59305bb43d073f66878b74 to your computer and use it in GitHub Desktop.
Save jselstad/1b25477d2a59305bb43d073f66878b74 to your computer and use it in GitHub Desktop.
Kinematic Interpretation of a Cubic Bezier Curve - For Unity
// This function evaluates points along a bezier curve given by a starting position and velocity, and an ending position and velocity
// This can be thought of as interpolating between two linear trajectories
public static Vector3 cubicBezier(Vector3 aStart, Vector3 aVel, Vector3 bEnd, Vector3 bVel, float duration, float time) {
float alpha = time / duration; //Normalize the time
Vector3 aEnd = aStart + (aVel * duration); //Where it would end up if it were just A's dynamics
Vector3 bStart = bEnd - (bVel * duration); //Where it would start if it were just B's dynamics
return
(Mathf.Pow(1 - alpha, 3f) * aStart) +
(3f * Mathf.Pow(1 - alpha, 2f) * alpha * aEnd) +
((3f * (1f - alpha) * alpha * alpha) * bStart) +
(alpha * alpha * alpha * bEnd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment