|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#include <btBulletDynamicsCommon.h> |
| 5 | + |
| 6 | +int main() { |
| 7 | + int i, j; |
| 8 | + |
| 9 | + btDefaultCollisionConfiguration* collisionConfiguration |
| 10 | + = new btDefaultCollisionConfiguration(); |
| 11 | + btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); |
| 12 | + btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase(); |
| 13 | + btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; |
| 14 | + btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld( |
| 15 | + dispatcher, overlappingPairCache, solver, collisionConfiguration); |
| 16 | + |
| 17 | + dynamicsWorld->setGravity(btVector3(0, -10, 0)); |
| 18 | + btCollisionShape* groundShape = new btBoxShape( |
| 19 | + btVector3(btScalar(50.0), btScalar(50.0), btScalar(50.0))); |
| 20 | + btAlignedObjectArray<btCollisionShape*> collisionShapes; |
| 21 | + collisionShapes.push_back(groundShape); |
| 22 | + btTransform groundTransform; |
| 23 | + groundTransform.setIdentity(); |
| 24 | + groundTransform.setOrigin(btVector3(0, 0, 0)); |
| 25 | + |
| 26 | + // Shpere at position 0, 10, 0. |
| 27 | + { |
| 28 | + btCollisionShape *colShape = new btSphereShape(btScalar(1.0)); |
| 29 | + collisionShapes.push_back(colShape); |
| 30 | + btTransform startTransform; |
| 31 | + startTransform.setIdentity(); |
| 32 | + startTransform.setOrigin(btVector3(0, 10, 0)); |
| 33 | + btVector3 localInertia(0, 0, 0); |
| 34 | + btScalar mass(1.0f); |
| 35 | + colShape->calculateLocalInertia(mass, localInertia); |
| 36 | + btDefaultMotionState *myMotionState = new btDefaultMotionState(startTransform); |
| 37 | + btRigidBody *body = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo( |
| 38 | + mass, myMotionState, colShape, localInertia)); |
| 39 | + dynamicsWorld->addRigidBody(body); |
| 40 | + } |
| 41 | + |
| 42 | + // Main loop. Update state and print it. |
| 43 | + for (i = 0; i < 100; ++i) { |
| 44 | + dynamicsWorld->stepSimulation(1.0f / 60.0f, 10); |
| 45 | + for (j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; --j) { |
| 46 | + btCollisionObject *obj = dynamicsWorld->getCollisionObjectArray()[j]; |
| 47 | + btRigidBody *body = btRigidBody::upcast(obj); |
| 48 | + btTransform trans; |
| 49 | + if (body && body->getMotionState()) { |
| 50 | + body->getMotionState()->getWorldTransform(trans); |
| 51 | + } else { |
| 52 | + trans = obj->getWorldTransform(); |
| 53 | + } |
| 54 | + printf("%d = %7.3f, %7.3f, %7.3f\n", |
| 55 | + j, |
| 56 | + float(trans.getOrigin().getX()), |
| 57 | + float(trans.getOrigin().getY()), |
| 58 | + float(trans.getOrigin().getZ())); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Cleanup. |
| 63 | + for (i = dynamicsWorld->getNumCollisionObjects() - 1; i >= 0; --i) { |
| 64 | + btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i]; |
| 65 | + btRigidBody* body = btRigidBody::upcast(obj); |
| 66 | + if (body && body->getMotionState()) { |
| 67 | + delete body->getMotionState(); |
| 68 | + } |
| 69 | + dynamicsWorld->removeCollisionObject( obj ); |
| 70 | + delete obj; |
| 71 | + } |
| 72 | + for (i = 0; i < collisionShapes.size(); ++i) { |
| 73 | + btCollisionShape* shape = collisionShapes[i]; |
| 74 | + collisionShapes[i] = 0; |
| 75 | + delete shape; |
| 76 | + } |
| 77 | + delete dynamicsWorld; |
| 78 | + delete solver; |
| 79 | + delete overlappingPairCache; |
| 80 | + delete dispatcher; |
| 81 | + delete collisionConfiguration; |
| 82 | + collisionShapes.clear(); |
| 83 | +} |
0 commit comments