From 2a3fe8f8d1d47ebe83aa15758eed4961095096d5 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Tue, 11 Aug 2015 06:58:55 +0200 Subject: [PATCH] Handle division by zero in a better way in Vector2, Vector3 getUnit() and normalize() --- src/mathematics/Vector2.cpp | 4 +- src/mathematics/Vector2.h | 4 +- src/mathematics/Vector3.cpp | 4 +- src/mathematics/Vector3.h | 4 +- testbed/src/PhysicsEventListener.cpp | 38 ---------------- testbed/src/PhysicsEventListener.h | 66 ---------------------------- 6 files changed, 12 insertions(+), 108 deletions(-) delete mode 100644 testbed/src/PhysicsEventListener.cpp delete mode 100644 testbed/src/PhysicsEventListener.h diff --git a/src/mathematics/Vector2.cpp b/src/mathematics/Vector2.cpp index ccad70dc..80c0ca34 100644 --- a/src/mathematics/Vector2.cpp +++ b/src/mathematics/Vector2.cpp @@ -54,7 +54,9 @@ Vector2::~Vector2() { Vector2 Vector2::getUnit() const { decimal lengthVector = length(); - assert(lengthVector > MACHINE_EPSILON); + if (lengthVector < MACHINE_EPSILON) { + return *this; + } // Compute and return the unit vector decimal lengthInv = decimal(1.0) / lengthVector; diff --git a/src/mathematics/Vector2.h b/src/mathematics/Vector2.h index 666d60c7..1083d26a 100644 --- a/src/mathematics/Vector2.h +++ b/src/mathematics/Vector2.h @@ -178,7 +178,9 @@ inline decimal Vector2::dot(const Vector2& vector) const { // Normalize the vector inline void Vector2::normalize() { decimal l = length(); - assert(l > std::numeric_limits::epsilon()); + if (l < MACHINE_EPSILON) { + return; + } x /= l; y /= l; } diff --git a/src/mathematics/Vector3.cpp b/src/mathematics/Vector3.cpp index 1e33136a..48bb714f 100644 --- a/src/mathematics/Vector3.cpp +++ b/src/mathematics/Vector3.cpp @@ -55,7 +55,9 @@ Vector3::~Vector3() { Vector3 Vector3::getUnit() const { decimal lengthVector = length(); - assert(lengthVector > MACHINE_EPSILON); + if (lengthVector < MACHINE_EPSILON) { + return *this; + } // Compute and return the unit vector decimal lengthInv = decimal(1.0) / lengthVector; diff --git a/src/mathematics/Vector3.h b/src/mathematics/Vector3.h index f8710fe3..f8ffb77f 100644 --- a/src/mathematics/Vector3.h +++ b/src/mathematics/Vector3.h @@ -193,7 +193,9 @@ inline Vector3 Vector3::cross(const Vector3& vector) const { // Normalize the vector inline void Vector3::normalize() { decimal l = length(); - assert(l > std::numeric_limits::epsilon()); + if (l < MACHINE_EPSILON) { + return; + } x /= l; y /= l; z /= l; diff --git a/testbed/src/PhysicsEventListener.cpp b/testbed/src/PhysicsEventListener.cpp deleted file mode 100644 index 8d34b9b0..00000000 --- a/testbed/src/PhysicsEventListener.cpp +++ /dev/null @@ -1,38 +0,0 @@ - -/******************************************************************************** -* ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2015 Daniel Chappuis * -********************************************************************************* -* * -* This software is provided 'as-is', without any express or implied warranty. * -* In no event will the authors be held liable for any damages arising from the * -* use of this software. * -* * -* Permission is granted to anyone to use this software for any purpose, * -* including commercial applications, and to alter it and redistribute it * -* freely, subject to the following restrictions: * -* * -* 1. The origin of this software must not be misrepresented; you must not claim * -* that you wrote the original software. If you use this software in a * -* product, an acknowledgment in the product documentation would be * -* appreciated but is not required. * -* * -* 2. Altered source versions must be plainly marked as such, and must not be * -* misrepresented as being the original software. * -* * -* 3. This notice may not be removed or altered from any source distribution. * -* * -********************************************************************************/ - -// Libraries -#include "PhysicsEventListener.h" - -// Called when a new contact point is found between two bodies that were separated before -void PhysicsEventListener::beginContact(const rp3d::ContactPointInfo& contact) { - -} - -// Called when a new contact point is found between two bodies -void PhysicsEventListener::newContact(const rp3d::ContactPointInfo& contact) { - -} diff --git a/testbed/src/PhysicsEventListener.h b/testbed/src/PhysicsEventListener.h deleted file mode 100644 index d9227feb..00000000 --- a/testbed/src/PhysicsEventListener.h +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://www.reactphysics3d.com * -* Copyright (c) 2010-2015 Daniel Chappuis * -********************************************************************************* -* * -* This software is provided 'as-is', without any express or implied warranty. * -* In no event will the authors be held liable for any damages arising from the * -* use of this software. * -* * -* Permission is granted to anyone to use this software for any purpose, * -* including commercial applications, and to alter it and redistribute it * -* freely, subject to the following restrictions: * -* * -* 1. The origin of this software must not be misrepresented; you must not claim * -* that you wrote the original software. If you use this software in a * -* product, an acknowledgment in the product documentation would be * -* appreciated but is not required. * -* * -* 2. Altered source versions must be plainly marked as such, and must not be * -* misrepresented as being the original software. * -* * -* 3. This notice may not be removed or altered from any source distribution. * -* * -********************************************************************************/ - -#ifndef PHYSICSEVENTLISTENER_H -#define PHYSICSEVENTLISTENER_H - -// Libraries -#include "reactphysics3d.h" -#include "openglframework.h" - -// Structure ContactPoint -struct ContactPoint { - - public: - openglframework::Vector3 point; - - /// Constructor - ContactPoint(const openglframework::Vector3& contactPoint) : point(contactPoint) { - - } -}; - -// Class PhysicsEventListener -// This class inherits from the EventListener class -// of ReactPhysics3D in order to be notified of events -// that occured in a physics world -class PhysicsEventListener : rp3d::EventListener { - - private: - - // Current contact points - std::vector mCurrentContactPoints; - - public: - - /// Called when a new contact point is found between two bodies that were separated before - virtual void beginContact(const rp3d::ContactPointInfo& contact); - - /// Called when a new contact point is found between two bodies - virtual void newContact(const rp3d::ContactPointInfo& contact); -}; - -#endif -