From f3510fb281236464bb48d89fdb50738d8b63ae10 Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Wed, 1 Sep 2010 16:37:13 +0000 Subject: [PATCH] Now the AABB is automaticaly computed for each RigidBody git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@383 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- sources/reactphysics3d/body/AABB.h | 4 +- sources/reactphysics3d/body/Body.cpp | 67 ++++++++++------- sources/reactphysics3d/body/Body.h | 68 +++++++++--------- sources/reactphysics3d/body/BoundingVolume.h | 2 +- .../body/BroadBoundingVolume.cpp | 34 +++++++++ .../reactphysics3d/body/BroadBoundingVolume.h | 47 ++++++++++++ .../body/NarrowBoundingVolume.cpp | 34 +++++++++ .../body/NarrowBoundingVolume.h | 50 +++++++++++++ sources/reactphysics3d/body/OBB.cpp | 71 ++++++------------- sources/reactphysics3d/body/OBB.h | 54 +++++++++++++- sources/reactphysics3d/body/RigidBody.cpp | 23 ++++-- sources/reactphysics3d/body/RigidBody.h | 18 ++--- 12 files changed, 341 insertions(+), 131 deletions(-) create mode 100644 sources/reactphysics3d/body/BroadBoundingVolume.cpp create mode 100644 sources/reactphysics3d/body/BroadBoundingVolume.h create mode 100644 sources/reactphysics3d/body/NarrowBoundingVolume.cpp create mode 100644 sources/reactphysics3d/body/NarrowBoundingVolume.h diff --git a/sources/reactphysics3d/body/AABB.h b/sources/reactphysics3d/body/AABB.h index 324c7a11..deb83be6 100644 --- a/sources/reactphysics3d/body/AABB.h +++ b/sources/reactphysics3d/body/AABB.h @@ -21,7 +21,7 @@ #define AABB_H // Libraries -#include "BoundingVolume.h" +#include "BroadBoundingVolume.h" #include "../mathematics/mathematics.h" // ReactPhysics3D namespace @@ -35,7 +35,7 @@ namespace reactphysics3d { point and three extent size in the x,y and z directions. ------------------------------------------------------------------- */ -class AABB : public BoundingVolume { +class AABB : public BroadBoundingVolume { protected : Vector3D center; // Center point of the AABB double extent[3]; // Three extents size in the x, y and z directions diff --git a/sources/reactphysics3d/body/Body.cpp b/sources/reactphysics3d/body/Body.cpp index 8d19ad7e..88eb1e69 100644 --- a/sources/reactphysics3d/body/Body.cpp +++ b/sources/reactphysics3d/body/Body.cpp @@ -1,45 +1,60 @@ /**************************************************************************** - * 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 . * - ***************************************************************************/ +* 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 . * +***************************************************************************/ // Libraries #include "Body.h" -#include "BoundingVolume.h" +#include "BroadBoundingVolume.h" +#include "NarrowBoundingVolume.h" // We want to use the ReactPhysics3D namespace using namespace reactphysics3d; // Constructor -Body::Body(double mass, BoundingVolume* broadBoundingVolume, BoundingVolume* narrowBoundingVolume) throw(std::invalid_argument) - : mass(mass), broadBoundingVolume(broadBoundingVolume), narrowBoundingVolume(narrowBoundingVolume) { +Body::Body(double mass) throw(std::invalid_argument) + : mass(mass), broadBoundingVolume(0), narrowBoundingVolume(0) { // Check if the mass is not larger than zero if (mass <= 0.0) { // We throw an exception 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 Body::~Body() { - delete broadBoundingVolume; - delete narrowBoundingVolume; + if (broadBoundingVolume) { + delete broadBoundingVolume; + } + if (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); } diff --git a/sources/reactphysics3d/body/Body.h b/sources/reactphysics3d/body/Body.h index 94f5a085..33123791 100644 --- a/sources/reactphysics3d/body/Body.h +++ b/sources/reactphysics3d/body/Body.h @@ -1,33 +1,35 @@ /**************************************************************************** - * 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 . * - ***************************************************************************/ +* 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 . * +***************************************************************************/ #ifndef BODY_H #define BODY_H - // Libraries +// Libraries #include +#include // Namespace reactphysics3d namespace reactphysics3d { -class BoundingVolume; +class BroadBoundingVolume; +class NarrowBoundingVolume; /* ------------------------------------------------------------------- Class Body : @@ -37,16 +39,18 @@ class BoundingVolume; */ class Body { protected : - double mass; // Mass of the body - BoundingVolume* broadBoundingVolume; // Bounding volume used for the broad-phase collision detection - BoundingVolume* narrowBoundingVolume; // Bounding volume used for the narrow-phase collision detection - bool isMotionEnabled; // True if the body is able to move - bool isCollisionEnabled; // True if the body can collide with others bodies + double mass; // Mass of the body + BroadBoundingVolume* broadBoundingVolume; // Bounding volume used for the broad-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 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 : - Body(double mass, BoundingVolume* broadBoundingVolume, - BoundingVolume* narrowBoundingVolume) throw(std::invalid_argument); // Constructor - virtual ~Body(); // Destructor + Body(double mass) throw(std::invalid_argument); // Constructor + virtual ~Body(); // Destructor double getMass() const; // Return the mass of the body void setMass(double mass); // Set 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 bool getIsCollisionEnabled() const; // Return true if the body can collide with others bodies void setIsCollisionEnabled(bool isCollisionEnabled); // Set the isCollisionEnabled value - const BoundingVolume* getBroadBoundingVolume() const; // Return the broad-phase bounding volume - const BoundingVolume* getNarrowBoundingVolume() const; // Return the narrow-phase bounding volume of the body + const BroadBoundingVolume* getBroadBoundingVolume() const; // Return the broad-phase bounding volume + const NarrowBoundingVolume* getNarrowBoundingVolume() const; // Return the narrow-phase bounding volume of the body }; -// --- Inlines function --- // - // Method that return the mass of the body inline double Body::getMass() const { return mass; @@ -91,12 +93,12 @@ inline void Body::setIsCollisionEnabled(bool isCollisionEnabled) { } // Return the broad-phase bounding volume -inline const BoundingVolume* Body::getBroadBoundingVolume() const { +inline const BroadBoundingVolume* Body::getBroadBoundingVolume() const { return broadBoundingVolume; } // Return the oriented bounding box of the rigid body -inline const BoundingVolume* Body::getNarrowBoundingVolume() const { +inline const NarrowBoundingVolume* Body::getNarrowBoundingVolume() const { return narrowBoundingVolume; } diff --git a/sources/reactphysics3d/body/BoundingVolume.h b/sources/reactphysics3d/body/BoundingVolume.h index c0a45b09..80512018 100644 --- a/sources/reactphysics3d/body/BoundingVolume.h +++ b/sources/reactphysics3d/body/BoundingVolume.h @@ -38,7 +38,7 @@ namespace reactphysics3d { */ class BoundingVolume { protected : - Body* body; // Pointer to the body + Body* body; // Pointer to the body public : BoundingVolume(); // Constructor diff --git a/sources/reactphysics3d/body/BroadBoundingVolume.cpp b/sources/reactphysics3d/body/BroadBoundingVolume.cpp new file mode 100644 index 00000000..8a00f97a --- /dev/null +++ b/sources/reactphysics3d/body/BroadBoundingVolume.cpp @@ -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 . * +***************************************************************************/ + +// Libraries +#include "BroadBoundingVolume.h" + +// We want to use the ReactPhysics3D namespace +using namespace reactphysics3d; + +// Constructor +BroadBoundingVolume::BroadBoundingVolume() : BoundingVolume() { + +} + +// Destructor +BroadBoundingVolume::~BroadBoundingVolume() { + +} diff --git a/sources/reactphysics3d/body/BroadBoundingVolume.h b/sources/reactphysics3d/body/BroadBoundingVolume.h new file mode 100644 index 00000000..89e3df64 --- /dev/null +++ b/sources/reactphysics3d/body/BroadBoundingVolume.h @@ -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 . * +***************************************************************************/ + +#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 \ No newline at end of file diff --git a/sources/reactphysics3d/body/NarrowBoundingVolume.cpp b/sources/reactphysics3d/body/NarrowBoundingVolume.cpp new file mode 100644 index 00000000..bd8459b2 --- /dev/null +++ b/sources/reactphysics3d/body/NarrowBoundingVolume.cpp @@ -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 . * +***************************************************************************/ + +// Libraries +#include "NarrowBoundingVolume.h" + +// We want to use the ReactPhysics3D namespace +using namespace reactphysics3d; + +// Constructor +NarrowBoundingVolume::NarrowBoundingVolume() : BoundingVolume() { + +} + +// Destructor +NarrowBoundingVolume::~NarrowBoundingVolume() { + +} diff --git a/sources/reactphysics3d/body/NarrowBoundingVolume.h b/sources/reactphysics3d/body/NarrowBoundingVolume.h new file mode 100644 index 00000000..847cbc82 --- /dev/null +++ b/sources/reactphysics3d/body/NarrowBoundingVolume.h @@ -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 . * +***************************************************************************/ + +#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 \ No newline at end of file diff --git a/sources/reactphysics3d/body/OBB.cpp b/sources/reactphysics3d/body/OBB.cpp index 84beee31..6ca7258e 100644 --- a/sources/reactphysics3d/body/OBB.cpp +++ b/sources/reactphysics3d/body/OBB.cpp @@ -176,59 +176,30 @@ vector OBB::getExtremeVertices(const Vector3D& directionAxis) const { 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 OBB::getFace(unsigned int index) const throw(invalid_argument) { - // Check the argument - if (index >=0 && index <6) { - vector 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 // This method allocates a new OBB object and return a pointer to the new allocated OBB object OBB* OBB::computeFromVertices(const vector& vertices, const Vector3D& center) { // TODO : Implement this method; 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]); +} diff --git a/sources/reactphysics3d/body/OBB.h b/sources/reactphysics3d/body/OBB.h index 68581483..cc747e66 100644 --- a/sources/reactphysics3d/body/OBB.h +++ b/sources/reactphysics3d/body/OBB.h @@ -22,7 +22,7 @@ // Libraries #include -#include "BoundingVolume.h" +#include "NarrowBoundingVolume.h" #include "../mathematics/mathematics.h" // ReactPhysics3D namespace @@ -38,7 +38,7 @@ namespace reactphysics3d { of the box along the three axis of the OBB. ------------------------------------------------------------------- */ -class OBB : public BoundingVolume { +class OBB : public NarrowBoundingVolume { protected : Vector3D center; // Center point of the OBB 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 virtual std::vector 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 AABB* computeAABB() const; // Return the corresponding AABB virtual void draw() const; // Draw the OBB (only for testing purpose) static OBB* computeFromVertices(const std::vector& 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 OBB::getFace(unsigned int index) const throw(std::invalid_argument) { + // Check the argument + if (index >=0 && index <6) { + std::vector 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 inline double OBB::getExtent(unsigned int index) const throw(std::invalid_argument) { diff --git a/sources/reactphysics3d/body/RigidBody.cpp b/sources/reactphysics3d/body/RigidBody.cpp index d337d3d9..a221a20c 100644 --- a/sources/reactphysics3d/body/RigidBody.cpp +++ b/sources/reactphysics3d/body/RigidBody.cpp @@ -19,14 +19,16 @@ // Libraries #include "RigidBody.h" +#include "BroadBoundingVolume.h" +#include "NarrowBoundingVolume.h" // We want to use the ReactPhysics3D namespace using namespace reactphysics3d; // Constructor - RigidBody::RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal, BoundingVolume* broadBoundingVolume, - BoundingVolume* narrowBoundingVolume) - : Body(mass, broadBoundingVolume, narrowBoundingVolume), position(position), orientation(orientation.getUnit()), inertiaTensorLocal(inertiaTensorLocal), + RigidBody::RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal, + NarrowBoundingVolume* narrowBoundingVolume) + : Body(mass), position(position), orientation(orientation.getUnit()), inertiaTensorLocal(inertiaTensorLocal), inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass), oldPosition(position), oldOrientation(orientation) { restitution = 1.0; @@ -34,6 +36,12 @@ isCollisionEnabled = true; 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(); @@ -44,4 +52,11 @@ // Destructor RigidBody::~RigidBody() { -}; \ No newline at end of file +}; + +// 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); +} \ No newline at end of file diff --git a/sources/reactphysics3d/body/RigidBody.h b/sources/reactphysics3d/body/RigidBody.h index 340e4563..467faeab 100644 --- a/sources/reactphysics3d/body/RigidBody.h +++ b/sources/reactphysics3d/body/RigidBody.h @@ -15,7 +15,7 @@ * * * You should have received a copy of the GNU Lesser General Public License * * along with ReactPhysics3D. If not, see . * - ***************************************************************************/ +***************************************************************************/ #ifndef RIGIDBODY_H #define RIGIDBODY_H @@ -23,7 +23,6 @@ // Libraries #include #include "Body.h" -#include "BoundingVolume.h" #include "../mathematics/mathematics.h" // Namespace reactphysics3d @@ -54,9 +53,9 @@ class RigidBody : public Body { double restitution; // Coefficient of restitution (between 0 and 1), 1 for a very boucing body public : - RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensorLocal, - BoundingVolume* broadBoundingVolume, BoundingVolume* narrowBoundingVolume); // Constructor // Copy-constructor - virtual ~RigidBody(); // Destructor + RigidBody(const Vector3D& position, const Quaternion& orientation, double mass, + const Matrix3x3& inertiaTensorLocal, NarrowBoundingVolume* narrowBoundingVolume); // Constructor // Copy-constructor + virtual ~RigidBody(); // Destructor Vector3D getPosition() const; // Return the position of the body void setPosition(const Vector3D& position); // Set the position of the body @@ -190,7 +189,7 @@ inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const { // Set the interpolation factor of the body inline void RigidBody::setInterpolationFactor(double factor) { - //assert(factor >= 0.0 && factor <= 1.0); + assert(factor >= 0.0 && factor <= 1.0); // Set the factor interpolationFactor = factor; @@ -240,13 +239,6 @@ inline void RigidBody::updateOldPositionAndOrientation() { 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 #endif