5 #ifndef GLVERTEX_PHYSICS_H
6 #define GLVERTEX_PHYSICS_H
10 #ifndef HAVE_BULLET_PHYSICS
18 #include <btBulletDynamicsCommon.h>
19 #include <BulletCollision/CollisionShapes/btShapeHull.h>
22 class lgl_BulletPhysics
30 broadphase =
new btDbvtBroadphase();
33 collisionConfiguration =
new btDefaultCollisionConfiguration();
34 dispatcher =
new btCollisionDispatcher(collisionConfiguration);
37 solver =
new btSequentialImpulseConstraintSolver;
40 dynamicsWorld =
new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
43 groundRigidBody = NULL;
50 dynamicsWorld->removeRigidBody(groundRigidBody);
51 deleteBulletData(groundRigidBody);
58 delete collisionConfiguration;
62 void setBulletGroundPlane(
vec3 o,
vec3 n,
double restitution = 1.0)
66 dynamicsWorld->removeRigidBody(groundRigidBody);
67 deleteBulletData(groundRigidBody);
68 groundRigidBody = NULL;
73 btCollisionShape *groundShape =
new btStaticPlaneShape(btVector3(n.x, n.y, n.z), 0);
74 btDefaultMotionState *groundMotionState =
new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(o.x, o.y, o.z)));
76 btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
77 groundRigidBody =
new btRigidBody(groundRigidBodyCI);
78 groundRigidBody->setRestitution(restitution);
80 dynamicsWorld->addRigidBody(groundRigidBody);
85 void setBulletGravity(
vec3 g)
87 dynamicsWorld->setGravity(btVector3(g.x, g.y, g.z));
95 btTransform offset(btQuaternion(0, 0, 0, 1), btVector3(-center.x, -center.y, -center.z));
97 btTransform transform;
100 btCollisionShape *shape =
new btBoxShape(btVector3(ext2.x, ext2.y, ext2.z));
101 btDefaultMotionState *motionState =
new btDefaultMotionState(transform, offset);
104 btVector3 inertia(0, 0, 0);
105 shape->calculateLocalInertia(mass, inertia);
107 btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, shape, inertia);
108 btRigidBody *body =
new btRigidBody(rigidBodyCI);
110 body->setDamping(0.01, 0.05);
111 body->setSleepingThresholds(0.25, 0.25);
121 btTransform offset(btQuaternion(0, 0, 0, 1), btVector3(-center.x, -center.y, -center.z));
123 btTransform transform;
126 btCollisionShape *shape =
new btSphereShape(radius);
127 btDefaultMotionState *motionState =
new btDefaultMotionState(transform, offset);
130 btVector3 inertia(0, 0, 0);
131 shape->calculateLocalInertia(mass, inertia);
133 btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, shape, inertia);
134 btRigidBody *body =
new btRigidBody(rigidBodyCI);
136 body->setDamping(0.01, 0.25);
137 body->setSleepingThresholds(0.25, 0.25);
147 btTransform offset(btQuaternion(0, 0, 0, 1), btVector3(-center.x, -center.y, -center.z));
149 btTransform transform;
152 btVector3 pos(0, 0, 0);
154 btCollisionShape *shape =
new btMultiSphereShape(&pos, &rad, 1);
155 shape->setLocalScaling(btVector3(ext.x, ext.y, ext.z));
158 btDefaultMotionState *motionState =
new btDefaultMotionState(transform, offset);
161 btVector3 inertia(0, 0, 0);
162 shape->calculateLocalInertia(mass, inertia);
164 btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, shape, inertia);
165 btRigidBody *body =
new btRigidBody(rigidBodyCI);
167 body->setDamping(0.01, 0.05);
168 body->setSleepingThresholds(0.25, 0.25);
177 btTransform offset(btQuaternion(0, 0, 0, 1), btVector3(-center.x, -center.y, -center.z));
179 btTransform transform;
182 btConvexHullShape *shape =
new btConvexHullShape();
183 for (
unsigned int i=0; i<points.size(); i++) shape->addPoint(btVector3(points[i].x, points[i].y, points[i].z));
186 btDefaultMotionState *motionState =
new btDefaultMotionState(transform, offset);
189 btVector3 inertia(0, 0, 0);
190 shape->calculateLocalInertia(mass, inertia);
192 btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, shape, inertia);
193 btRigidBody *body =
new btRigidBody(rigidBodyCI);
194 body->setSleepingThresholds(0.25f, 0.25f);
203 btTransform offset(btQuaternion(0, 0, 0, 1), btVector3(-center.x, -center.y, -center.z));
205 btTransform transform;
208 btConvexHullShape *shape =
new btConvexHullShape();
209 for (
unsigned int i=0; i<points.size(); i++) shape->addPoint(btVector3(points[i].x, points[i].y, points[i].z));
212 btShapeHull *hull =
new btShapeHull(shape);
213 hull->buildHull(shape->getMargin());
216 btConvexHullShape *simplified =
new btConvexHullShape();
217 for (
int i=0; i<hull->numVertices(); i++) simplified->addPoint(hull->getVertexPointer()[i]);
218 simplified->setMargin(0);
221 btDefaultMotionState *motionState =
new btDefaultMotionState(transform, offset);
224 btVector3 inertia(0, 0, 0);
225 simplified->calculateLocalInertia(mass, inertia);
227 btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, motionState, simplified, inertia);
228 btRigidBody *body =
new btRigidBody(rigidBodyCI);
229 body->setSleepingThresholds(0.25f, 0.25f);
235 void setBulletMass(btRigidBody *body, btScalar mass)
237 btVector3 inertia(0, 0, 0);
238 body->getCollisionShape()->calculateLocalInertia(mass, inertia);
239 body->setMassProps(mass, inertia);
244 void setBulletFactor(btRigidBody *body,
float factor)
248 body->setLinearVelocity(btVector3(0, 0, 0));
249 body->setAngularVelocity(btVector3(0, 0, 0));
250 body->setLinearFactor(btVector3(0, 0, 0));
251 body->setAngularFactor(btVector3(0, 0, 0));
252 body->setActivationState(DISABLE_DEACTIVATION);
256 body->setLinearFactor(btVector3(factor, factor, factor));
257 body->setAngularFactor(btVector3(factor, factor, factor));
258 body->setActivationState(WANTS_DEACTIVATION);
263 void addBulletBody(btRigidBody *body)
265 dynamicsWorld->addRigidBody(body);
269 void removeBulletBody(btRigidBody *body)
271 dynamicsWorld->removeRigidBody(body);
275 void updateBullet(
double dt)
277 dynamicsWorld->stepSimulation(dt, 3, 1.0/120);
286 body->getCenterOfMassTransform().getOpenGLMatrix(matrix);
301 btTransform transform;
302 transform.setFromOpenGLMatrix((
const float *)
mat4f(m));
303 body->setWorldTransform(transform);
304 body->setLinearVelocity(btVector3(0, 0, 0));
305 body->setAngularVelocity(btVector3(0, 0, 0));
314 btVector3 velocity = body->getLinearVelocity();
315 return(
vec3(velocity.x(), velocity.y(), velocity.z()));
326 btVector3 velocity(v.x, v.y, v.z);
327 body->setLinearVelocity(velocity);
337 body->applyForce(btVector3(force.x, force.y, force.z), btVector3(rel_pos.x, rel_pos.y, rel_pos.z));
347 body->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z));
357 body->applyTorqueImpulse(btVector3(torque.x, torque.y, torque.z));
364 body->getCollisionShape()->setUserPointer(pobj);
376 btVector3 f(from.x, from.y, from.z);
377 btVector3 t(to.x, to.y, to.z);
378 btCollisionWorld::ClosestRayResultCallback rayCallback(f, t);
379 dynamicsWorld->rayTest(f, t, rayCallback);
381 if (rayCallback.hasHit()) {
382 const btRigidBody *body = btRigidBody::upcast(rayCallback.m_collisionObject);
383 btVector3 hit = rayCallback.m_hitPointWorld;
384 to =
vec3(hit.x(), hit.y(), hit.z());
385 return(getUserPointer(body));
395 deleteBulletData(data);
399 static void deleteBulletData(btRigidBody *data)
401 delete data->getMotionState();
402 delete data->getCollisionShape();
408 btDiscreteDynamicsWorld *dynamicsWorld;
409 btSequentialImpulseConstraintSolver *solver;
410 btBroadphaseInterface *broadphase;
411 btCollisionDispatcher *dispatcher;
412 btDefaultCollisionConfiguration *collisionConfiguration;
414 btRigidBody *groundRigidBody;
426 lgl_BulletPhysics::deleteBulletData(
this);
433 btRigidBody *body = lgl_BulletPhysics::createBulletBox(
this);
434 lgl_BulletPhysics::setUserPointer(body,
this);
438 virtual mat4 getPhysicalTransformation()
440 return(lgl_BulletPhysics::getBulletTransformation(
this));
443 virtual void transformationChanged(
const mat4 &m)
445 lgl_BulletPhysics::setBulletTransformation(
this, m);
448 virtual vec3 getPhysicalVelocity()
450 return(lgl_BulletPhysics::getBulletVelocity(
this));
453 virtual void velocityChanged(
const vec3 &v)
455 lgl_BulletPhysics::setBulletVelocity(
this, v);
460 lgl_BulletPhysics::applyBulletForce(
this, force, rel_pos);
465 lgl_BulletPhysics::applyBulletImpulse(
this, impulse);
470 lgl_BulletPhysics::applyBulletTorque(
this, torque);
484 lgl_BulletPhysics::deleteBulletData(
this);
491 btRigidBody *body = lgl_BulletPhysics::createBulletBox(
this);
492 lgl_BulletPhysics::setUserPointer(body,
this);
496 virtual mat4 getPhysicalTransformation()
498 return(lgl_BulletPhysics::getBulletTransformation(
this));
501 virtual void transformationChanged(
const mat4 &m)
503 lgl_BulletPhysics::setBulletTransformation(
this, m);
506 virtual vec3 getPhysicalVelocity()
508 return(lgl_BulletPhysics::getBulletVelocity(
this));
511 virtual void velocityChanged(
const vec3 &v)
513 lgl_BulletPhysics::setBulletVelocity(
this, v);
518 lgl_BulletPhysics::applyBulletForce(
this, force, rel_pos);
523 lgl_BulletPhysics::applyBulletImpulse(
this, impulse);
528 lgl_BulletPhysics::applyBulletTorque(
this, torque);
542 lgl_BulletPhysics::deleteBulletData(
this);
549 btRigidBody *body = lgl_BulletPhysics::createBulletBox(
this);
550 lgl_BulletPhysics::setUserPointer(body,
this);
554 virtual mat4 getPhysicalTransformation()
556 return(lgl_BulletPhysics::getBulletTransformation(
this));
559 virtual void transformationChanged(
const mat4 &m)
561 lgl_BulletPhysics::setBulletTransformation(
this, m);
564 virtual vec3 getPhysicalVelocity()
566 return(lgl_BulletPhysics::getBulletVelocity(
this));
569 virtual void velocityChanged(
const vec3 &v)
571 lgl_BulletPhysics::setBulletVelocity(
this, v);
576 lgl_BulletPhysics::applyBulletForce(
this, force, rel_pos);
581 lgl_BulletPhysics::applyBulletImpulse(
this, impulse);
586 lgl_BulletPhysics::applyBulletTorque(
this, torque);
600 lgl_BulletPhysics::deleteBulletData(
this);
610 body = lgl_BulletPhysics::createBulletSphere(
this);
612 body = lgl_BulletPhysics::createBulletEllipsoid(
this);
614 lgl_BulletPhysics::setUserPointer(body,
this);
619 virtual mat4 getPhysicalTransformation()
621 return(lgl_BulletPhysics::getBulletTransformation(
this));
624 virtual void transformationChanged(
const mat4 &m)
626 lgl_BulletPhysics::setBulletTransformation(
this, m);
629 virtual vec3 getPhysicalVelocity()
631 return(lgl_BulletPhysics::getBulletVelocity(
this));
634 virtual void velocityChanged(
const vec3 &v)
636 lgl_BulletPhysics::setBulletVelocity(
this, v);
641 lgl_BulletPhysics::applyBulletForce(
this, force, rel_pos);
646 lgl_BulletPhysics::applyBulletImpulse(
this, impulse);
651 lgl_BulletPhysics::applyBulletTorque(
this, torque);
661 typedef lgl_PhysicalObjects<btRigidBody>::iterator iterator;
665 : lgl_BulletPhysics(),
668 setBulletGroundPlane(ground_o, ground_n, ground_r);
678 setBulletGroundPlane(ground_o, ground_n, ground_r);
685 setBulletGravity(ground_n*ground_g);
711 removeBulletBody(body);
717 setBulletFactor(body, yes?0:1);
720 virtual void updatePhysicalObjects(
double dt)