Now the AABB is automaticaly computed for each RigidBody

git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@383 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2010-09-01 16:37:13 +00:00
parent 4b6e2cd22b
commit f3510fb281
12 changed files with 341 additions and 131 deletions

View File

@ -21,7 +21,7 @@
#define AABB_H #define AABB_H
// Libraries // Libraries
#include "BoundingVolume.h" #include "BroadBoundingVolume.h"
#include "../mathematics/mathematics.h" #include "../mathematics/mathematics.h"
// ReactPhysics3D namespace // ReactPhysics3D namespace
@ -35,7 +35,7 @@ namespace reactphysics3d {
point and three extent size in the x,y and z directions. point and three extent size in the x,y and z directions.
------------------------------------------------------------------- -------------------------------------------------------------------
*/ */
class AABB : public BoundingVolume { class AABB : public BroadBoundingVolume {
protected : protected :
Vector3D center; // Center point of the AABB Vector3D center; // Center point of the AABB
double extent[3]; // Three extents size in the x, y and z directions double extent[3]; // Three extents size in the x, y and z directions

View File

@ -1,45 +1,60 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2009 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
* ReactPhysics3D is free software: you can redistribute it and/or modify * * ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published * * it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or * * by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* ReactPhysics3D is distributed in the hope that it will be useful, * * ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. * * GNU Lesser General Public License for more details. *
* * * *
* You should have received a copy of the GNU Lesser General Public License * * You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
// Libraries // Libraries
#include "Body.h" #include "Body.h"
#include "BoundingVolume.h" #include "BroadBoundingVolume.h"
#include "NarrowBoundingVolume.h"
// We want to use the ReactPhysics3D namespace // We want to use the ReactPhysics3D namespace
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor // Constructor
Body::Body(double mass, BoundingVolume* broadBoundingVolume, BoundingVolume* narrowBoundingVolume) throw(std::invalid_argument) Body::Body(double mass) throw(std::invalid_argument)
: mass(mass), broadBoundingVolume(broadBoundingVolume), narrowBoundingVolume(narrowBoundingVolume) { : mass(mass), broadBoundingVolume(0), narrowBoundingVolume(0) {
// Check if the mass is not larger than zero // Check if the mass is not larger than zero
if (mass <= 0.0) { if (mass <= 0.0) {
// We throw an exception // We throw an exception
throw std::invalid_argument("Exception in Body constructor : the mass has to be different larger than zero"); throw std::invalid_argument("Exception in Body constructor : the mass has to be different larger than zero");
} }
// Set the body pointer to the bounding volumes
broadBoundingVolume->setBodyPointer(this);
narrowBoundingVolume->setBodyPointer(this);
} }
// Destructor // Destructor
Body::~Body() { Body::~Body() {
if (broadBoundingVolume) {
delete broadBoundingVolume; delete broadBoundingVolume;
}
if (narrowBoundingVolume) {
delete narrowBoundingVolume; delete narrowBoundingVolume;
}
}
// Set the broad-phase bounding volume
void Body::setBroadBoundingVolume(BroadBoundingVolume* broadBoundingVolume) {
assert(broadBoundingVolume);
this->broadBoundingVolume = broadBoundingVolume;
broadBoundingVolume->setBodyPointer(this);
}
// Set the narrow-phase bounding volume
void Body::setNarrowBoundingVolume(NarrowBoundingVolume* narrowBoundingVolume) {
assert(narrowBoundingVolume);
this->narrowBoundingVolume = narrowBoundingVolume;
narrowBoundingVolume->setBodyPointer(this);
} }

View File

@ -1,33 +1,35 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2009 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
* ReactPhysics3D is free software: you can redistribute it and/or modify * * ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published * * it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or * * by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* ReactPhysics3D is distributed in the hope that it will be useful, * * ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. * * GNU Lesser General Public License for more details. *
* * * *
* You should have received a copy of the GNU Lesser General Public License * * You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef BODY_H #ifndef BODY_H
#define BODY_H #define BODY_H
// Libraries // Libraries
#include <stdexcept> #include <stdexcept>
#include <cassert>
// Namespace reactphysics3d // Namespace reactphysics3d
namespace reactphysics3d { namespace reactphysics3d {
class BoundingVolume; class BroadBoundingVolume;
class NarrowBoundingVolume;
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Body : Class Body :
@ -38,14 +40,16 @@ class BoundingVolume;
class Body { class Body {
protected : protected :
double mass; // Mass of the body double mass; // Mass of the body
BoundingVolume* broadBoundingVolume; // Bounding volume used for the broad-phase collision detection BroadBoundingVolume* broadBoundingVolume; // Bounding volume used for the broad-phase collision detection
BoundingVolume* narrowBoundingVolume; // Bounding volume used for the narrow-phase collision detection NarrowBoundingVolume* narrowBoundingVolume; // Bounding volume used for the narrow-phase collision detection
bool isMotionEnabled; // True if the body is able to move bool isMotionEnabled; // True if the body is able to move
bool isCollisionEnabled; // True if the body can collide with others bodies bool isCollisionEnabled; // True if the body can collide with others bodies
void setBroadBoundingVolume(BroadBoundingVolume* broadBoundingVolume); // Set the broad-phase bounding volume
void setNarrowBoundingVolume(NarrowBoundingVolume* narrowBoundingVolume); // Set the narrow-phase bounding volume
public : public :
Body(double mass, BoundingVolume* broadBoundingVolume, Body(double mass) throw(std::invalid_argument); // Constructor
BoundingVolume* narrowBoundingVolume) throw(std::invalid_argument); // Constructor
virtual ~Body(); // Destructor virtual ~Body(); // Destructor
double getMass() const; // Return the mass of the body double getMass() const; // Return the mass of the body
@ -54,12 +58,10 @@ class Body {
void setIsMotionEnabled(bool isMotionEnabled); // Set the value to true if the body can move void setIsMotionEnabled(bool isMotionEnabled); // Set the value to true if the body can move
bool getIsCollisionEnabled() const; // Return true if the body can collide with others bodies bool getIsCollisionEnabled() const; // Return true if the body can collide with others bodies
void setIsCollisionEnabled(bool isCollisionEnabled); // Set the isCollisionEnabled value void setIsCollisionEnabled(bool isCollisionEnabled); // Set the isCollisionEnabled value
const BoundingVolume* getBroadBoundingVolume() const; // Return the broad-phase bounding volume const BroadBoundingVolume* getBroadBoundingVolume() const; // Return the broad-phase bounding volume
const BoundingVolume* getNarrowBoundingVolume() const; // Return the narrow-phase bounding volume of the body const NarrowBoundingVolume* getNarrowBoundingVolume() const; // Return the narrow-phase bounding volume of the body
}; };
// --- Inlines function --- //
// Method that return the mass of the body // Method that return the mass of the body
inline double Body::getMass() const { inline double Body::getMass() const {
return mass; return mass;
@ -91,12 +93,12 @@ inline void Body::setIsCollisionEnabled(bool isCollisionEnabled) {
} }
// Return the broad-phase bounding volume // Return the broad-phase bounding volume
inline const BoundingVolume* Body::getBroadBoundingVolume() const { inline const BroadBoundingVolume* Body::getBroadBoundingVolume() const {
return broadBoundingVolume; return broadBoundingVolume;
} }
// Return the oriented bounding box of the rigid body // Return the oriented bounding box of the rigid body
inline const BoundingVolume* Body::getNarrowBoundingVolume() const { inline const NarrowBoundingVolume* Body::getNarrowBoundingVolume() const {
return narrowBoundingVolume; return narrowBoundingVolume;
} }

View File

@ -0,0 +1,34 @@
/****************************************************************************
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
* ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "BroadBoundingVolume.h"
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor
BroadBoundingVolume::BroadBoundingVolume() : BoundingVolume() {
}
// Destructor
BroadBoundingVolume::~BroadBoundingVolume() {
}

View File

@ -0,0 +1,47 @@
/****************************************************************************
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
* ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef BROAD_BOUNDING_VOLUME_H
#define BROAD_BOUNDING_VOLUME_H
// Libraries
#include "BoundingVolume.h"
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Class BroadBoundingVolume :
This class represents the volume that contains a rigid body
This volume will be used to compute the broad-phase collision
detection.
-------------------------------------------------------------------
*/
class BroadBoundingVolume : public BoundingVolume {
protected :
public :
BroadBoundingVolume(); // Constructor
virtual ~BroadBoundingVolume(); // Destructor
};
}
#endif

View File

@ -0,0 +1,34 @@
/****************************************************************************
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
* ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "NarrowBoundingVolume.h"
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor
NarrowBoundingVolume::NarrowBoundingVolume() : BoundingVolume() {
}
// Destructor
NarrowBoundingVolume::~NarrowBoundingVolume() {
}

View File

@ -0,0 +1,50 @@
/****************************************************************************
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
* ReactPhysics3D is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ReactPhysics3D is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef NARROW_BOUNDING_VOLUME_H
#define NARROW_BOUNDING_VOLUME_H
// Libraries
#include "BoundingVolume.h"
#include "AABB.h"
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Class NarrowBoundingVolume :
This class represents the volume that contains a rigid body
This volume will be used to compute the narrow-phase collision
detection.
-------------------------------------------------------------------
*/
class NarrowBoundingVolume : public BoundingVolume {
protected :
public :
NarrowBoundingVolume(); // Constructor
virtual ~NarrowBoundingVolume(); // Destructor
virtual AABB* computeAABB() const=0; // Return the corresponding AABB
};
}
#endif

View File

@ -176,59 +176,30 @@ vector<Vector3D> OBB::getExtremeVertices(const Vector3D& directionAxis) const {
return extremeVertices; return extremeVertices;
} }
// Return the 4 vertices of a face of the OBB. The 4 vertices will be ordered. The convention is that the index 0 corresponds to
// the face in the direction of the axis[0], 1 corresponds to the face in the opposite direction of the axis[0], 2 corresponds to
// the face in the direction of the axis[1], etc.
vector<Vector3D> OBB::getFace(unsigned int index) const throw(invalid_argument) {
// Check the argument
if (index >=0 && index <6) {
vector<Vector3D> vertices;
switch(index) {
case 0: vertices.push_back(center + (axis[0]*extent[0]) + (axis[1]*extent[1]) - (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) + (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) - (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) - (axis[1]*extent[1]) - (axis[2]*extent[2]));
break;
case 1: vertices.push_back(center - (axis[0]*extent[0]) + (axis[1]*extent[1]) - (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) + (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) - (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) - (axis[1]*extent[1]) - (axis[2]*extent[2]));
break;
case 2: vertices.push_back(center + (axis[1]*extent[1]) + (axis[0]*extent[0]) - (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) + (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) - (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) - (axis[0]*extent[0]) - (axis[2]*extent[2]));
break;
case 3: vertices.push_back(center - (axis[1]*extent[1]) + (axis[0]*extent[0]) - (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) + (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) - (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) - (axis[0]*extent[0]) - (axis[2]*extent[2]));
break;
case 4: vertices.push_back(center + (axis[2]*extent[2]) + (axis[0]*extent[0]) - (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) + (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) - (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) - (axis[0]*extent[0]) - (axis[1]*extent[1]));
break;
case 5: vertices.push_back(center - (axis[2]*extent[2]) + (axis[0]*extent[0]) - (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) + (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) - (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) - (axis[0]*extent[0]) - (axis[1]*extent[1]));
break;
}
// Return the vertices
assert(vertices.size() == 4);
return vertices;
}
else {
// Throw an exception
throw invalid_argument("Exception: The argument must be between 0 and 5");
}
}
// Static method that computes an OBB from a set of vertices. The "center" argument corresponds to the center of the OBB // Static method that computes an OBB from a set of vertices. The "center" argument corresponds to the center of the OBB
// This method allocates a new OBB object and return a pointer to the new allocated OBB object // This method allocates a new OBB object and return a pointer to the new allocated OBB object
OBB* OBB::computeFromVertices(const vector<Vector3D>& vertices, const Vector3D& center) { OBB* OBB::computeFromVertices(const vector<Vector3D>& vertices, const Vector3D& center) {
// TODO : Implement this method; // TODO : Implement this method;
return 0; return 0;
} }
// Return the corresponding AABB
AABB* OBB::computeAABB() const {
double maxLength[] = {0.0, 0.0, 0.0}; // Maximum length for each of the three x,y and z axis
Vector3D vertex;
double length;
// For each vertex of the OBB
for (int i = 0; i<8; i++) {
vertex = getVertex(i) - center;
for (int j=0; j<3; j++) {
length = std::abs(vertex.getValue(j));
if (length > maxLength[j]) {
maxLength[j] = length;
}
}
}
// Create and return the AABB
return new AABB(center, maxLength[0], maxLength[1], maxLength[2]);
}

View File

@ -22,7 +22,7 @@
// Libraries // Libraries
#include <cfloat> #include <cfloat>
#include "BoundingVolume.h" #include "NarrowBoundingVolume.h"
#include "../mathematics/mathematics.h" #include "../mathematics/mathematics.h"
// ReactPhysics3D namespace // ReactPhysics3D namespace
@ -38,7 +38,7 @@ namespace reactphysics3d {
of the box along the three axis of the OBB. of the box along the three axis of the OBB.
------------------------------------------------------------------- -------------------------------------------------------------------
*/ */
class OBB : public BoundingVolume { class OBB : public NarrowBoundingVolume {
protected : protected :
Vector3D center; // Center point of the OBB Vector3D center; // Center point of the OBB
Vector3D oldAxis[3]; // Array that contains the three unit length axis at the beginning Vector3D oldAxis[3]; // Array that contains the three unit length axis at the beginning
@ -60,6 +60,7 @@ class OBB : public BoundingVolume {
void setExtent(unsigned int index, double extent) throw(std::invalid_argument); // Set an extent value void setExtent(unsigned int index, double extent) throw(std::invalid_argument); // Set an extent value
virtual std::vector<Vector3D> getExtremeVertices(const Vector3D& axis) const; // Return all the vertices that are projected at the extreme of the projection of the bouding volume on the axis virtual std::vector<Vector3D> getExtremeVertices(const Vector3D& axis) const; // Return all the vertices that are projected at the extreme of the projection of the bouding volume on the axis
virtual void update(const Vector3D& newCenter, const Quaternion& rotationQuaternion); // Update the oriented bounding box orientation according to a new orientation of the rigid body virtual void update(const Vector3D& newCenter, const Quaternion& rotationQuaternion); // Update the oriented bounding box orientation according to a new orientation of the rigid body
virtual AABB* computeAABB() const; // Return the corresponding AABB
virtual void draw() const; // Draw the OBB (only for testing purpose) virtual void draw() const; // Draw the OBB (only for testing purpose)
static OBB* computeFromVertices(const std::vector<Vector3D>& vertices, const Vector3D& center); // Compute an OBB from a set of vertices static OBB* computeFromVertices(const std::vector<Vector3D>& vertices, const Vector3D& center); // Compute an OBB from a set of vertices
}; };
@ -132,6 +133,55 @@ inline Vector3D OBB::getVertex(unsigned int index) const throw (std::invalid_arg
} }
} }
// Return the 4 vertices of a face of the OBB. The 4 vertices will be ordered. The convention is that the index 0 corresponds to
// the face in the direction of the axis[0], 1 corresponds to the face in the opposite direction of the axis[0], 2 corresponds to
// the face in the direction of the axis[1], etc.
inline std::vector<Vector3D> OBB::getFace(unsigned int index) const throw(std::invalid_argument) {
// Check the argument
if (index >=0 && index <6) {
std::vector<Vector3D> vertices;
switch(index) {
case 0: vertices.push_back(center + (axis[0]*extent[0]) + (axis[1]*extent[1]) - (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) + (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) - (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[0]*extent[0]) - (axis[1]*extent[1]) - (axis[2]*extent[2]));
break;
case 1: vertices.push_back(center - (axis[0]*extent[0]) + (axis[1]*extent[1]) - (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) + (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) - (axis[1]*extent[1]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[0]*extent[0]) - (axis[1]*extent[1]) - (axis[2]*extent[2]));
break;
case 2: vertices.push_back(center + (axis[1]*extent[1]) + (axis[0]*extent[0]) - (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) + (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) - (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center + (axis[1]*extent[1]) - (axis[0]*extent[0]) - (axis[2]*extent[2]));
break;
case 3: vertices.push_back(center - (axis[1]*extent[1]) + (axis[0]*extent[0]) - (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) + (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) - (axis[0]*extent[0]) + (axis[2]*extent[2]));
vertices.push_back(center - (axis[1]*extent[1]) - (axis[0]*extent[0]) - (axis[2]*extent[2]));
break;
case 4: vertices.push_back(center + (axis[2]*extent[2]) + (axis[0]*extent[0]) - (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) + (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) - (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center + (axis[2]*extent[2]) - (axis[0]*extent[0]) - (axis[1]*extent[1]));
break;
case 5: vertices.push_back(center - (axis[2]*extent[2]) + (axis[0]*extent[0]) - (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) + (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) - (axis[0]*extent[0]) + (axis[1]*extent[1]));
vertices.push_back(center - (axis[2]*extent[2]) - (axis[0]*extent[0]) - (axis[1]*extent[1]));
break;
}
// Return the vertices
assert(vertices.size() == 4);
return vertices;
}
else {
// Throw an exception
throw std::invalid_argument("Exception: The argument must be between 0 and 5");
}
}
// Return an extent value // Return an extent value
inline double OBB::getExtent(unsigned int index) const throw(std::invalid_argument) { inline double OBB::getExtent(unsigned int index) const throw(std::invalid_argument) {

View File

@ -19,14 +19,16 @@
// Libraries // Libraries
#include "RigidBody.h" #include "RigidBody.h"
#include "BroadBoundingVolume.h"
#include "NarrowBoundingVolume.h"
// We want to use the ReactPhysics3D namespace // We want to use the ReactPhysics3D namespace
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor // Constructor
RigidBody::RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal, BoundingVolume* broadBoundingVolume, RigidBody::RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal,
BoundingVolume* narrowBoundingVolume) NarrowBoundingVolume* narrowBoundingVolume)
: Body(mass, broadBoundingVolume, narrowBoundingVolume), position(position), orientation(orientation.getUnit()), inertiaTensorLocal(inertiaTensorLocal), : Body(mass), position(position), orientation(orientation.getUnit()), inertiaTensorLocal(inertiaTensorLocal),
inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass), oldPosition(position), oldOrientation(orientation) { inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass), oldPosition(position), oldOrientation(orientation) {
restitution = 1.0; restitution = 1.0;
@ -34,6 +36,12 @@
isCollisionEnabled = true; isCollisionEnabled = true;
interpolationFactor = 0.0; interpolationFactor = 0.0;
// Set the bounding volume for the narrow-phase collision detection
setNarrowBoundingVolume(narrowBoundingVolume);
// Compute the broad-phase bounding volume (an AABB)
setBroadBoundingVolume(narrowBoundingVolume->computeAABB());
// Update the orientation of the OBB according to the orientation of the rigid body // Update the orientation of the OBB according to the orientation of the rigid body
update(); update();
@ -45,3 +53,10 @@
RigidBody::~RigidBody() { RigidBody::~RigidBody() {
}; };
// Update the rigid body in order to reflect a change in the body state
void RigidBody::update() {
// Update the orientation of the corresponding bounding volumes of the rigid body
broadBoundingVolume->update(position, orientation);
narrowBoundingVolume->update(position, orientation);
}

View File

@ -15,7 +15,7 @@
* * * *
* You should have received a copy of the GNU Lesser General Public License * * You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef RIGIDBODY_H #ifndef RIGIDBODY_H
#define RIGIDBODY_H #define RIGIDBODY_H
@ -23,7 +23,6 @@
// Libraries // Libraries
#include <cassert> #include <cassert>
#include "Body.h" #include "Body.h"
#include "BoundingVolume.h"
#include "../mathematics/mathematics.h" #include "../mathematics/mathematics.h"
// Namespace reactphysics3d // Namespace reactphysics3d
@ -54,8 +53,8 @@ class RigidBody : public Body {
double restitution; // Coefficient of restitution (between 0 and 1), 1 for a very boucing body double restitution; // Coefficient of restitution (between 0 and 1), 1 for a very boucing body
public : public :
RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal, RigidBody(const Vector3D& position, const Quaternion& orientation, double mass,
BoundingVolume* broadBoundingVolume, BoundingVolume* narrowBoundingVolume); // Constructor // Copy-constructor const Matrix3x3& inertiaTensorLocal, NarrowBoundingVolume* narrowBoundingVolume); // Constructor // Copy-constructor
virtual ~RigidBody(); // Destructor virtual ~RigidBody(); // Destructor
Vector3D getPosition() const; // Return the position of the body Vector3D getPosition() const; // Return the position of the body
@ -190,7 +189,7 @@ inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const {
// Set the interpolation factor of the body // Set the interpolation factor of the body
inline void RigidBody::setInterpolationFactor(double factor) { inline void RigidBody::setInterpolationFactor(double factor) {
//assert(factor >= 0.0 && factor <= 1.0); assert(factor >= 0.0 && factor <= 1.0);
// Set the factor // Set the factor
interpolationFactor = factor; interpolationFactor = factor;
@ -240,13 +239,6 @@ inline void RigidBody::updateOldPositionAndOrientation() {
oldOrientation = orientation; oldOrientation = orientation;
} }
// Update the rigid body in order to reflect a change in the body state
inline void RigidBody::update() {
// Update the orientation of the corresponding bounding volumes of the rigid body
broadBoundingVolume->update(position, orientation);
narrowBoundingVolume->update(position, orientation);
}
} // End of the ReactPhyscis3D namespace } // End of the ReactPhyscis3D namespace
#endif #endif