From 6376b833274ebe05508176bab6371ad78b4fdf1d Mon Sep 17 00:00:00 2001 From: ScottLongley Date: Sun, 15 Aug 2021 19:48:12 +1000 Subject: [PATCH] Add LookingAt to Actor --- Source/Engine/Level/Actor.cpp | 27 ++++++++++++++++++++------- Source/Engine/Level/Actor.h | 13 +++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 076ac4608..1bbae93fa 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -1424,10 +1424,24 @@ Actor* Actor::Intersects(const Ray& ray, float& distance, Vector3& normal) } void Actor::LookAt(const Vector3& worldPos) +{ + const Quaternion orientation = LookingAt(worldPos); + + SetOrientation(orientation); +} + +void Actor::LookAt(const Vector3& worldPos, const Vector3& worldUp) +{ + const Quaternion orientation = LookingAt(worldPos, worldUp); + + SetOrientation(orientation); +} + +Quaternion Actor::LookingAt(const Vector3& worldPos) { const Vector3 direction = worldPos - _transform.Translation; if (direction.LengthSquared() < ZeroTolerance) - return; + return _parent->GetOrientation(); const Vector3 newForward = Vector3::Normalize(direction); const Vector3 oldForward = _transform.Orientation * Vector3::Forward; @@ -1447,26 +1461,25 @@ void Actor::LookAt(const Vector3& worldPos) orientation = rotQuat * _transform.Orientation; } - SetOrientation(orientation); + return orientation; } -void Actor::LookAt(const Vector3& worldPos, const Vector3& worldUp) +Quaternion Actor::LookingAt(const Vector3& worldPos, const Vector3& worldUp) { const Vector3 direction = worldPos - _transform.Translation; if (direction.LengthSquared() < ZeroTolerance) - return; + return _parent->GetOrientation(); const Vector3 forward = Vector3::Normalize(direction); const Vector3 up = Vector3::Normalize(worldUp); if (Math::IsOne(Vector3::Dot(forward, up))) { - LookAt(worldPos); - return; + return LookingAt(worldPos); } Quaternion orientation; Quaternion::LookRotation(direction, up, orientation); - SetOrientation(orientation); + return orientation; } void WriteObjectToBytes(SceneObject* obj, rapidjson_flax::StringBuffer& buffer, MemoryWriteStream& output) diff --git a/Source/Engine/Level/Actor.h b/Source/Engine/Level/Actor.h index 890d9f2f1..56930eccf 100644 --- a/Source/Engine/Level/Actor.h +++ b/Source/Engine/Level/Actor.h @@ -792,6 +792,19 @@ public: /// The up direction that Constrains y axis orientation to a plane this vector lies on. This rule might be broken if forward and up direction are nearly parallel. API_FUNCTION() void LookAt(const Vector3& worldPos, const Vector3& worldUp); + /// + /// Gets rotation of the actor oriented towards the specified world position. + /// + /// The world position to orient towards. + API_FUNCTION() Quaternion LookingAt(const Vector3& worldPos); + + /// + /// Gets rotation of the actor oriented towards the specified world position with upwards direction. + /// + /// The world position to orient towards. + /// The up direction that Constrains y axis orientation to a plane this vector lies on. This rule might be broken if forward and up direction are nearly parallel. + API_FUNCTION() Quaternion LookingAt(const Vector3& worldPos, const Vector3& worldUp); + public: ///