|
@@ -14,6 +14,7 @@ namespace gameplay
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
class Node;
|
|
class Node;
|
|
|
|
|
+class PhysicsConstraint;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Defines a class for physics rigid bodies.
|
|
* Defines a class for physics rigid bodies.
|
|
@@ -21,14 +22,18 @@ class Node;
|
|
|
class PhysicsRigidBody : public Ref
|
|
class PhysicsRigidBody : public Ref
|
|
|
{
|
|
{
|
|
|
friend class Node;
|
|
friend class Node;
|
|
|
- friend class PhysicsMotionState;
|
|
|
|
|
|
|
+ friend class PhysicsController;
|
|
|
friend class PhysicsFixedConstraint;
|
|
friend class PhysicsFixedConstraint;
|
|
|
friend class PhysicsGenericConstraint;
|
|
friend class PhysicsGenericConstraint;
|
|
|
friend class PhysicsHingeConstraint;
|
|
friend class PhysicsHingeConstraint;
|
|
|
|
|
+ friend class PhysicsMotionState;
|
|
|
friend class PhysicsSocketConstraint;
|
|
friend class PhysicsSocketConstraint;
|
|
|
friend class PhysicsSpringConstraint;
|
|
friend class PhysicsSpringConstraint;
|
|
|
|
|
|
|
|
public:
|
|
public:
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Represents the different types of rigid bodies.
|
|
|
|
|
+ */
|
|
|
enum Type
|
|
enum Type
|
|
|
{
|
|
{
|
|
|
PHYSICS_SHAPE_BOX,
|
|
PHYSICS_SHAPE_BOX,
|
|
@@ -38,133 +43,187 @@ public:
|
|
|
PHYSICS_SHAPE_NONE
|
|
PHYSICS_SHAPE_NONE
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Applies the given force to the rigid body (optionally, from the given relative position).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param force The force to be applied.
|
|
|
|
|
+ * @param relativePosition The relative position from which to apply the force.
|
|
|
|
|
+ */
|
|
|
void applyForce(const Vector3& force, const Vector3* relativePosition = NULL);
|
|
void applyForce(const Vector3& force, const Vector3* relativePosition = NULL);
|
|
|
- void applyImpulse(const Vector3& impulse, const Vector3* relativePosition = NULL);
|
|
|
|
|
- void applyTorque(const Vector3& torque);
|
|
|
|
|
- void applyTorqueImpulse(const Vector3& torque);
|
|
|
|
|
- // TODO!!
|
|
|
|
|
- //void update();
|
|
|
|
|
|
|
|
|
|
- inline void setFriction(float friction);
|
|
|
|
|
- inline void setRestitution(float restitution);
|
|
|
|
|
- inline void setAnisotropicFriction(const Vector3& friction);
|
|
|
|
|
- inline void setGravity(const Vector3& gravity);
|
|
|
|
|
- inline float getFriction();
|
|
|
|
|
- inline float getRestitution();
|
|
|
|
|
- inline Vector3 getAnisotropicFriction();
|
|
|
|
|
- inline Vector3 getGravity();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Applies the given force impulse to the rigid body (optionally, from the given relative position).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param impulse The force impulse to be applied.
|
|
|
|
|
+ * @param relativePosition The relative position from which to apply the force.
|
|
|
|
|
+ */
|
|
|
|
|
+ void applyImpulse(const Vector3& impulse, const Vector3* relativePosition = NULL);
|
|
|
|
|
|
|
|
- inline void setLinearVelocity(const Vector3& velocity);
|
|
|
|
|
- inline void setAngularVelocity(const Vector3& velocity);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Applies the given torque to the rigid body.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param torque The torque to be applied.
|
|
|
|
|
+ */
|
|
|
|
|
+ void applyTorque(const Vector3& torque);
|
|
|
|
|
|
|
|
- inline Vector3 getLinearVelocity();
|
|
|
|
|
- inline Vector3 getAngularVelocity();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Applies the given torque impulse to the rigid body.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param torque The torque impulse to be applied.
|
|
|
|
|
+ */
|
|
|
|
|
+ void applyTorqueImpulse(const Vector3& torque);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's angular damping.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The angular damping.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline float getAngularDamping() const;
|
|
|
|
|
|
|
|
- inline void setDamping(float linearDamping, float angularDamping);
|
|
|
|
|
- inline float getLinearDamping();
|
|
|
|
|
- inline float getAngularDamping();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's angular velocity.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The angular velocity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline Vector3 getAngularVelocity() const;
|
|
|
|
|
|
|
|
-private:
|
|
|
|
|
- PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, float mass, float friction = 0.5,
|
|
|
|
|
- float restitution = 0.0, float linearDamping = 0.0, float angularDamping = 0.0);
|
|
|
|
|
- ~PhysicsRigidBody();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's anisotropic friction.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The anisotropic friction.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline Vector3 getAnisotropicFriction() const;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Private copy constructor to disallow copying.
|
|
|
|
|
|
|
+ * Gets the rigid body's friction.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The friction.
|
|
|
*/
|
|
*/
|
|
|
- PhysicsRigidBody(const PhysicsRigidBody& body) {}
|
|
|
|
|
|
|
+ inline float getFriction() const;
|
|
|
|
|
|
|
|
- static btRigidBody* createBulletRigidBody(btCollisionShape* shape, float mass, Node* node,
|
|
|
|
|
- float friction, float restitution, float linearDamping, float angularDamping,
|
|
|
|
|
- const Vector3* centerOfMassOffset = NULL);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the gravity that affects the rigid body (this can
|
|
|
|
|
+ * be different from the global gravity; @see #setGravity).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The gravity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline Vector3 getGravity() const;
|
|
|
|
|
|
|
|
- btRigidBody* _body;
|
|
|
|
|
- Node* _node;
|
|
|
|
|
-};
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's linear damping.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The linear damping.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline float getLinearDamping() const;
|
|
|
|
|
|
|
|
-// Inline functions.
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's linear velocity.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The linear velocity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline Vector3 getLinearVelocity() const;
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setFriction(float friction)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setFriction(friction);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the rigid body's restitution.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return The restitution.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline float getRestitution() const;
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setRestitution(float restitution)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setRestitution(restitution);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's angular velocity.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param velocity The angular velocity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setAngularVelocity(const Vector3& velocity);
|
|
|
|
|
|
|
|
-float PhysicsRigidBody::getFriction()
|
|
|
|
|
-{
|
|
|
|
|
- return _body->getFriction();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's anisotropic friction.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param friction The anisotropic friction.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setAnisotropicFriction(const Vector3& friction);
|
|
|
|
|
|
|
|
-float PhysicsRigidBody::getRestitution()
|
|
|
|
|
-{
|
|
|
|
|
- return _body->getRestitution();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's linear and angular damping.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param linearDamping The linear damping; between 0.0 (minimum) and 1.0 (maximum).
|
|
|
|
|
+ * @param angularDamping The angular damping; between 0.0 (minimum) and 1.0 (maximum).
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setDamping(float linearDamping, float angularDamping);
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setLinearVelocity(const Vector3& velocity)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's friction.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param friction The friction.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setFriction(float friction);
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setAngularVelocity(const Vector3& velocity)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's gravity (this overrides the global gravity for this rigid body).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param gravity The gravity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setGravity(const Vector3& gravity);
|
|
|
|
|
|
|
|
-// TODO: We can't cache these Vector3 values, but should we have member variables
|
|
|
|
|
-// that are set each time instead of creating a new Vector3 every time?
|
|
|
|
|
-Vector3 PhysicsRigidBody::getLinearVelocity()
|
|
|
|
|
-{
|
|
|
|
|
- const btVector3& v = _body->getLinearVelocity();
|
|
|
|
|
- return Vector3(v.x(), v.y(), v.z());
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's linear velocity.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param velocity The linear velocity.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setLinearVelocity(const Vector3& velocity);
|
|
|
|
|
|
|
|
-Vector3 PhysicsRigidBody::getAngularVelocity()
|
|
|
|
|
-{
|
|
|
|
|
- const btVector3& v = _body->getAngularVelocity();
|
|
|
|
|
- return Vector3(v.x(), v.y(), v.z());
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Sets the rigid body's restitution (or bounciness).
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param restitution The restitution.
|
|
|
|
|
+ */
|
|
|
|
|
+ inline void setRestitution(float restitution);
|
|
|
|
|
|
|
|
-Vector3 PhysicsRigidBody::getAnisotropicFriction()
|
|
|
|
|
-{
|
|
|
|
|
- const btVector3& af = _body->getAnisotropicFriction();
|
|
|
|
|
- return Vector3(af.x(), af.y(), af.z());
|
|
|
|
|
-}
|
|
|
|
|
|
|
+private:
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Creates a rigid body.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param node The node to create a rigid body for; note that the node must have
|
|
|
|
|
+ * a model attached to it prior to creating a rigid body for it.
|
|
|
|
|
+ * @param type The type of rigid body to set.
|
|
|
|
|
+ * @param mass The mass of the rigid body, in kilograms.
|
|
|
|
|
+ * @param friction The friction of the rigid body (non-zero values give best simulation results).
|
|
|
|
|
+ * @param restitution The restitution of the rigid body (this controls the bounciness of
|
|
|
|
|
+ * the rigid body; use zero for best simulation results).
|
|
|
|
|
+ * @param linearDamping The percentage of linear velocity lost per second (between 0.0 and 1.0).
|
|
|
|
|
+ * @param angularDamping The percentage of angular velocity lost per second (between 0.0 and 1.0).
|
|
|
|
|
+ */
|
|
|
|
|
+ PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, float mass, float friction = 0.5,
|
|
|
|
|
+ float restitution = 0.0, float linearDamping = 0.0, float angularDamping = 0.0);
|
|
|
|
|
|
|
|
-Vector3 PhysicsRigidBody::getGravity()
|
|
|
|
|
-{
|
|
|
|
|
- const btVector3& g = _body->getGravity();
|
|
|
|
|
- return Vector3(g.x(), g.y(), g.z());
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Destructor.
|
|
|
|
|
+ */
|
|
|
|
|
+ ~PhysicsRigidBody();
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setDamping(float linearDamping, float angularDamping)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setDamping(linearDamping, angularDamping);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Private copy constructor to disallow copying.
|
|
|
|
|
+ */
|
|
|
|
|
+ PhysicsRigidBody(const PhysicsRigidBody& body);
|
|
|
|
|
|
|
|
-float PhysicsRigidBody::getLinearDamping()
|
|
|
|
|
-{
|
|
|
|
|
- return _body->getLinearDamping();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ // Creates the underlying Bullet Physics rigid body object
|
|
|
|
|
+ // for a PhysicsRigidBody object using the given parameters.
|
|
|
|
|
+ static btRigidBody* createBulletRigidBody(btCollisionShape* shape, float mass, Node* node,
|
|
|
|
|
+ float friction, float restitution, float linearDamping, float angularDamping,
|
|
|
|
|
+ const Vector3* centerOfMassOffset = NULL);
|
|
|
|
|
|
|
|
-float PhysicsRigidBody::getAngularDamping()
|
|
|
|
|
-{
|
|
|
|
|
- return _body->getAngularDamping();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ // Adds a constraint to this rigid body.
|
|
|
|
|
+ inline void addConstraint(PhysicsConstraint* constraint);
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setAnisotropicFriction(const Vector3& friction)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setAnisotropicFriction(btVector3(friction.x, friction.y, friction.z));
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ btCollisionShape* _shape;
|
|
|
|
|
+ btRigidBody* _body;
|
|
|
|
|
+ Node* _node;
|
|
|
|
|
+ std::vector<PhysicsConstraint*> _constraints;
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
-void PhysicsRigidBody::setGravity(const Vector3& gravity)
|
|
|
|
|
-{
|
|
|
|
|
- _body->setGravity(btVector3(gravity.x, gravity.y, gravity.z));
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+#include "PhysicsRigidBody.inl"
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|