So I am looking to get the angle of an AnimationCurve at a specified evaluation time. I know from my calculus classes the fancy mathematical way is to take the derivative of the the function of the curve. But I don’t know the function of the AnimationCurve… or is there a way to do it? Can I somehow take the derivative on an AnimationCurve and then use that to figure out the tange angle at specified points? Or do I just need to evaluate two points on the curve and do some trig?
Can you do a basic Vector2.Angle? It’s very arbitrary, but may fit your needs.
var p1 = curve.Evaluate(myTime);
var p2 = curve.Evaluate(myTime - 0.01);
var angle = Vector2.Angle(new Vector2(p1, p1), new Vector2(p2, p2));
Not rigorously tested yet, but here’s my attempt on the actual derivative:
public static float Derivative(this AnimationCurve self, float time)
{
if (self == null) return 0.0f;
for (int i = 0;i < self.length - 1;i++)
{
if (time < self[i].time) continue;
if (time > self[i + 1].time) continue;
return Derivative(self[i], self[i + 1], (time - self[i].time) / (self[i + 1].time - self[i].time));
}
return 0.0f;
}
private static float Derivative(Keyframe from, Keyframe to, float lerp)
{
float dt = to.time - from.time;
float m0 = from.outTangent * dt;
float m1 = to.inTangent * dt;
float lerp2 = lerp * lerp;
float a = 6.0f * lerp2 - 6.0f * lerp;
float b = 3.0f * lerp2 - 4.0f * lerp + 1.0f;
float c = 3.0f * lerp2 - 2.0f * lerp;
float d = -a;
return a * from.value + b * m0 + c * m1 + d * to.value;
}
5 Likes