From adc53c65239e58cc413a59c7890f1c1fcc239021 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sun, 9 Nov 2014 18:53:50 +0100 Subject: [PATCH] Make possible to use the CollisionBody::setIsActive() method to activate or deactivate the simulation of a given body --- src/body/Body.h | 18 ++++++++++++++++-- src/body/CollisionBody.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/body/CollisionBody.h | 3 +++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/body/Body.h b/src/body/Body.h index 2a0077a1..a4dc54f6 100644 --- a/src/body/Body.h +++ b/src/body/Body.h @@ -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) { diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index 921268ef..e8108bb9 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -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 { diff --git a/src/body/CollisionBody.h b/src/body/CollisionBody.h index a70ad41a..98e4858b 100644 --- a/src/body/CollisionBody.h +++ b/src/body/CollisionBody.h @@ -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;