2012-09-18 20:09:49 +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 *
|
2012-09-18 20:09:49 +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. *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2013-04-18 20:54:36 +00:00
|
|
|
#ifndef REACTPHYSICS3D_COLLISION_BODY_H
|
|
|
|
#define REACTPHYSICS3D_COLLISION_BODY_H
|
2012-09-18 20:09:49 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cassert>
|
|
|
|
#include "Body.h"
|
|
|
|
#include "../mathematics/Transform.h"
|
|
|
|
#include "../collision/shapes/AABB.h"
|
|
|
|
#include "../collision/shapes/CollisionShape.h"
|
|
|
|
#include "../configuration.h"
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Namespace reactphysics3d
|
2012-09-18 20:09:49 +00:00
|
|
|
namespace reactphysics3d {
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
// Class CollisionBody
|
|
|
|
/**
|
|
|
|
* This class represents a body that is able to collide with others
|
|
|
|
* bodies. This class inherits from the Body class.
|
|
|
|
*/
|
2012-09-18 20:09:49 +00:00
|
|
|
class CollisionBody : public Body {
|
|
|
|
|
|
|
|
protected :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Collision shape of the body
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape* mCollisionShape;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Position and orientation of the body
|
2012-10-09 20:21:02 +00:00
|
|
|
Transform mTransform;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Last position and orientation of the body
|
2012-10-09 20:21:02 +00:00
|
|
|
Transform mOldTransform;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Interpolation factor used for the state interpolation
|
2012-10-09 20:21:02 +00:00
|
|
|
decimal mInterpolationFactor;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// True if the body is active (not sleeping)
|
2012-10-09 20:21:02 +00:00
|
|
|
bool mIsActive;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// True if the body is able to move
|
2012-10-09 20:21:02 +00:00
|
|
|
bool mIsMotionEnabled;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// True if the body can collide with others bodies
|
2012-10-09 20:21:02 +00:00
|
|
|
bool mIsCollisionEnabled;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// AABB for Broad-Phase collision detection
|
2013-03-14 21:47:59 +00:00
|
|
|
AABB mAabb;
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// True if the body has moved during the last frame
|
2012-10-09 20:21:02 +00:00
|
|
|
bool mHasMoved;
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private copy-constructor
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionBody(const CollisionBody& body);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private assignment operator
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionBody& operator=(const CollisionBody& body);
|
2012-09-18 20:09:49 +00:00
|
|
|
|
|
|
|
public :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Constructor
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionBody(const Transform& transform, CollisionShape* collisionShape, bodyindex id);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Destructor
|
2012-10-09 20:21:02 +00:00
|
|
|
virtual ~CollisionBody();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return true if the body has moved during the last frame
|
2012-10-09 20:21:02 +00:00
|
|
|
bool getHasMoved() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the hasMoved variable (true if the body has moved during the last frame)
|
2012-10-09 20:21:02 +00:00
|
|
|
void setHasMoved(bool hasMoved);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the collision shape
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape* getCollisionShape() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the collision shape
|
2012-10-09 20:21:02 +00:00
|
|
|
void setCollisionShape(CollisionShape* collisionShape);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return true for an active body
|
2012-10-09 20:21:02 +00:00
|
|
|
bool getIsActive() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the isActive variable
|
2012-10-09 20:21:02 +00:00
|
|
|
void setIsActive(bool isActive);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the current position and orientation
|
2012-10-09 20:21:02 +00:00
|
|
|
const Transform& getTransform() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the current position and orientation
|
2012-10-09 20:21:02 +00:00
|
|
|
void setTransform(const Transform& transform);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the AAABB of the body
|
2013-03-14 21:47:59 +00:00
|
|
|
const AABB& getAABB() const;
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return the interpolated transform for rendering
|
2012-10-09 20:21:02 +00:00
|
|
|
Transform getInterpolatedTransform() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the interpolation factor of the body
|
2012-10-09 20:21:02 +00:00
|
|
|
void setInterpolationFactor(decimal factor);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return if the rigid body can move
|
2012-10-09 20:21:02 +00:00
|
|
|
bool getIsMotionEnabled() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the value to true if the body can move
|
2012-10-09 20:21:02 +00:00
|
|
|
void setIsMotionEnabled(bool isMotionEnabled);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Return true if the body can collide with others bodies
|
2012-10-09 20:21:02 +00:00
|
|
|
bool getIsCollisionEnabled() const;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Set the isCollisionEnabled value
|
2012-10-09 20:21:02 +00:00
|
|
|
void setIsCollisionEnabled(bool isCollisionEnabled);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Update the old transform with the current one.
|
2012-10-09 20:21:02 +00:00
|
|
|
void updateOldTransform();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Update the Axis-Aligned Bounding Box coordinates
|
2012-10-09 20:21:02 +00:00
|
|
|
void updateAABB();
|
2012-09-18 20:09:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Return true if the body has moved during the last frame
|
|
|
|
inline bool CollisionBody::getHasMoved() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mHasMoved;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the hasMoved variable (true if the body has moved during the last frame)
|
|
|
|
inline void CollisionBody::setHasMoved(bool hasMoved) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mHasMoved = hasMoved;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the collision shape
|
|
|
|
inline CollisionShape* CollisionBody::getCollisionShape() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
assert(mCollisionShape);
|
|
|
|
return mCollisionShape;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the collision shape
|
|
|
|
inline void CollisionBody::setCollisionShape(CollisionShape* collisionShape) {
|
|
|
|
assert(collisionShape);
|
2012-10-09 20:21:02 +00:00
|
|
|
mCollisionShape = collisionShape;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the body is active
|
|
|
|
inline bool CollisionBody::getIsActive() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mIsActive;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the isActive variable
|
|
|
|
inline void CollisionBody::setIsActive(bool isActive) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mIsActive = isActive;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the interpolated transform for rendering
|
|
|
|
inline Transform CollisionBody::getInterpolatedTransform() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return Transform::interpolateTransforms(mOldTransform, mTransform, mInterpolationFactor);
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the interpolation factor of the body
|
|
|
|
inline void CollisionBody::setInterpolationFactor(decimal factor) {
|
|
|
|
// Set the factor
|
2012-10-09 20:21:02 +00:00
|
|
|
mInterpolationFactor = factor;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return if the rigid body can move
|
|
|
|
inline bool CollisionBody::getIsMotionEnabled() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mIsMotionEnabled;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value to true if the body can move
|
|
|
|
inline void CollisionBody::setIsMotionEnabled(bool isMotionEnabled) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mIsMotionEnabled = isMotionEnabled;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current position and orientation
|
|
|
|
inline const Transform& CollisionBody::getTransform() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mTransform;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current position and orientation
|
|
|
|
inline void CollisionBody::setTransform(const Transform& transform) {
|
|
|
|
|
|
|
|
// Check if the body has moved
|
2013-06-12 18:43:54 +00:00
|
|
|
if (mTransform != transform) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mHasMoved = true;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
mTransform = transform;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the AAABB of the body
|
2013-03-14 21:47:59 +00:00
|
|
|
inline const AABB& CollisionBody::getAABB() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mAabb;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the body can collide with others bodies
|
|
|
|
inline bool CollisionBody::getIsCollisionEnabled() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mIsCollisionEnabled;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the isCollisionEnabled value
|
|
|
|
inline void CollisionBody::setIsCollisionEnabled(bool isCollisionEnabled) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mIsCollisionEnabled = isCollisionEnabled;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
// Update the old transform with the current one.
|
|
|
|
/// This is used to compute the interpolated position and orientation of the body
|
2012-09-18 20:09:49 +00:00
|
|
|
inline void CollisionBody::updateOldTransform() {
|
2012-10-09 20:21:02 +00:00
|
|
|
mOldTransform = mTransform;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the rigid body in order to reflect a change in the body state
|
|
|
|
inline void CollisionBody::updateAABB() {
|
|
|
|
|
|
|
|
// Update the AABB
|
2013-03-14 21:47:59 +00:00
|
|
|
mCollisionShape->updateAABB(mAabb, mTransform);
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|