Rename RigidBody::applyForce() to RigidBody::applyForceAtWorldPosition() and add RigidBody::applyForceAtLocalPosition() method

This commit is contained in:
Daniel Chappuis 2020-05-09 00:09:38 +02:00
parent 862db8f379
commit e04565fb48
3 changed files with 39 additions and 2 deletions

View File

@ -24,6 +24,7 @@
- The EventListener class now has a onTrigger() method that is called when a trigger collider is colling with another collider.
- In the EventListener, the onContact() and onTrigger() method now reports the type of event (start, stay, exit) for each contact. This way the user can know whether it's a new contact or not or when two colliders are not in contact anymore.
- A DebugRenderer class has been added in order to display debug info (colliders, AABBs, contacts, ...) using graphics primities (lines, triangles).
- A RigidBody::applyForceAtLocalPosition() method has been added to apply a force at a given position of the rigid body in local-space.
### Fixed
@ -60,6 +61,7 @@
- Now, the local inertia tensor of a rigid body has to be set using a Vector3 instead of a Matrix3x3. You only need to provide the diagonal values of the matrix
- The RigidBody::recomputeMassInformation() method has been renamed to RigidBody::updateMassPropertiesFromColliders.
- Now, you need to manually call the RigidBody::recomputeMassInformation() method after adding colliders to a rigid body to recompute its inertia tensor, center of mass and mass
- The RigidBody::applyForce() method has been renamed to RigidBody::applyForceAtWorldPosition()
- The rendering in the testbed application has been improved
- The old Logger class has been renamed to DefaultLogger
- The Logger class is now an abstract class that you can inherit from in order to receive log events from the library

View File

@ -156,8 +156,11 @@ class RigidBody : public CollisionBody {
/// Apply an external force to the body at its center of mass.
void applyForceToCenterOfMass(const Vector3& force);
/// Apply an external force to the body at a given point (in local-space coordinates).
void applyForceAtLocalPosition(const Vector3& force, const Vector3& point);
/// Apply an external force to the body at a given point (in world-space coordinates).
void applyForce(const Vector3& force, const Vector3& point);
void applyForceAtWorldPosition(const Vector3& force, const Vector3& point);
/// Apply an external torque to the body.
void applyTorque(const Vector3& torque);

View File

@ -127,6 +127,38 @@ decimal RigidBody::getMass() const {
return mWorld.mRigidBodyComponents.getMass(mEntity);
}
// Apply an external force to the body at a given point (in local-space coordinates).
/// If the point is not at the center of mass of the body, it will also
/// generate some torque and therefore, change the angular velocity of the body.
/// If the body is sleeping, calling this method will wake it up. Note that the
/// force will we added to the sum of the applied forces and that this sum will be
/// reset to zero at the end of each call of the PhyscisWorld::update() method.
/// You can only apply a force to a dynamic body otherwise, this method will do nothing.
/**
* @param force The force to apply on the body
* @param point The point where the force is applied (in local-space coordinates)
*/
void RigidBody::applyForceAtLocalPosition(const Vector3& force, const Vector3& point) {
// If it is not a dynamic body, we do nothing
if (mWorld.mRigidBodyComponents.getBodyType(mEntity) != BodyType::DYNAMIC) return;
// Awake the body if it was sleeping
if (mWorld.mRigidBodyComponents.getIsSleeping(mEntity)) {
setIsSleeping(false);
}
// Add the force
const Vector3& externalForce = mWorld.mRigidBodyComponents.getExternalForce(mEntity);
mWorld.mRigidBodyComponents.setExternalForce(mEntity, externalForce + force);
// Add the torque
const Vector3& externalTorque = mWorld.mRigidBodyComponents.getExternalTorque(mEntity);
const Vector3& centerOfMassWorld = mWorld.mRigidBodyComponents.getCenterOfMassWorld(mEntity);
const Vector3 worldPoint = mWorld.mTransformComponents.getTransform(mEntity) * point;
mWorld.mRigidBodyComponents.setExternalTorque(mEntity, externalTorque + (worldPoint - centerOfMassWorld).cross(force));
}
// Apply an external force to the body at a given point (in world-space coordinates).
/// If the point is not at the center of mass of the body, it will also
/// generate some torque and therefore, change the angular velocity of the body.
@ -138,7 +170,7 @@ decimal RigidBody::getMass() const {
* @param force The force to apply on the body
* @param point The point where the force is applied (in world-space coordinates)
*/
void RigidBody::applyForce(const Vector3& force, const Vector3& point) {
void RigidBody::applyForceAtWorldPosition(const Vector3& force, const Vector3& point) {
// If it is not a dynamic body, we do nothing
if (mWorld.mRigidBodyComponents.getBodyType(mEntity) != BodyType::DYNAMIC) return;