From 136c7bfde8e46e83a29ac9459a819019653ea9ac Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 7 Apr 2014 22:16:19 -0400 Subject: [PATCH] implement hello world from http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World --- .gitignore | 1 + Makefile | 4 ++++ main.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95811e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a736b4 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +all: main + +main: main.cc + g++ -o $@ $$(pkg-config --cflags bullet) $^ $$(pkg-config --libs bullet) diff --git a/main.cc b/main.cc new file mode 100644 index 0000000..28dcb52 --- /dev/null +++ b/main.cc @@ -0,0 +1,62 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + btBroadphaseInterface * broadphase = new btDbvtBroadphase(); + btDefaultCollisionConfiguration * collision_configuration = new btDefaultCollisionConfiguration(); + btCollisionDispatcher * dispatcher = new btCollisionDispatcher(collision_configuration); + btSequentialImpulseConstraintSolver * solver = new btSequentialImpulseConstraintSolver; + btDiscreteDynamicsWorld * dynamics_world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collision_configuration); + dynamics_world->setGravity(btVector3(0, -9.8, 0)); + + btCollisionShape * ground_shape = new btStaticPlaneShape(btVector3(0, 1, 0), 1); + btCollisionShape * fall_shape = new btSphereShape(1); + + btDefaultMotionState * ground_motion_state = + new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0))); + btRigidBody::btRigidBodyConstructionInfo + ground_rigid_body_ci(0, ground_motion_state, ground_shape, btVector3(0, 0, 0)); + btRigidBody * ground_rigid_body = new btRigidBody(ground_rigid_body_ci); + + dynamics_world->addRigidBody(ground_rigid_body); + + btDefaultMotionState * fall_motion_state = + new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 50, 0))); + + btScalar mass = 1; + btVector3 fall_inertia(0, 0, 0); + fall_shape->calculateLocalInertia(mass, fall_inertia); + btRigidBody::btRigidBodyConstructionInfo + fall_rigid_body_ci(mass, fall_motion_state, fall_shape, fall_inertia); + btRigidBody * fall_rigid_body = new btRigidBody(fall_rigid_body_ci); + + dynamics_world->addRigidBody(fall_rigid_body); + + for (int i = 0; i < 300; i++) + { + dynamics_world->stepSimulation(1.0 / 60.0, 10); + + btTransform trans; + fall_rigid_body->getMotionState()->getWorldTransform(trans); + + std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl; + } + + delete fall_rigid_body; + delete fall_motion_state; + + delete ground_rigid_body; + delete ground_motion_state; + + delete fall_shape; + delete ground_shape; + + delete dynamics_world; + delete solver; + delete dispatcher; + delete collision_configuration; + delete broadphase; + + return 0; +}