public static double Dot(Vector v1, Vector v2) { Debug.Assert(v1.IsRational()); Debug.Assert(v2.IsRational()); return v1.X * v2.X + v1.Y * v2.Y; }
public static bool Animate( Point currentValue, Vector currentVelocity, Point targetValue, double attractionFator, double dampening, double terminalVelocity, double minValueDelta, double minVelocityDelta, out Point newValue, out Vector newVelocity) { Debug.Assert(currentValue.IsRational()); Debug.Assert(currentVelocity.IsRational()); Debug.Assert(targetValue.IsRational()); Debug.Assert(dampening.IsRational()); Debug.Assert(dampening > 0 && dampening < 1); Debug.Assert(attractionFator.IsRational()); Debug.Assert(attractionFator > 0); Debug.Assert(terminalVelocity > 0); Debug.Assert(minValueDelta > 0); Debug.Assert(minVelocityDelta > 0); Vector diff = targetValue - currentValue; if (diff.Length > minValueDelta || currentVelocity.Length > minVelocityDelta) { newVelocity = currentVelocity * (1 - dampening); newVelocity += diff * attractionFator; newVelocity *= (currentVelocity.Length > terminalVelocity) ? terminalVelocity / currentVelocity.Length : 1; newValue = currentValue + newVelocity; return true; } else { newValue = targetValue; newVelocity = new Vector(); return false; } }
public static bool Animate( Point currentValue, Vector currentVelocity, Vector force, double dampening, double terminalVelocity, double minValueDelta, double minVelocityDelta, out Point newValue, out Vector newVelocity) { Debug.Assert(currentValue.IsRational()); Debug.Assert(currentVelocity.IsRational()); Debug.Assert(dampening.IsRational()); Debug.Assert(dampening > 0 && dampening < 1); Debug.Assert(terminalVelocity > 0); Debug.Assert(minValueDelta > 0); Debug.Assert(minVelocityDelta > 0); if (force.Length > minValueDelta || currentVelocity.Length > minVelocityDelta) { newVelocity = currentVelocity * (1 - dampening); newVelocity += force; newVelocity *= (currentVelocity.Length > terminalVelocity) ? terminalVelocity / currentVelocity.Length : 1; newValue = currentValue + newVelocity; return true; } else { newValue = currentValue; newVelocity = new Vector(); return false; } }
public static double AngleRad(Vector v1, Vector v2) { Debug.Assert(v1.IsRational()); Debug.Assert(v2.IsRational()); double dot = Dot(v1, v2); double dotNormalize = dot / (v1.Length * v2.Length); double acos = Math.Acos(dotNormalize); return acos; }