|
|
@@ -12,18 +12,18 @@ namespace gameplay
|
|
|
{
|
|
|
|
|
|
PhysicsRigidBody::PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, float mass,
|
|
|
- float friction, float restitution, float linearDamping, float angularDamping)
|
|
|
+ float friction, float restitution, float linearDamping, float angularDamping)
|
|
|
: _shape(NULL), _body(NULL), _node(node)
|
|
|
{
|
|
|
- switch (type)
|
|
|
- {
|
|
|
- case PhysicsRigidBody::PHYSICS_SHAPE_BOX:
|
|
|
- {
|
|
|
- const BoundingBox& box = node->getModel()->getMesh()->getBoundingBox();
|
|
|
+ switch (type)
|
|
|
+ {
|
|
|
+ case PhysicsRigidBody::SHAPE_BOX:
|
|
|
+ {
|
|
|
+ const BoundingBox& box = node->getModel()->getMesh()->getBoundingBox();
|
|
|
|
|
|
PhysicsController* physics = Game::getInstance()->getPhysicsController();
|
|
|
_shape = physics->getBox(box.min, box.max, btVector3(node->getScaleX(), node->getScaleY(), node->getScaleZ()));
|
|
|
-
|
|
|
+
|
|
|
// Use the center of the bounding box as the center of mass offset.
|
|
|
Vector3 c(box.min, box.max);
|
|
|
c.scale(0.5f);
|
|
|
@@ -31,18 +31,18 @@ PhysicsRigidBody::PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, floa
|
|
|
c.negate();
|
|
|
|
|
|
if (c.lengthSquared() > MATH_EPSILON)
|
|
|
- _body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping, &c);
|
|
|
+ _body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping, &c);
|
|
|
else
|
|
|
_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
|
|
|
- break;
|
|
|
- }
|
|
|
- case PhysicsRigidBody::PHYSICS_SHAPE_SPHERE:
|
|
|
- {
|
|
|
- const BoundingSphere& sphere = node->getModel()->getMesh()->getBoundingSphere();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PhysicsRigidBody::SHAPE_SPHERE:
|
|
|
+ {
|
|
|
+ const BoundingSphere& sphere = node->getModel()->getMesh()->getBoundingSphere();
|
|
|
|
|
|
- PhysicsController* physics = Game::getInstance()->getPhysicsController();
|
|
|
- _shape = physics->getSphere(sphere.radius, btVector3(node->getScaleX(), node->getScaleY(), node->getScaleZ()));
|
|
|
+ PhysicsController* physics = Game::getInstance()->getPhysicsController();
|
|
|
+ _shape = physics->getSphere(sphere.radius, btVector3(node->getScaleX(), node->getScaleY(), node->getScaleZ()));
|
|
|
|
|
|
// Use the center of the bounding sphere as the center of mass offset.
|
|
|
Vector3 c(sphere.center);
|
|
|
@@ -53,33 +53,35 @@ PhysicsRigidBody::PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, floa
|
|
|
else
|
|
|
_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
|
|
|
- break;
|
|
|
- }
|
|
|
- case PhysicsRigidBody::PHYSICS_SHAPE_TRIANGLE_MESH:
|
|
|
- {
|
|
|
- //btTriangleIndexVertexArray meshShape(numTriangles, indexPointer, indexStride, numVertices, vertexPointer, vertexStride);
|
|
|
- //_shape = btBvhTriangleMeshShape(meshShape, true);
|
|
|
-
|
|
|
- //_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
- break;
|
|
|
- }
|
|
|
- case PhysicsRigidBody::PHYSICS_SHAPE_HEIGHTFIELD:
|
|
|
- {
|
|
|
- //_shape = btHeightfieldTerrainShape(width, length, data, scale, minHeight, maxHeight, upAxis, dataType, false);
|
|
|
-
|
|
|
- //_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PhysicsRigidBody::SHAPE_TRIANGLE_MESH:
|
|
|
+ {
|
|
|
+ //btTriangleIndexVertexArray meshShape(numTriangles, indexPointer, indexStride, numVertices, vertexPointer, vertexStride);
|
|
|
+ //_shape = btBvhTriangleMeshShape(meshShape, true);
|
|
|
+
|
|
|
+ //_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PhysicsRigidBody::SHAPE_HEIGHTFIELD:
|
|
|
+ {
|
|
|
+ //_shape = btHeightfieldTerrainShape(width, length, data, scale, minHeight, maxHeight, upAxis, dataType, false);
|
|
|
+
|
|
|
+ //_body = createBulletRigidBody(_shape, mass, node, friction, restitution, linearDamping, angularDamping);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
PhysicsRigidBody::~PhysicsRigidBody()
|
|
|
{
|
|
|
// Clean up all constraints linked to this rigid body.
|
|
|
- for (unsigned int i = 0; i < _constraints.size(); i++)
|
|
|
+ PhysicsConstraint* ptr = NULL;
|
|
|
+ while (_constraints.size() > 0)
|
|
|
{
|
|
|
- Game::getInstance()->getPhysicsController()->removeConstraint(_constraints[i]);
|
|
|
- SAFE_RELEASE(_constraints[i]);
|
|
|
+ ptr = _constraints.back();
|
|
|
+ _constraints.pop_back();
|
|
|
+ SAFE_DELETE(ptr);
|
|
|
}
|
|
|
|
|
|
// Clean up the rigid body and its related objects.
|
|
|
@@ -104,9 +106,9 @@ void PhysicsRigidBody::applyForce(const Vector3& force, const Vector3* relativeP
|
|
|
{
|
|
|
_body->activate();
|
|
|
if (relativePosition)
|
|
|
- _body->applyForce(btVector3(force.x, force.y, force.z), btVector3(relativePosition->x, relativePosition->y, relativePosition->z));
|
|
|
- else
|
|
|
- _body->applyCentralForce(btVector3(force.x, force.y, force.z));
|
|
|
+ _body->applyForce(btVector3(force.x, force.y, force.z), btVector3(relativePosition->x, relativePosition->y, relativePosition->z));
|
|
|
+ else
|
|
|
+ _body->applyCentralForce(btVector3(force.x, force.y, force.z));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -118,12 +120,12 @@ void PhysicsRigidBody::applyImpulse(const Vector3& impulse, const Vector3* relat
|
|
|
{
|
|
|
_body->activate();
|
|
|
|
|
|
- if (relativePosition)
|
|
|
+ if (relativePosition)
|
|
|
{
|
|
|
- _body->applyImpulse(btVector3(impulse.x, impulse.y, impulse.z), btVector3(relativePosition->x, relativePosition->y, relativePosition->z));
|
|
|
+ _body->applyImpulse(btVector3(impulse.x, impulse.y, impulse.z), btVector3(relativePosition->x, relativePosition->y, relativePosition->z));
|
|
|
}
|
|
|
- else
|
|
|
- _body->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z));
|
|
|
+ else
|
|
|
+ _body->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -134,7 +136,7 @@ void PhysicsRigidBody::applyTorque(const Vector3& torque)
|
|
|
if (torque.lengthSquared() > MATH_EPSILON)
|
|
|
{
|
|
|
_body->activate();
|
|
|
- _body->applyTorque(btVector3(torque.x, torque.y, torque.z));
|
|
|
+ _body->applyTorque(btVector3(torque.x, torque.y, torque.z));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -152,28 +154,43 @@ void PhysicsRigidBody::applyTorqueImpulse(const Vector3& torque)
|
|
|
btRigidBody* PhysicsRigidBody::createBulletRigidBody(btCollisionShape* shape, float mass, Node* node,
|
|
|
float friction, float restitution, float linearDamping, float angularDamping, const Vector3* centerOfMassOffset)
|
|
|
{
|
|
|
- // If the mass is non-zero, then the object is dynamic
|
|
|
- // and we need to calculate the local inertia.
|
|
|
- btVector3 localInertia(0.0, 0.0, 0.0);
|
|
|
- if (mass != 0.0)
|
|
|
- shape->calculateLocalInertia(mass, localInertia);
|
|
|
-
|
|
|
- // Create the Bullet physics rigid body object.
|
|
|
- PhysicsMotionState* motionState = new PhysicsMotionState(node, centerOfMassOffset);
|
|
|
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState, shape, localInertia);
|
|
|
+ // If the mass is non-zero, then the object is dynamic
|
|
|
+ // and we need to calculate the local inertia.
|
|
|
+ btVector3 localInertia(0.0, 0.0, 0.0);
|
|
|
+ if (mass != 0.0)
|
|
|
+ shape->calculateLocalInertia(mass, localInertia);
|
|
|
+
|
|
|
+ // Create the Bullet physics rigid body object.
|
|
|
+ PhysicsMotionState* motionState = new PhysicsMotionState(node, centerOfMassOffset);
|
|
|
+ btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState, shape, localInertia);
|
|
|
rbInfo.m_friction = friction;
|
|
|
rbInfo.m_restitution = restitution;
|
|
|
rbInfo.m_linearDamping = linearDamping;
|
|
|
rbInfo.m_angularDamping = angularDamping;
|
|
|
- btRigidBody* body = new btRigidBody(rbInfo);
|
|
|
+ btRigidBody* body = new btRigidBody(rbInfo);
|
|
|
|
|
|
- // Add the rigid body to the physics world.
|
|
|
- PhysicsController* physics = Game::getInstance()->getPhysicsController();
|
|
|
- physics->_world->addRigidBody(body);
|
|
|
+ // Add the rigid body to the physics world.
|
|
|
+ PhysicsController* physics = Game::getInstance()->getPhysicsController();
|
|
|
+ physics->_world->addRigidBody(body);
|
|
|
|
|
|
- return body;
|
|
|
+ return body;
|
|
|
}
|
|
|
|
|
|
+void PhysicsRigidBody::addConstraint(PhysicsConstraint* constraint)
|
|
|
+{
|
|
|
+ _constraints.push_back(constraint);
|
|
|
+}
|
|
|
|
|
|
+void PhysicsRigidBody::removeConstraint(PhysicsConstraint* constraint)
|
|
|
+{
|
|
|
+ for (unsigned int i = 0; i < _constraints.size(); i++)
|
|
|
+ {
|
|
|
+ if (_constraints[i] == constraint)
|
|
|
+ {
|
|
|
+ _constraints.erase(_constraints.begin() + i);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
}
|