From aa2eeb82437363680415e83562a83bbf64d291d3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 26 Jan 2021 12:32:35 +0100 Subject: [PATCH] Add GetSplineDirection util to Spline --- Source/Engine/Level/Actors/Spline.cpp | 15 +++++++++++++++ Source/Engine/Level/Actors/Spline.h | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Source/Engine/Level/Actors/Spline.cpp b/Source/Engine/Level/Actors/Spline.cpp index 4c2680e79..9f47aa06e 100644 --- a/Source/Engine/Level/Actors/Spline.cpp +++ b/Source/Engine/Level/Actors/Spline.cpp @@ -77,6 +77,21 @@ Transform Spline::GetSplineLocalTransform(float time) const return t; } +Vector3 Spline::GetSplineDirection(float time) const +{ + return _transform.LocalToWorldVector(GetSplineLocalDirection(time)); +} + +Vector3 Spline::GetSplineLocalDirection(float time) const +{ + if (Curve.GetKeyframes().Count() == 0) + return Vector3::Forward; + Transform t; + Curve.EvaluateFirstDerivative(t, time, _loop); + t.Translation.Normalize(); + return t.Translation; +} + Vector3 Spline::GetSplinePoint(int32 index) const { CHECK_RETURN(index >= 0 && index < GetSplinePointsCount(), Vector3::Zero) diff --git a/Source/Engine/Level/Actors/Spline.h b/Source/Engine/Level/Actors/Spline.h index fd489b823..cc4226414 100644 --- a/Source/Engine/Level/Actors/Spline.h +++ b/Source/Engine/Level/Actors/Spline.h @@ -96,6 +96,20 @@ public: /// The calculated curve point transformation (local-space). API_FUNCTION() Transform GetSplineLocalTransform(float time) const; + /// + /// Evaluates the spline curve direction (forward vector, aka position 1st derivative) at the given time in 3D (world-space). + /// + /// The time value. Can be negative or larger than curve length (curve will loop or clamp). + /// The calculated curve direction (world-space). + API_FUNCTION() Vector3 GetSplineDirection(float time) const; + + /// + /// Evaluates the spline curve direction (forward vector, aka position 1st derivative) at the given time in 3D (local-space). + /// + /// The time value. Can be negative or larger than curve length (curve will loop or clamp). + /// The calculated curve direction (local-space). + API_FUNCTION() Vector3 GetSplineLocalDirection(float time) const; + /// /// Evaluates the spline curve at the given index (world-space). ///