2010-09-09 22:27:01 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
2013-03-02 15:26:18 +00:00
|
|
|
* Copyright (c) 2010-2013 Daniel Chappuis *
|
2010-09-09 22:27:01 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* 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. *
|
2010-09-09 22:27:01 +00:00
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* 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. *
|
2010-09-09 22:27:01 +00:00
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2013-04-18 20:54:36 +00:00
|
|
|
#ifndef REACTPHYSICS3D_COLLISION_SHAPE_H
|
|
|
|
#define REACTPHYSICS3D_COLLISION_SHAPE_H
|
2010-09-09 22:27:01 +00:00
|
|
|
|
|
|
|
// Libraries
|
2011-07-09 16:58:50 +00:00
|
|
|
#include <cassert>
|
2013-09-12 20:45:43 +00:00
|
|
|
#include <typeinfo>
|
2012-08-03 22:34:30 +00:00
|
|
|
#include "../../mathematics/Vector3.h"
|
|
|
|
#include "../../mathematics/Matrix3x3.h"
|
2014-07-21 21:08:18 +00:00
|
|
|
#include "../../mathematics/Ray.h"
|
2013-03-14 21:47:59 +00:00
|
|
|
#include "AABB.h"
|
2014-07-21 21:08:18 +00:00
|
|
|
#include "../RaycastInfo.h"
|
2014-04-11 21:50:00 +00:00
|
|
|
#include "../../memory/MemoryAllocator.h"
|
2010-09-09 22:27:01 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// ReactPhysics3D namespace
|
2010-09-09 22:27:01 +00:00
|
|
|
namespace reactphysics3d {
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Type of the collision shape
|
2013-07-15 17:09:07 +00:00
|
|
|
enum CollisionShapeType {BOX, SPHERE, CONE, CYLINDER, CAPSULE, CONVEX_MESH};
|
2010-09-09 22:27:01 +00:00
|
|
|
|
2011-07-09 16:58:50 +00:00
|
|
|
// Declarations
|
2014-04-11 21:50:00 +00:00
|
|
|
class CollisionBody;
|
|
|
|
class ProxyShape;
|
2011-07-09 16:58:50 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
// Class CollisionShape
|
|
|
|
/**
|
|
|
|
* This abstract class represents the collision shape associated with a
|
|
|
|
* body that is used during the narrow-phase collision detection.
|
|
|
|
*/
|
2012-08-03 22:34:30 +00:00
|
|
|
class CollisionShape {
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
protected :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Type of the collision shape
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShapeType mType;
|
2013-04-22 19:25:40 +00:00
|
|
|
|
|
|
|
/// Current number of similar created shapes
|
|
|
|
uint mNbSimilarCreatedShapes;
|
2013-07-03 20:50:00 +00:00
|
|
|
|
|
|
|
/// Margin used for the GJK collision detection algorithm
|
|
|
|
decimal mMargin;
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private copy-constructor
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape(const CollisionShape& shape);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private assignment operator
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape& operator=(const CollisionShape& shape);
|
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
public :
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Constructor
|
2013-07-03 20:50:00 +00:00
|
|
|
CollisionShape(CollisionShapeType type, decimal margin);
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Destructor
|
2012-10-09 20:21:02 +00:00
|
|
|
virtual ~CollisionShape();
|
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
/// Allocate and return a copy of the object
|
|
|
|
virtual CollisionShape* clone(void* allocatedMemory) const=0;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the type of the collision shapes
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShapeType getType() const;
|
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
/// Return the number of similar created shapes
|
|
|
|
uint getNbSimilarCreatedShapes() const;
|
|
|
|
|
2013-07-03 20:50:00 +00:00
|
|
|
/// Return the current object margin
|
|
|
|
decimal getMargin() const;
|
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
/// Return the number of bytes used by the collision shape
|
|
|
|
virtual size_t getSizeInBytes() const = 0;
|
|
|
|
|
2013-07-15 17:09:07 +00:00
|
|
|
/// Return the local bounds of the shape in x, y and z directions
|
|
|
|
virtual void getLocalBounds(Vector3& min, Vector3& max) const=0;
|
2013-03-03 15:24:46 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the local inertia tensor of the collision shapes
|
2012-10-09 20:21:02 +00:00
|
|
|
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const=0;
|
2013-03-14 21:47:59 +00:00
|
|
|
|
2014-04-11 21:50:00 +00:00
|
|
|
/// Compute the world-space AABB of the collision shape given a transform
|
|
|
|
virtual void computeAABB(AABB& aabb, const Transform& transform) const;
|
2013-04-22 19:25:40 +00:00
|
|
|
|
|
|
|
/// Increment the number of similar allocated collision shapes
|
|
|
|
void incrementNbSimilarCreatedShapes();
|
|
|
|
|
|
|
|
/// Decrement the number of similar allocated collision shapes
|
|
|
|
void decrementNbSimilarCreatedShapes();
|
|
|
|
|
|
|
|
/// Equality operator between two collision shapes.
|
|
|
|
bool operator==(const CollisionShape& otherCollisionShape) const;
|
|
|
|
|
|
|
|
/// Test equality between two collision shapes of the same type (same derived classes).
|
|
|
|
virtual bool isEqualTo(const CollisionShape& otherCollisionShape) const=0;
|
2014-04-11 21:50:00 +00:00
|
|
|
|
|
|
|
/// Create a proxy collision shape for the collision shape
|
|
|
|
virtual ProxyShape* createProxyShape(MemoryAllocator& allocator, CollisionBody* body,
|
2014-05-15 04:39:39 +00:00
|
|
|
const Transform& transform, decimal mass)=0;
|
2014-08-01 10:36:32 +00:00
|
|
|
|
|
|
|
/// Raycast method
|
|
|
|
virtual bool raycast(const Ray& ray, decimal distance = RAYCAST_INFINITY_DISTANCE) const=0;
|
|
|
|
|
|
|
|
/// Raycast method with feedback information
|
|
|
|
virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo,
|
|
|
|
decimal distance = RAYCAST_INFINITY_DISTANCE) const=0;
|
2014-04-11 21:50:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Class ProxyShape
|
|
|
|
/**
|
|
|
|
* The CollisionShape instances are supposed to be unique for memory optimization. For instance,
|
|
|
|
* consider two rigid bodies with the same sphere collision shape. In this situation, we will have
|
|
|
|
* a unique instance of SphereShape but we need to differentiate between the two instances during
|
|
|
|
* the collision detection. They do not have the same position in the world and they do not
|
|
|
|
* belong to the same rigid body. The ProxyShape class is used for that purpose by attaching a
|
|
|
|
* rigid body with one of its collision shape. A body can have multiple proxy shapes (one for
|
|
|
|
* each collision shape attached to the body).
|
|
|
|
*/
|
|
|
|
class ProxyShape {
|
|
|
|
|
2014-08-01 10:36:32 +00:00
|
|
|
protected:
|
2014-04-11 21:50:00 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
|
|
|
/// Pointer to the parent body
|
|
|
|
CollisionBody* mBody;
|
|
|
|
|
|
|
|
/// Local-space to parent body-space transform (does not change over time)
|
|
|
|
const Transform mLocalToBodyTransform;
|
|
|
|
|
|
|
|
/// Mass (in kilogramms) of the corresponding collision shape
|
|
|
|
decimal mMass;
|
|
|
|
|
|
|
|
/// Pointer to the next proxy shape of the body (linked list)
|
|
|
|
ProxyShape* mNext;
|
|
|
|
|
|
|
|
/// Broad-phase ID (node ID in the dynamic AABB tree)
|
|
|
|
int mBroadPhaseID;
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Private copy-constructor
|
|
|
|
ProxyShape(const ProxyShape& proxyShape);
|
|
|
|
|
|
|
|
/// Private assignment operator
|
|
|
|
ProxyShape& operator=(const ProxyShape& proxyShape);
|
|
|
|
|
2014-05-15 04:39:39 +00:00
|
|
|
/// Return the non-const collision shape
|
|
|
|
virtual CollisionShape* getInternalCollisionShape() const=0;
|
|
|
|
|
2014-04-11 21:50:00 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
ProxyShape(CollisionBody* body, const Transform& transform, decimal mass);
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
~ProxyShape();
|
|
|
|
|
|
|
|
/// Return the collision shape
|
|
|
|
virtual const CollisionShape* getCollisionShape() const=0;
|
|
|
|
|
|
|
|
/// Return the number of bytes used by the proxy collision shape
|
|
|
|
virtual size_t getSizeInBytes() const=0;
|
|
|
|
|
|
|
|
/// Return the parent body
|
|
|
|
CollisionBody* getBody() const;
|
|
|
|
|
|
|
|
/// Return the mass of the collision shape
|
|
|
|
decimal getMass() const;
|
|
|
|
|
|
|
|
/// Return the local to parent body transform
|
|
|
|
const Transform& getLocalToBodyTransform() const;
|
|
|
|
|
|
|
|
/// Return a local support point in a given direction with the object margin
|
|
|
|
virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction)=0;
|
|
|
|
|
|
|
|
/// Return a local support point in a given direction without the object margin
|
|
|
|
virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction)=0;
|
|
|
|
|
|
|
|
/// Return the current object margin
|
|
|
|
virtual decimal getMargin() const=0;
|
|
|
|
|
2014-08-01 10:36:32 +00:00
|
|
|
/// Return true if a point is inside the collision shape
|
|
|
|
virtual bool testPointInside(const Vector3& worldPoint)=0;
|
|
|
|
|
2014-07-21 21:08:18 +00:00
|
|
|
/// Raycast method
|
2014-08-01 10:36:32 +00:00
|
|
|
virtual bool raycast(const Ray& ray, decimal distance = RAYCAST_INFINITY_DISTANCE) const=0;
|
2014-07-21 21:08:18 +00:00
|
|
|
|
|
|
|
/// Raycast method with feedback information
|
|
|
|
virtual bool raycast(const Ray& ray, RaycastInfo& raycastInfo,
|
2014-08-01 10:36:32 +00:00
|
|
|
decimal distance = RAYCAST_INFINITY_DISTANCE) const=0;
|
2014-07-21 21:08:18 +00:00
|
|
|
|
2014-04-11 21:50:00 +00:00
|
|
|
// -------------------- Friendship -------------------- //
|
|
|
|
|
|
|
|
friend class OverlappingPair;
|
|
|
|
friend class CollisionBody;
|
|
|
|
friend class RigidBody;
|
|
|
|
friend class BroadPhaseAlgorithm;
|
|
|
|
friend class DynamicAABBTree;
|
2014-05-15 04:39:39 +00:00
|
|
|
friend class CollisionDetection;
|
2010-09-09 22:27:01 +00:00
|
|
|
};
|
|
|
|
|
2012-08-03 22:34:30 +00:00
|
|
|
// Return the type of the collision shape
|
|
|
|
inline CollisionShapeType CollisionShape::getType() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mType;
|
2013-03-14 21:47:59 +00:00
|
|
|
}
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
// Return the number of similar created shapes
|
|
|
|
inline uint CollisionShape::getNbSimilarCreatedShapes() const {
|
|
|
|
return mNbSimilarCreatedShapes;
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:50:00 +00:00
|
|
|
// Return the current object margin
|
|
|
|
inline decimal CollisionShape::getMargin() const {
|
|
|
|
return mMargin;
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
// Increment the number of similar allocated collision shapes
|
|
|
|
inline void CollisionShape::incrementNbSimilarCreatedShapes() {
|
|
|
|
mNbSimilarCreatedShapes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the number of similar allocated collision shapes
|
|
|
|
inline void CollisionShape::decrementNbSimilarCreatedShapes() {
|
|
|
|
mNbSimilarCreatedShapes--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equality operator between two collision shapes.
|
|
|
|
/// This methods returns true only if the two collision shapes are of the same type and
|
|
|
|
/// of the same dimensions.
|
|
|
|
inline bool CollisionShape::operator==(const CollisionShape& otherCollisionShape) const {
|
|
|
|
|
|
|
|
// If the two collisions shapes are not of the same type (same derived classes)
|
|
|
|
// we return false
|
|
|
|
if (mType != otherCollisionShape.mType) return false;
|
|
|
|
|
|
|
|
assert(typeid(*this) == typeid(otherCollisionShape));
|
|
|
|
|
2013-07-03 20:50:00 +00:00
|
|
|
if (mMargin != otherCollisionShape.mMargin) return false;
|
|
|
|
|
2013-04-22 19:25:40 +00:00
|
|
|
// Check if the two shapes are equal
|
|
|
|
return otherCollisionShape.isEqualTo(*this);
|
|
|
|
}
|
|
|
|
|
2014-04-11 21:50:00 +00:00
|
|
|
// Return the parent body
|
|
|
|
inline CollisionBody* ProxyShape::getBody() const {
|
|
|
|
return mBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the mass of the collision shape
|
|
|
|
inline decimal ProxyShape::getMass() const {
|
|
|
|
return mMass;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the local to parent body transform
|
|
|
|
inline const Transform& ProxyShape::getLocalToBodyTransform() const {
|
|
|
|
return mLocalToBodyTransform;
|
|
|
|
}
|
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 22:34:30 +00:00
|
|
|
#endif
|