Last active
June 15, 2017 19:25
-
-
Save etherealvisage/80c72c0383dd23af0d7c93aa3bfc5730 to your computer and use it in GitHub Desktop.
Simple bullet rigidbody simulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile as: | |
// $ g++ bullet_test.cpp -o bullet_test -I/usr/include/bullet/ -lBulletSoftBody -lBulletDynamics -lBulletCollision -lLinearMath | |
#include <iostream> | |
#include <memory> | |
#include <bullet/btBulletDynamicsCommon.h> | |
int main() { | |
auto broadphase = std::make_shared<btDbvtBroadphase>(); | |
auto collision = std::make_shared<btDefaultCollisionConfiguration>(); | |
auto dispatcher = std::make_shared<btCollisionDispatcher>(collision.get()); | |
auto solver = std::make_shared<btSequentialImpulseConstraintSolver>(); | |
auto world = std::make_shared<btDiscreteDynamicsWorld>( | |
dispatcher.get(), broadphase.get(), solver.get(), collision.get()); | |
world->setGravity(btVector3(0, 0, 0)); | |
btCollisionShape *shape = new btSphereShape(1); | |
btDefaultMotionState *motionState = new btDefaultMotionState( | |
btTransform(btQuaternion(0, 0, 0, 1), btVector3(-1.0, 0, 0))); | |
btVector3 inertia; | |
shape->calculateLocalInertia(1.0, inertia); | |
btRigidBody::btRigidBodyConstructionInfo info( | |
1.0, motionState, shape, inertia); | |
auto body = new btRigidBody(info); | |
body->setLinearVelocity(btVector3(1 / 3.0, 0, 0)); | |
body->setDamping(0, 0); | |
world->addRigidBody(body); | |
for(int i = 0; i < 100; i++) { | |
world->stepSimulation(1 / 30.0, 10); | |
btTransform transform; | |
body->getMotionState()->getWorldTransform(transform); | |
std::cout << "position: " << transform.getOrigin().x() << "," | |
<< transform.getOrigin().y() << "," | |
<< transform.getOrigin().z() << std::endl; | |
body->setLinearVelocity(btVector3(1 / 3.0, 0, 0)); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
position: -0.994444,0,0 | |
position: -0.983333,0,0 | |
position: -0.972222,0,0 | |
position: -0.961111,0,0 | |
position: -0.95,0,0 | |
position: -0.938889,0,0 | |
position: -0.927778,0,0 | |
position: -0.916666,0,0 | |
position: -0.905555,0,0 | |
position: -0.894444,0,0 | |
position: -0.883333,0,0 | |
position: -0.872222,0,0 | |
position: -0.861111,0,0 | |
position: -0.85,0,0 | |
position: -0.838888,0,0 | |
position: -0.827777,0,0 | |
position: -0.816666,0,0 | |
position: -0.805555,0,0 | |
position: -0.794444,0,0 | |
position: -0.783333,0,0 | |
position: -0.772222,0,0 | |
position: -0.76111,0,0 | |
position: -0.749999,0,0 | |
position: -0.738888,0,0 | |
position: -0.727777,0,0 | |
position: -0.716666,0,0 | |
position: -0.705555,0,0 | |
position: -0.694444,0,0 | |
position: -0.683333,0,0 | |
position: -0.672221,0,0 | |
position: -0.66111,0,0 | |
position: -0.649999,0,0 | |
position: -0.638888,0,0 | |
position: -0.627777,0,0 | |
position: -0.616666,0,0 | |
position: -0.605555,0,0 | |
position: -0.594443,0,0 | |
position: -0.583332,0,0 | |
position: -0.572221,0,0 | |
position: -0.56111,0,0 | |
position: -0.549999,0,0 | |
position: -0.538888,0,0 | |
position: -0.527777,0,0 | |
position: -0.516665,0,0 | |
position: -0.505554,0,0 | |
position: -0.494443,0,0 | |
position: -0.483332,0,0 | |
position: -0.472221,0,0 | |
position: -0.46111,0,0 | |
position: -0.449999,0,0 | |
position: -0.438887,0,0 | |
position: -0.427776,0,0 | |
position: -0.416665,0,0 | |
position: -0.405554,0,0 | |
position: -0.394443,0,0 | |
position: -0.383332,0,0 | |
position: -0.372221,0,0 | |
position: -0.361109,0,0 | |
position: -0.349998,0,0 | |
position: -0.338887,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 | |
position: -0.333332,0,0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment