From 35066a3451bc972f79b3226fc937b03bf114fe4b Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 17 Jun 2015 23:35:06 +0200 Subject: [PATCH] Math/Sphere: Fix (Squared)Distance Former-commit-id: d91c0d6e8a193e47c3ed45babaf3b83bf1cbe7ee --- include/Nazara/Math/Sphere.inl | 10 ++++------ include/Nazara/Math/Vector3.hpp | 3 +++ include/Nazara/Math/Vector3.inl | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Math/Sphere.inl b/include/Nazara/Math/Sphere.inl index f8ece724c..f617d28e2 100644 --- a/include/Nazara/Math/Sphere.inl +++ b/include/Nazara/Math/Sphere.inl @@ -68,14 +68,13 @@ bool NzSphere::Contains(const NzVector3& point) const template T NzSphere::Distance(T X, T Y, T Z) const { - NzVector3 distance(X-x, Y-y, Z-z); - return distance.GetLength(); + return Distance({X, Y, Z}); } template T NzSphere::Distance(const NzVector3& point) const { - return Distance(point.x, point.y, point.z); + return NzVector3f::Distance(point, GetPosition()) - radius; } template @@ -249,14 +248,13 @@ NzSphere& NzSphere::Set(const NzSphere& sphere) template T NzSphere::SquaredDistance(T X, T Y, T Z) const { - NzVector3 distance(X-x, Y-y, Z-z); - return distance.GetSquaredLength(); + return SquaredDistance({X, Y, Z}); } template T NzSphere::SquaredDistance(const NzVector3& point) const { - return SquaredDistance(point.x, point.y, point.z); + return NzVector3f::Distance(point, GetPosition()) - radius * radius; } template diff --git a/include/Nazara/Math/Vector3.hpp b/include/Nazara/Math/Vector3.hpp index 14cd41b0a..f180816b2 100644 --- a/include/Nazara/Math/Vector3.hpp +++ b/include/Nazara/Math/Vector3.hpp @@ -103,12 +103,15 @@ class NzVector3 static NzVector3 Backward(); static NzVector3 CrossProduct(const NzVector3& vec1, const NzVector3& vec2); static T DotProduct(const NzVector3& vec1, const NzVector3& vec2); + static T Distance(const NzVector3& vec1, const NzVector3& vec2); + static float Distancef(const NzVector3& vec1, const NzVector3& vec2); static NzVector3 Down(); static NzVector3 Forward(); static NzVector3 Left(); static NzVector3 Lerp(const NzVector3& from, const NzVector3& to, T interpolation); static NzVector3 Normalize(const NzVector3& vec); static NzVector3 Right(); + static T SquaredDistance(const NzVector3& vec1, const NzVector3& vec2); static NzVector3 Unit(); static NzVector3 UnitX(); static NzVector3 UnitY(); diff --git a/include/Nazara/Math/Vector3.inl b/include/Nazara/Math/Vector3.inl index a2f0ed35c..0d6d10cf5 100644 --- a/include/Nazara/Math/Vector3.inl +++ b/include/Nazara/Math/Vector3.inl @@ -567,6 +567,18 @@ NzVector3 NzVector3::Backward() return vector; } +template +T NzVector3::Distance(const NzVector3& vec1, const NzVector3& vec2) +{ + return vec1.Distance(vec2); +} + +template +float NzVector3::Distancef(const NzVector3& vec1, const NzVector3& vec2) +{ + return vec1.Distancef(vec2); +} + template NzVector3 NzVector3::Down() { @@ -615,6 +627,12 @@ NzVector3 NzVector3::Right() return vector; } +template +T NzVector3::SquaredDistance(const NzVector3& vec1, const NzVector3& vec2) +{ + return vec1.SquaredDistance(vec2); +} + template NzVector3 NzVector3::Unit() {