diff --git a/src/body/Body.h b/src/body/Body.h index 946aa61f..e16a15c4 100644 --- a/src/body/Body.h +++ b/src/body/Body.h @@ -83,9 +83,6 @@ class Body { /// Private assignment operator Body& operator=(const Body& body); - /// Set the variable to know whether or not the body is sleeping - virtual void setIsSleeping(bool isSleeping); - public : // -------------------- Methods -------------------- // @@ -105,6 +102,9 @@ class Body { /// Set whether or not the body is allowed to go to sleep void setIsAllowedToSleep(bool isAllowedToSleep); + /// Set the variable to know whether or not the body is sleeping + virtual void setIsSleeping(bool isSleeping); + /// Return whether or not the body is sleeping bool isSleeping() const; diff --git a/src/body/CollisionBody.h b/src/body/CollisionBody.h index 819584b5..955a2130 100644 --- a/src/body/CollisionBody.h +++ b/src/body/CollisionBody.h @@ -132,7 +132,7 @@ class CollisionBody : public Body { const Transform& getTransform() const; /// Set the current position and orientation - void setTransform(const Transform& transform); + virtual void setTransform(const Transform& transform); /// Add a collision shape to the body. virtual ProxyShape* addCollisionShape(const CollisionShape& collisionShape, diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index ddf1ea83..32f540f8 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -265,6 +265,64 @@ void RigidBody::removeCollisionShape(const ProxyShape* proxyShape) { recomputeMassInformation(); } +// Set the linear velocity of the rigid body. +/** + * @param linearVelocity Linear velocity vector of the body + */ +void RigidBody::setLinearVelocity(const Vector3& linearVelocity) { + + // If it is a static body, we do nothing + if (mType == STATIC) return; + + // Update the linear velocity of the current body state + mLinearVelocity = linearVelocity; + + // If the linear velocity is not zero, awake the body + if (mLinearVelocity.lengthSquare() > decimal(0.0)) { + setIsSleeping(false); + } +} + +// Set the angular velocity. +/** +* @param angularVelocity The angular velocity vector of the body +*/ +void RigidBody::setAngularVelocity(const Vector3& angularVelocity) { + + // If it is a static body, we do nothing + if (mType == STATIC) return; + + // Set the angular velocity + mAngularVelocity = angularVelocity; + + // If the velocity is not zero, awake the body + if (mAngularVelocity.lengthSquare() > decimal(0.0)) { + setIsSleeping(false); + } +} + +// Set the current position and orientation +/** + * @param transform The transformation of the body that transforms the local-space + * of the body into world-space + */ +void RigidBody::setTransform(const Transform& transform) { + + // Update the transform of the body + mTransform = transform; + + const Vector3 oldCenterOfMass = mCenterOfMassWorld; + + // Compute the new center of mass in world-space coordinates + mCenterOfMassWorld = mTransform * mCenterOfMassLocal; + + // Update the linear velocity of the center of mass + mLinearVelocity += mAngularVelocity.cross(mCenterOfMassWorld - oldCenterOfMass); + + // Update the broad-phase state of the body + updateBroadPhaseState(); +} + // Recompute the center of mass, total mass and inertia tensor of the body using all // the collision shapes attached to the body. void RigidBody::recomputeMassInformation() { diff --git a/src/body/RigidBody.h b/src/body/RigidBody.h index 964fa286..e8cc573b 100644 --- a/src/body/RigidBody.h +++ b/src/body/RigidBody.h @@ -118,9 +118,6 @@ class RigidBody : public CollisionBody { /// Update the broad-phase state for this body (because it has moved for instance) virtual void updateBroadPhaseState() const; - /// Set the variable to know whether or not the body is sleeping - virtual void setIsSleeping(bool isSleeping); - public : // -------------------- Methods -------------------- // @@ -134,6 +131,9 @@ class RigidBody : public CollisionBody { /// Set the type of the body (static, kinematic or dynamic) void setType(BodyType type); + /// Set the current position and orientation + virtual void setTransform(const Transform& transform); + /// Return the mass of the body decimal getMass() const; @@ -149,6 +149,9 @@ class RigidBody : public CollisionBody { /// Set the angular velocity. void setAngularVelocity(const Vector3& angularVelocity); + /// Set the variable to know whether or not the body is sleeping + virtual void setIsSleeping(bool isSleeping); + /// Return the local inertia tensor of the body (in body coordinates) const Matrix3x3& getInertiaTensorLocal() const; @@ -252,20 +255,6 @@ inline Vector3 RigidBody::getAngularVelocity() const { return mAngularVelocity; } -// Set the angular velocity. -/// You should only call this method for a kinematic body. Otherwise, it -/// will do nothing. -/** -* @param angularVelocity The angular velocity vector of the body -*/ -inline void RigidBody::setAngularVelocity(const Vector3& angularVelocity) { - - // If it is a kinematic body - if (mType == KINEMATIC) { - mAngularVelocity = angularVelocity; - } -} - // Return the local inertia tensor of the body (in local-space coordinates) /** * @return The 3x3 inertia tensor matrix of the body (in local-space coordinates) @@ -310,22 +299,6 @@ inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const { mTransform.getOrientation().getMatrix().getTranspose(); } -// Set the linear velocity of the rigid body. -/// You should only call this method for a kinematic body. Otherwise, it -/// will do nothing. -/** - * @param linearVelocity Linear velocity vector of the body - */ -inline void RigidBody::setLinearVelocity(const Vector3& linearVelocity) { - - // If it is a kinematic body - if (mType == KINEMATIC) { - - // Update the linear velocity of the current body state - mLinearVelocity = linearVelocity; - } -} - // Return true if the gravity needs to be applied to this rigid body /** * @return True if the gravity is applied to the body