diff --git a/Source/Contents/ActorGeometry.cpp b/Source/Contents/ActorGeometry.cpp new file mode 100644 index 0000000..dd7717a --- /dev/null +++ b/Source/Contents/ActorGeometry.cpp @@ -0,0 +1,54 @@ +#include "Contents/Actors/RenderObject.h" +#include "Utility/Math/AffineMatrix.h" + +#include + +// FUNCTION: fn_21007 +float __stdcall CRenderObject::Heading(float x, float z, float targetX, float targetZ) { + const float deltaX = targetX - x; + const float deltaZ = targetZ - z; + float angle = (static_cast(std::atan2(static_cast(deltaX), static_cast(-deltaZ))) / 3.141592f) * 180.0f; + if (angle >= 360.0f) { + angle -= 360.0f; + } + + if (angle < 0.0f) { + angle += 360.0f; + } + + return angle; +} + +// FUNCTION: fn_21008 +void CRenderObject::Face(float x, float z) { + const float deltaX = x - this->render.position[0]; + const float deltaZ = z - this->render.position[2]; + if (deltaX != 0.0f || deltaZ != 0.0f) { + float angle = (static_cast(std::atan2(static_cast(deltaX), static_cast(-deltaZ))) / 3.141592f) * 180.0f; + this->render.angle = angle; + if (angle >= 360.0f) { + angle -= 360.0f; + this->render.angle = angle; + } + + if (angle < 0.0f) { + this->render.angle = angle + 360.0f; + } + } +} + +// FUNCTION: fn_21009 +float CRenderObject::HorizontalDistance(float x, float z) const { + const float deltaX = x - this->render.position[0]; + const float deltaZ = z - this->render.position[2]; + return SquareRoot(deltaZ * deltaZ + deltaX * deltaX); +} + +// FUNCTION: fn_21010 +float CRenderObject::Distance(float x, float y, float z) const { + const float deltaX = x - this->render.position[0]; + const float deltaZ = z - this->render.position[2]; + const float deltaY = y - this->render.position[1]; + const float horizontal = SquareRoot(deltaZ * deltaZ + deltaX * deltaX); + return SquareRoot(horizontal * horizontal + deltaY * deltaY); +} diff --git a/Source/Contents/Actors/RenderObject.h b/Source/Contents/Actors/RenderObject.h index 69ec0e5..604a97a 100644 --- a/Source/Contents/Actors/RenderObject.h +++ b/Source/Contents/Actors/RenderObject.h @@ -14,6 +14,10 @@ public: virtual void SetScreenBounds(const float* bounds, float x, float y); virtual void SetScreenPosition(float x, float y); + static float __stdcall Heading(float x, float z, float targetX, float targetZ); + void Face(float x, float z); + float HorizontalDistance(float x, float z) const; + float Distance(float x, float y, float z) const; RenderObjectState render; }; diff --git a/Source/Utility/Math/AffineMatrix.h b/Source/Utility/Math/AffineMatrix.h index d517784..44c2641 100644 --- a/Source/Utility/Math/AffineMatrix.h +++ b/Source/Utility/Math/AffineMatrix.h @@ -8,6 +8,7 @@ float ClientCos(float radians); float ClientRadians(int degrees); float ClientSine(int degrees); float ClientCosine(int degrees); +float SquareRoot(float value); struct AffineMatrix { float values[12]; diff --git a/compile.json b/compile.json index 83df415..4aac58b 100644 --- a/compile.json +++ b/compile.json @@ -239,6 +239,7 @@ "Source/Client/Registry.cpp": 0, "Source/Client/ReplayPacketRecorder.cpp": 2, "Source/Client/Session.cpp": 0, + "Source/Contents/ActorGeometry.cpp": 4, "Source/Contents/ActorNameCard.cpp": 4, "Source/Contents/ActorNameDirectory.cpp": 4, "Source/Contents/Actors/AttackMotion.cpp": 4,