Skip to content

Instantly share code, notes, and snippets.

@zensharp
Created January 14, 2021 04:23
Show Gist options
  • Save zensharp/6adf7d2851c02e239e1f3c8863ddab52 to your computer and use it in GitHub Desktop.
Save zensharp/6adf7d2851c02e239e1f3c8863ddab52 to your computer and use it in GitHub Desktop.
public bool TryGetSecant(Vector3 position, Vector3 velocity, Vector3 acceleration, float arcDistance, out Vector3 secant) {
if (acceleration == Vector3.zero) {
secant = velocity;
return false;
}
var axis = Vector3.Cross(velocity, acceleration);
if (axis == Vector3.zero) {
secant = velocity;
return false;
}
var radius = Mathf.Pow(velocity.magnitude, 3.0F) / axis.magnitude;
var inwards = Vector3.Cross(axis, velocity).normalized;
var radiusVector = -inwards * radius;
var center = position + -radiusVector;
var angle = arcDistance / radius;
var rotation = Quaternion.AngleAxis(angle * Mathf.Rad2Deg, axis);
var endpoint = center + rotation * radiusVector;
secant = position - endpoint;
return !(secant == Vector3.zero);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment