Handle division by zero in a better way in Vector2, Vector3 getUnit() and normalize()

This commit is contained in:
Daniel Chappuis 2015-08-11 06:58:55 +02:00
parent 047f612c37
commit 2a3fe8f8d1
6 changed files with 12 additions and 108 deletions

View File

@ -54,7 +54,9 @@ Vector2::~Vector2() {
Vector2 Vector2::getUnit() const { Vector2 Vector2::getUnit() const {
decimal lengthVector = length(); decimal lengthVector = length();
assert(lengthVector > MACHINE_EPSILON); if (lengthVector < MACHINE_EPSILON) {
return *this;
}
// Compute and return the unit vector // Compute and return the unit vector
decimal lengthInv = decimal(1.0) / lengthVector; decimal lengthInv = decimal(1.0) / lengthVector;

View File

@ -178,7 +178,9 @@ inline decimal Vector2::dot(const Vector2& vector) const {
// Normalize the vector // Normalize the vector
inline void Vector2::normalize() { inline void Vector2::normalize() {
decimal l = length(); decimal l = length();
assert(l > std::numeric_limits<decimal>::epsilon()); if (l < MACHINE_EPSILON) {
return;
}
x /= l; x /= l;
y /= l; y /= l;
} }

View File

@ -55,7 +55,9 @@ Vector3::~Vector3() {
Vector3 Vector3::getUnit() const { Vector3 Vector3::getUnit() const {
decimal lengthVector = length(); decimal lengthVector = length();
assert(lengthVector > MACHINE_EPSILON); if (lengthVector < MACHINE_EPSILON) {
return *this;
}
// Compute and return the unit vector // Compute and return the unit vector
decimal lengthInv = decimal(1.0) / lengthVector; decimal lengthInv = decimal(1.0) / lengthVector;

View File

@ -193,7 +193,9 @@ inline Vector3 Vector3::cross(const Vector3& vector) const {
// Normalize the vector // Normalize the vector
inline void Vector3::normalize() { inline void Vector3::normalize() {
decimal l = length(); decimal l = length();
assert(l > std::numeric_limits<decimal>::epsilon()); if (l < MACHINE_EPSILON) {
return;
}
x /= l; x /= l;
y /= l; y /= l;
z /= l; z /= l;

View File

@ -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) {
}

View File

@ -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<ContactPoint> 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