From 0ddec3f842e4b1fb597c7b3427ff3e26c1cbc8c7 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 27 Aug 2015 22:31:05 +0200 Subject: [PATCH] Start working on triangular meshes collision detection --- CMakeLists.txt | 10 + src/collision/TriangleMesh.cpp | 39 ++++ src/collision/TriangleMesh.h | 87 ++++++++ src/collision/TriangleVertexArray.cpp | 45 +++++ src/collision/TriangleVertexArray.h | 84 ++++++++ .../narrowphase/ConcaveVsConvexAlgorithm.cpp | 27 +++ .../narrowphase/ConcaveVsConvexAlgorithm.h | 71 +++++++ src/collision/shapes/BoxShape.h | 2 +- src/collision/shapes/CapsuleShape.h | 2 +- src/collision/shapes/CollisionShape.h | 4 +- src/collision/shapes/ConcaveMeshShape.cpp | 48 +++++ src/collision/shapes/ConcaveMeshShape.h | 172 ++++++++++++++++ src/collision/shapes/ConeShape.h | 2 +- src/collision/shapes/ConvexMeshShape.h | 5 +- src/collision/shapes/CylinderShape.h | 2 +- src/collision/shapes/SphereShape.h | 2 +- src/collision/shapes/TriangleShape.cpp | 69 +++++++ src/collision/shapes/TriangleShape.h | 191 ++++++++++++++++++ testbed/CMakeLists.txt | 2 +- testbed/common/Capsule.cpp | 2 +- testbed/common/Sphere.cpp | 2 +- 21 files changed, 857 insertions(+), 11 deletions(-) create mode 100644 src/collision/TriangleMesh.cpp create mode 100644 src/collision/TriangleMesh.h create mode 100644 src/collision/TriangleVertexArray.cpp create mode 100644 src/collision/TriangleVertexArray.h create mode 100644 src/collision/narrowphase/ConcaveVsConvexAlgorithm.cpp create mode 100644 src/collision/narrowphase/ConcaveVsConvexAlgorithm.h create mode 100644 src/collision/shapes/ConcaveMeshShape.cpp create mode 100644 src/collision/shapes/ConcaveMeshShape.h create mode 100644 src/collision/shapes/TriangleShape.cpp create mode 100644 src/collision/shapes/TriangleShape.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 52efef81..ee484a6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,8 @@ SET (REACTPHYSICS3D_SOURCES "src/collision/narrowphase/NarrowPhaseAlgorithm.cpp" "src/collision/narrowphase/SphereVsSphereAlgorithm.h" "src/collision/narrowphase/SphereVsSphereAlgorithm.cpp" + "src/collision/narrowphase/ConcaveVsConvexAlgorithm.h" + "src/collision/narrowphase/ConcaveVsConvexAlgorithm.cpp" "src/collision/shapes/AABB.h" "src/collision/shapes/AABB.cpp" "src/collision/shapes/BoxShape.h" @@ -88,10 +90,18 @@ SET (REACTPHYSICS3D_SOURCES "src/collision/shapes/CylinderShape.cpp" "src/collision/shapes/SphereShape.h" "src/collision/shapes/SphereShape.cpp" + "src/collision/shapes/TriangleShape.h" + "src/collision/shapes/TriangleShape.cpp" + "src/collision/shapes/ConcaveMeshShape.h" + "src/collision/shapes/ConcaveMeshShape.cpp" "src/collision/RaycastInfo.h" "src/collision/RaycastInfo.cpp" "src/collision/ProxyShape.h" "src/collision/ProxyShape.cpp" + "src/collision/TriangleVertexArray.h" + "src/collision/TriangleVertexArray.cpp" + "src/collision/TriangleMesh.h" + "src/collision/TriangleMesh.cpp" "src/collision/CollisionDetection.h" "src/collision/CollisionDetection.cpp" "src/constraint/BallAndSocketJoint.h" diff --git a/src/collision/TriangleMesh.cpp b/src/collision/TriangleMesh.cpp new file mode 100644 index 00000000..8ae2c0b9 --- /dev/null +++ b/src/collision/TriangleMesh.cpp @@ -0,0 +1,39 @@ +/******************************************************************************** +* 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 "TriangleMesh.h" + +using namespace reactphysics3d; + +// Constructor +TriangleMesh::TriangleMesh() { + +} + +// Destructor +TriangleMesh::~TriangleMesh() { + +} diff --git a/src/collision/TriangleMesh.h b/src/collision/TriangleMesh.h new file mode 100644 index 00000000..7b6f29a8 --- /dev/null +++ b/src/collision/TriangleMesh.h @@ -0,0 +1,87 @@ +/******************************************************************************** +* 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 REACTPHYSICS3D_TRIANGLE_MESH_H +#define REACTPHYSICS3D_TRIANGLE_MESH_H + +// Libraries +#include +#include "TriangleVertexArray.h" + +namespace reactphysics3d { + +// Class TriangleMesh +/** + * This class represents a mesh made of triangles. A TriangleMesh contains + * one or several parts. Each part is a set of triangles represented in a + * TriangleVertexArray object describing all the triangles vertices of the part. + * A TriangleMesh object is used to create a ConcaveMeshShape from a triangle + * mesh for instance. + */ +class TriangleMesh { + + protected: + + /// All the triangle arrays of the mesh (one triangle array per part) + std::vector mTriangleArrays; + + public: + + /// Constructor + TriangleMesh(); + + /// Destructor + ~TriangleMesh(); + + /// Add a subpart of the mesh + void addSubpart(TriangleVertexArray* triangleVertexArray); + + /// Return a pointer to a given subpart (triangle vertex array) of the mesh + TriangleVertexArray* getSubpart(uint indexSubpart) const; + + /// Return the number of subparts of the mesh + uint getNbSubparts() const; +}; + +// Add a subpart of the mesh +inline void TriangleMesh::addSubpart(TriangleVertexArray* triangleVertexArray) { + mTriangleArrays.push_back(triangleVertexArray ); +} + +// Return a pointer to a given subpart (triangle vertex array) of the mesh +inline TriangleVertexArray* TriangleMesh::getSubpart(uint indexSubpart) const { + assert(indexSubpart < mTriangleArrays.size()); + return mTriangleArrays[indexSubpart]; +} + +// Return the number of subparts of the mesh +inline uint TriangleMesh::getNbSubparts() const { + return mTriangleArrays.size(); +} + +} + +#endif + diff --git a/src/collision/TriangleVertexArray.cpp b/src/collision/TriangleVertexArray.cpp new file mode 100644 index 00000000..7f6c2c24 --- /dev/null +++ b/src/collision/TriangleVertexArray.cpp @@ -0,0 +1,45 @@ +/******************************************************************************** +* 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 "TriangleVertexArray.h" + +using namespace reactphysics3d; + +// Constructor +TriangleVertexArray::TriangleVertexArray(uint nbVertices, void* verticesStart, int verticesStride, + uint nbTriangles, void* indexesStart, int indexesStride) { + mNbVertices = nbVertices; + mVerticesStart = reinterpret_cast(verticesStart); + mVerticesStride = verticesStride; + mNbTriangles = nbTriangles; + mIndexesStart = reinterpret_cast(indexesStart); + mIndexesStride = indexesStride; +} + +// Destructor +TriangleVertexArray::~TriangleVertexArray() { + +} diff --git a/src/collision/TriangleVertexArray.h b/src/collision/TriangleVertexArray.h new file mode 100644 index 00000000..c6001277 --- /dev/null +++ b/src/collision/TriangleVertexArray.h @@ -0,0 +1,84 @@ +/******************************************************************************** +* 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 REACTPHYSICS3D_TRIANGLE_VERTEX_ARRAY_H +#define REACTPHYSICS3D_TRIANGLE_VERTEX_ARRAY_H + +// Libraries + +namespace reactphysics3d { + +// Class TriangleVertexArray +/** + * This class is used to describe the vertices and faces of a triangular mesh. + * A TriangleVertexArray represents a continuous array of vertices and indexes + * of a triangular mesh. When you create a TriangleVertexArray, no data is copied + * into the array. It only stores pointer to the da. The purpose is to allow + * the user to share vertices data between the physics engine and the rendering + * part. Therefore, make sure that the data pointed by a TriangleVertexArray + * remain valid during the TriangleVertexArray life. + */ +class TriangleVertexArray { + + protected: + + /// Number of vertices in the array + uint mNbVertices; + + /// Pointer to the first vertex value in the array + unsigned char* mVerticesStart; + + /// Stride (number of bytes) between the beginning of two vertices + /// values in the array + int mVerticesStride; + + /// Number of triangles in the array + uint mNbTriangles; + + /// Pointer to the first vertex index of the array + unsigned char* mIndexesStart; + + /// Stride (number of bytes) between the beginning of two indexes in + /// the array + int mIndexesStride; + + public: + + /// Constructor + TriangleVertexArray(uint nbVertices, void* verticesStart, int verticesStride, + uint nbTriangles, void* indexesStart, int indexesStride); + + /// Destructor + ~TriangleVertexArray(); + + + + +}; + +} + +#endif + diff --git a/src/collision/narrowphase/ConcaveVsConvexAlgorithm.cpp b/src/collision/narrowphase/ConcaveVsConvexAlgorithm.cpp new file mode 100644 index 00000000..47f09139 --- /dev/null +++ b/src/collision/narrowphase/ConcaveVsConvexAlgorithm.cpp @@ -0,0 +1,27 @@ +/******************************************************************************** +* 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 "ConcaveVsConvexAlgorithm.h" diff --git a/src/collision/narrowphase/ConcaveVsConvexAlgorithm.h b/src/collision/narrowphase/ConcaveVsConvexAlgorithm.h new file mode 100644 index 00000000..4e637415 --- /dev/null +++ b/src/collision/narrowphase/ConcaveVsConvexAlgorithm.h @@ -0,0 +1,71 @@ +/******************************************************************************** +* 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 REACTPHYSICS3D_CONCAVE_VS_CONVEX_ALGORITHM_H +#define REACTPHYSICS3D_CONCAVE_VS_CONVEX_ALGORITHM_H + +// Libraries + +/// Namespace ReactPhysics3D +namespace reactphysics3d { + +// Class ConcaveVsConvexAlgorithm +/** + * This class is used to compute the narrow-phase collision detection + * between a concave collision shape and a convex collision shape. The idea is + * to use the GJK collision detection algorithm to compute the collision between + * the convex shape and each of the triangles in the concave shape. + */ +class ConcaveVsConvexAlgorithm : public NarrowPhaseAlgorithm { + + protected : + + // -------------------- Methods -------------------- // + + /// Private copy-constructor + ConcaveVsConvexAlgorithm(const ConcaveVsConvexAlgorithm& algorithm); + + /// Private assignment operator + ConcaveVsConvexAlgorithm& operator=(const ConcaveVsConvexAlgorithm& algorithm); + + public : + + // -------------------- Methods -------------------- // + + /// Constructor + ConcaveVsConvexAlgorithm(); + + /// Destructor + virtual ~ConcaveVsConvexAlgorithm(); + + /// Return true and compute a contact info if the two bounding volume collide + virtual bool testCollision(ProxyShape* collisionShape1, ProxyShape* collisionShape2, + ContactPointInfo*& contactInfo); +}; + +} + +#endif + diff --git a/src/collision/shapes/BoxShape.h b/src/collision/shapes/BoxShape.h index 9f7e49ec..de7e1540 100644 --- a/src/collision/shapes/BoxShape.h +++ b/src/collision/shapes/BoxShape.h @@ -52,7 +52,7 @@ namespace reactphysics3d { */ class BoxShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/CapsuleShape.h b/src/collision/shapes/CapsuleShape.h index 240f73cb..f4cc2625 100644 --- a/src/collision/shapes/CapsuleShape.h +++ b/src/collision/shapes/CapsuleShape.h @@ -46,7 +46,7 @@ namespace reactphysics3d { */ class CapsuleShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/CollisionShape.h b/src/collision/shapes/CollisionShape.h index d2aeabe7..5e3663bf 100644 --- a/src/collision/shapes/CollisionShape.h +++ b/src/collision/shapes/CollisionShape.h @@ -40,8 +40,8 @@ namespace reactphysics3d { /// Type of the collision shape -enum CollisionShapeType {BOX, SPHERE, CONE, CYLINDER, CAPSULE, CONVEX_MESH}; -const int NB_COLLISION_SHAPE_TYPES = 6; +enum CollisionShapeType {TRIANGLE, BOX, SPHERE, CONE, CYLINDER, CAPSULE, CONVEX_MESH, CONCAVE_MESH}; +const int NB_COLLISION_SHAPE_TYPES = 8; // Declarations class ProxyShape; diff --git a/src/collision/shapes/ConcaveMeshShape.cpp b/src/collision/shapes/ConcaveMeshShape.cpp new file mode 100644 index 00000000..336d0b05 --- /dev/null +++ b/src/collision/shapes/ConcaveMeshShape.cpp @@ -0,0 +1,48 @@ +/******************************************************************************** +* 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 "ConcaveMeshShape.h" + +using namespace reactphysics3d; + +// Constructor +ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* triangleMesh) { + +} + +// Destructor +ConcaveMeshShape::~ConcaveMeshShape() { + +} + +// Raycast method with feedback information +bool ConcaveMeshShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const { + + + // TODO : Implement this + + return false; +} diff --git a/src/collision/shapes/ConcaveMeshShape.h b/src/collision/shapes/ConcaveMeshShape.h new file mode 100644 index 00000000..a9deb29e --- /dev/null +++ b/src/collision/shapes/ConcaveMeshShape.h @@ -0,0 +1,172 @@ +/******************************************************************************** +* 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 REACTPHYSICS3D_CONCAVE_MESH_SHAPE_H +#define REACTPHYSICS3D_CONCAVE_MESH_SHAPE_H + +namespace reactphysics3d { + +// TODO : Implement raycasting with this collision shape + +// Class ConcaveMeshShape +/** + * This class represents a concave mesh shape. Note that collision detection + * with a concave mesh shape can be very expensive. You should use only use + * this shape for a static mesh. + */ +class ConcaveMeshShape : public CollisionShape { + + protected: + + // -------------------- Methods -------------------- // + + /// Private copy-constructor + ConcaveMeshShape(const ConcaveMeshShape& shape); + + /// Private assignment operator + ConcaveMeshShape& operator=(const ConcaveMeshShape& shape); + + /// Return a local support point in a given direction with the object margin + virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction, + void** cachedCollisionData) const; + + /// Return a local support point in a given direction without the object margin + virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction, + void** cachedCollisionData) const; + + /// Return true if a point is inside the collision shape + virtual bool testPointInside(const Vector3& localPoint, ProxyShape* proxyShape) const; + + /// Raycast method with feedback information + virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const; + + /// Allocate and return a copy of the object + virtual ConcaveMeshShape* clone(void* allocatedMemory) const; + + /// Return the number of bytes used by the collision shape + virtual size_t getSizeInBytes() const; + + public: + + /// Constructor + ConcaveMeshShape(TriangleMesh* triangleMesh); + + /// Destructor + ~ConcaveMeshShape(); + + /// Return the local bounds of the shape in x, y and z directions. + virtual void getLocalBounds(Vector3& min, Vector3& max) const; + + /// Return the local inertia tensor of the collision shape + virtual void computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const; + + /// Update the AABB of a body using its collision shape + virtual void computeAABB(AABB& aabb, const Transform& transform); + + /// Test equality between two sphere shapes + virtual bool isEqualTo(const CollisionShape& otherCollisionShape) const; +}; + +/// Allocate and return a copy of the object +inline ConcaveMeshShape* ConcaveMeshShape::clone(void* allocatedMemory) const { + return new (allocatedMemory) ConcaveMeshShape(*this); +} + +// Return the number of bytes used by the collision shape +inline size_t ConcaveMeshShape::getSizeInBytes() const { + return sizeof(ConcaveMeshShape); +} + +// Return a local support point in a given direction with the object margin +inline Vector3 ConcaveMeshShape::getLocalSupportPointWithMargin(const Vector3& direction, + void** cachedCollisionData) const { + + // TODO : Implement this + return Vector3(0, 0, 0); +} + +// Return a local support point in a given direction without the object margin +inline Vector3 ConcaveMeshShape::getLocalSupportPointWithoutMargin(const Vector3& direction, + void** cachedCollisionData) const { + // TODO : Implement this + return Vector3(0.0, 0.0, 0.0); +} + +// Return the local bounds of the shape in x, y and z directions. +// This method is used to compute the AABB of the box +/** + * @param min The minimum bounds of the shape in local-space coordinates + * @param max The maximum bounds of the shape in local-space coordinates + */ +inline void ConcaveMeshShape::getLocalBounds(Vector3& min, Vector3& max) const { + + // TODO : Implement this +} + +// Return the local inertia tensor of the sphere +/** + * @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space + * coordinates + * @param mass Mass to use to compute the inertia tensor of the collision shape + */ +inline void ConcaveMeshShape::computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const { + + // TODO : Implement this + decimal diag = decimal(0.4) * mass * mRadius * mRadius; + tensor.setAllValues(diag, 0.0, 0.0, + 0.0, diag, 0.0, + 0.0, 0.0, diag); +} + +// Update the AABB of a body using its collision shape +/** + * @param[out] aabb The axis-aligned bounding box (AABB) of the collision shape + * computed in world-space coordinates + * @param transform Transform used to compute the AABB of the collision shape + */ +inline void ConcaveMeshShape::computeAABB(AABB& aabb, const Transform& transform) { + + // TODO : Implement this +} + +// Test equality between two sphere shapes +inline bool ConcaveMeshShape::isEqualTo(const CollisionShape& otherCollisionShape) const { + const ConcaveMeshShape& otherShape = dynamic_cast(otherCollisionShape); + + // TODO : Implement this + + return false; +} + +// Return true if a point is inside the collision shape +inline bool ConcaveMeshShape::testPointInside(const Vector3& localPoint, ProxyShape* proxyShape) const { + + // TODO : Implement this + return false; +} + +} +#endif + diff --git a/src/collision/shapes/ConeShape.h b/src/collision/shapes/ConeShape.h index f7aec26e..9d0d9db3 100644 --- a/src/collision/shapes/ConeShape.h +++ b/src/collision/shapes/ConeShape.h @@ -51,7 +51,7 @@ namespace reactphysics3d { */ class ConeShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/ConvexMeshShape.h b/src/collision/shapes/ConvexMeshShape.h index 3f0b366e..7245255b 100644 --- a/src/collision/shapes/ConvexMeshShape.h +++ b/src/collision/shapes/ConvexMeshShape.h @@ -41,6 +41,9 @@ namespace reactphysics3d { // Declaration class CollisionWorld; +// TODO : Make possible to create a ConvexMeshShape using a TriangleMesh as for +// the ConcaveMeshShape + // Class ConvexMeshShape /** * This class represents a convex mesh shape. In order to create a convex mesh shape, you @@ -59,7 +62,7 @@ class CollisionWorld; */ class ConvexMeshShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/CylinderShape.h b/src/collision/shapes/CylinderShape.h index 5b381296..0e193d39 100644 --- a/src/collision/shapes/CylinderShape.h +++ b/src/collision/shapes/CylinderShape.h @@ -51,7 +51,7 @@ namespace reactphysics3d { */ class CylinderShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/SphereShape.h b/src/collision/shapes/SphereShape.h index d49db1ef..ac3e3cf2 100644 --- a/src/collision/shapes/SphereShape.h +++ b/src/collision/shapes/SphereShape.h @@ -44,7 +44,7 @@ namespace reactphysics3d { */ class SphereShape : public CollisionShape { - private : + protected : // -------------------- Attributes -------------------- // diff --git a/src/collision/shapes/TriangleShape.cpp b/src/collision/shapes/TriangleShape.cpp new file mode 100644 index 00000000..9537c2ce --- /dev/null +++ b/src/collision/shapes/TriangleShape.cpp @@ -0,0 +1,69 @@ +/******************************************************************************** +* 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 "TriangleShape.h" +#include "collision/ProxyShape.h" +#include "configuration.h" +#include + +using namespace reactphysics3d; + +// Constructor +/** + * @param point1 First point of the triangle + * @param point2 Second point of the triangle + * @param point3 Third point of the triangle + * @param margin The collision margin (in meters) around the collision shape + */ +TriangleShape::TriangleShape(const Vector3& point1, const Vector3& point2, + const Vector3& point3, decimal margin) + : CollisionShape(TRIANGLE, margin) { + mPoints[0] = point1; + mPoints[1] = point2; + mPoints[2] = point3; +} + +// Private copy-constructor +TriangleShape::TriangleShape(const TriangleShape& shape) + : CollisionShape(shape) { + mPoints[0] = shape.mPoints[0]; + mPoints[1] = shape.mPoints[1]; + mPoints[2] = shape.mPoints[2]; +} + +// Destructor +TriangleShape::~TriangleShape() { + +} + +// Raycast method with feedback information +bool TriangleShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const { + + // TODO : Implement this + + return false; +} + diff --git a/src/collision/shapes/TriangleShape.h b/src/collision/shapes/TriangleShape.h new file mode 100644 index 00000000..f56158f1 --- /dev/null +++ b/src/collision/shapes/TriangleShape.h @@ -0,0 +1,191 @@ +/******************************************************************************** +* 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 REACTPHYSICS3D_TRIANGLE_SHAPE_H +#define REACTPHYSICS3D_TRIANGLE_SHAPE_H + +// Libraries +#include "mathematics/mathematics.h" +#include "CollisionShape.h" + +/// ReactPhysics3D namespace +namespace reactphysics3d { + +// Class TriangleShape +/** + * This class represents a triangle collision shape that is centered + * at the origin and defined three points. + */ +class TriangleShape : public CollisionShape { + + protected: + + // -------------------- Attribute -------------------- // + + /// Three points of the triangle + Vector3 mPoints[3]; + + // -------------------- Methods -------------------- // + + /// Private copy-constructor + TriangleShape(const TriangleShape& shape); + + /// Private assignment operator + TriangleShape& operator=(const TriangleShape& shape); + + /// Return a local support point in a given direction with the object margin + virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction, + void** cachedCollisionData) const; + + /// Return a local support point in a given direction without the object margin + virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction, + void** cachedCollisionData) const; + + /// Return true if a point is inside the collision shape + virtual bool testPointInside(const Vector3& localPoint, ProxyShape* proxyShape) const; + + /// Raycast method with feedback information + virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const; + + /// Allocate and return a copy of the object + virtual TriangleShape* clone(void* allocatedMemory) const; + + /// Return the number of bytes used by the collision shape + virtual size_t getSizeInBytes() const; + + public: + + // -------------------- Methods -------------------- // + + /// Constructor + TriangleShape(const Vector3& point1, const Vector3& point2, + const Vector3& point3, decimal margin); + + /// Destructor + virtual ~TriangleShape(); + + /// Return the local bounds of the shape in x, y and z directions. + virtual void getLocalBounds(Vector3& min, Vector3& max) const; + + /// Return the local inertia tensor of the collision shape + virtual void computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const; + + /// Update the AABB of a body using its collision shape + virtual void computeAABB(AABB& aabb, const Transform& transform); + + /// Test equality between two triangle shapes + virtual bool isEqualTo(const CollisionShape& otherCollisionShape) const; +}; + +/// Allocate and return a copy of the object +inline TriangleShape* TriangleShape::clone(void* allocatedMemory) const { + return new (allocatedMemory) TriangleShape(*this); +} + +// Return the number of bytes used by the collision shape +inline size_t TriangleShape::getSizeInBytes() const { + return sizeof(TriangleShape); +} + +// Return a local support point in a given direction with the object margin +inline Vector3 TriangleShape::getLocalSupportPointWithMargin(const Vector3& direction, + void** cachedCollisionData) const { + + // TODO : Do we need to use margin for triangle support point ? + + return getLocalSupportPointWithoutMargin(direction, cachedCollisionData); +} + +// Return a local support point in a given direction without the object margin +inline Vector3 TriangleShape::getLocalSupportPointWithoutMargin(const Vector3& direction, + void** cachedCollisionData) const { + Vector3 dotProducts(direction.dot(mPoints[0]), direction.dot(mPoints[1], direction.dot(mPoints[2]))); + return mPoints[dotProducts.getMaxAxis()]; +} + +// Return the local bounds of the shape in x, y and z directions. +// This method is used to compute the AABB of the box +/** + * @param min The minimum bounds of the shape in local-space coordinates + * @param max The maximum bounds of the shape in local-space coordinates + */ +inline void TriangleShape::getLocalBounds(Vector3& min, Vector3& max) const { + + // TODO :This code is wrong + const Vector3 xAxis(worldPoint1.X, worldPoint2.X, worldPoint3.X); + const Vector3 yAxis(worldPoint1.Y, worldPoint2.Y, worldPoint3.Y); + const Vector3 zAxis(worldPoint1.Z, worldPoint2.Z, worldPoint3.Z); + min.setAllValues(xAxis.getMinAxis(), yAxis.getMinAxis(), zAxis.getMinAxis()); + max.setAllValues(xAxis.getMaxAxis(), yAxis.getMaxAxis(), zAxis.getMaxAxis()); +} + +// Return the local inertia tensor of the triangle shape +/** + * @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space + * coordinates + * @param mass Mass to use to compute the inertia tensor of the collision shape + */ +inline void TriangleShape::computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const { + tensor.setToZero(); +} + +// Update the AABB of a body using its collision shape +/** + * @param[out] aabb The axis-aligned bounding box (AABB) of the collision shape + * computed in world-space coordinates + * @param transform Transform used to compute the AABB of the collision shape + */ +inline void TriangleShape::computeAABB(AABB& aabb, const Transform& transform) { + + // TODO :This code is wrong + + const Vector3 worldPoint1 = transform * mPoints[0]; + const Vector3 worldPoint2 = transform * mPoints[1]; + const Vector3 worldPoint3 = transform * mPoints[2]; + + const Vector3 xAxis(worldPoint1.X, worldPoint2.X, worldPoint3.X); + const Vector3 yAxis(worldPoint1.Y, worldPoint2.Y, worldPoint3.Y); + const Vector3 zAxis(worldPoint1.Z, worldPoint2.Z, worldPoint3.Z); + aabb.setMin(Vector3(xAxis.getMinAxis(), yAxis.getMinAxis(), zAxis.getMinAxis())); + aabb.setMax(Vector3(xAxis.getMaxAxis(), yAxis.getMaxAxis(), zAxis.getMaxAxis())); +} + +// Test equality between two triangle shapes +inline bool TriangleShape::isEqualTo(const CollisionShape& otherCollisionShape) const { + const TriangleShape& otherShape = dynamic_cast(otherCollisionShape); + return (mPoints[0] == otherShape.mPoints[0] && + mPoints[1] == otherShape.mPoints[1] && + mPoints[2] == otherShape.mPoints[2]); +} + +// Return true if a point is inside the collision shape +inline bool TriangleShape::testPointInside(const Vector3& localPoint, ProxyShape* proxyShape) const { + return false; +} + +} + +#endif + diff --git a/testbed/CMakeLists.txt b/testbed/CMakeLists.txt index b81a6fec..24a1a8cb 100644 --- a/testbed/CMakeLists.txt +++ b/testbed/CMakeLists.txt @@ -26,7 +26,7 @@ FILE(COPY "imgui/DroidSans.ttf" DESTINATION "${EXECUTABLE_OUTPUT_PATH}") SET(CMAKE_CXX_FLAGS "-Wall -std=c++0x") # Headers -INCLUDE_DIRECTORIES(${GLEW_INCLUDE_PATH} "src/" "opengl-framework/src/" "glfw/include/" "common/" "scenes/" "imgui/") +INCLUDE_DIRECTORIES("src/" "opengl-framework/src/" "glfw/include/" "common/" "scenes/" "imgui/") # Testbed source files SET(TESTBED_SOURCES diff --git a/testbed/common/Capsule.cpp b/testbed/common/Capsule.cpp index b65146d4..77bfe437 100644 --- a/testbed/common/Capsule.cpp +++ b/testbed/common/Capsule.cpp @@ -238,7 +238,7 @@ void Capsule::createVBOAndVAO() { // Create th VBO for the indices data mVBOIndices.create(); mVBOIndices.bind(); - size_t sizeIndices = mIndices[0].size() * sizeof(uint); + size_t sizeIndices = mIndices[0].size() * sizeof(unsigned int); mVBOIndices.copyDataIntoVBO(sizeIndices, getIndicesPointer(), GL_STATIC_DRAW); mVBOIndices.unbind(); diff --git a/testbed/common/Sphere.cpp b/testbed/common/Sphere.cpp index 61fe9671..783478ce 100644 --- a/testbed/common/Sphere.cpp +++ b/testbed/common/Sphere.cpp @@ -236,7 +236,7 @@ void Sphere::createVBOAndVAO() { // Create th VBO for the indices data mVBOIndices.create(); mVBOIndices.bind(); - size_t sizeIndices = mIndices[0].size() * sizeof(uint); + size_t sizeIndices = mIndices[0].size() * sizeof(unsigned int); mVBOIndices.copyDataIntoVBO(sizeIndices, getIndicesPointer(), GL_STATIC_DRAW); mVBOIndices.unbind();