Make possible to use the CollisionBody::setIsActive() method to activate or deactivate the simulation of a given body

This commit is contained in:
Daniel Chappuis 2014-11-09 18:53:50 +01:00
parent ca660b5057
commit adc53c6523
3 changed files with 57 additions and 2 deletions

View File

@ -54,8 +54,14 @@ class Body {
/// True if the body is allowed to go to sleep for better efficiency
bool mIsAllowedToSleep;
// TODO : Use this variable to make bodies active or not
/// True if the body is active
/// True if the body is active.
/// An inactive body does not participate in collision detection,
/// is not simulated and will not be hit in a ray casting query.
/// A body is active by default. If you set this
/// value to "false", all the proxy shapes of this body will be
/// removed from the broad-phase. If you set this value to "true",
/// all the proxy shapes will be added to the broad-phase. A joint
/// connected to an inactive body will also be inactive.
bool mIsActive;
/// True if the body is sleeping (for sleeping technique)
@ -100,6 +106,9 @@ class Body {
/// Return true if the body is active
bool isActive() const;
/// Set whether or not the body is active
virtual void setIsActive(bool isActive);
/// Set the variable to know whether or not the body is sleeping
virtual void setIsSleeping(bool isSleeping);
@ -153,6 +162,11 @@ inline bool Body::isActive() const {
return mIsActive;
}
// Set whether or not the body is active
inline void Body::setIsActive(bool isActive) {
mIsActive = isActive;
}
// Set the variable to know whether or not the body is sleeping
inline void Body::setIsSleeping(bool isSleeping) {

View File

@ -188,6 +188,44 @@ void CollisionBody::updateBroadPhaseState() const {
}
}
// Set whether or not the body is active
void CollisionBody::setIsActive(bool isActive) {
// If the state does not change
if (mIsActive == isActive) return;
Body::setIsActive(isActive);
// TODO : Implement this
// If we have to activate the body
if (isActive) {
// For each proxy shape of the body
for (ProxyShape* shape = mProxyCollisionShapes; shape != NULL; shape = shape->mNext) {
// Compute the world-space AABB of the new collision shape
AABB aabb;
shape->getCollisionShape()->computeAABB(aabb, mTransform * shape->mLocalToBodyTransform);
// Add the proxy shape to the collision detection
mWorld.mCollisionDetection.addProxyCollisionShape(shape, aabb);
}
}
else { // If we have to deactivate the body
// For each proxy shape of the body
for (ProxyShape* shape = mProxyCollisionShapes; shape != NULL; shape = shape->mNext) {
// Remove the proxy shape from the collision detection
mWorld.mCollisionDetection.removeProxyCollisionShape(shape);
}
// Reset the contact manifold list of the body
resetContactManifoldsList();
}
}
// Ask the broad-phase to test again the collision shapes of the body for collision
// (as if the body has moved).
void CollisionBody::askForBroadPhaseCollisionCheck() const {

View File

@ -134,6 +134,9 @@ class CollisionBody : public Body {
/// Set the type of the body
void setType(BodyType type);
/// Set whether or not the body is active
virtual void setIsActive(bool isActive);
/// Return the current position and orientation
const Transform& getTransform() const;