Change of plans!!
- Pablo Narvaja

- Nov 19, 2019
- 2 min read
I realized that I want to have a simple game running by june 2020 and the physics isn't a big priority system as may be game logic, animation, etc so I switched to Bullet3D for the moment. For the sake of not publish a short post I will show the step to use bullet3d in your project.
Downloading Bullet3D
You might download the zip or clone the bullet repo.
Compiling the library
Use cmake to create the visualstudio solution or for your environment of choise.
I use cmake gui to do it and this are the steps.

After you configure the solution (you can use my configuration for something simple)

Hit generate and when finish the solution file will be available.
Open "BULLET_PHYSICS.sln" or the equivalent for your IDE and compile the project "ALL_BUILD".
Now you will have the libraries need to link to your project. For a simple rigidbody world you only need the following libs:
LinearMath.lib
BulletCollision.lib
BulletDynamics.lib
As for the include directory add the "bullet3-master\src".
Simple World
Now we will create a simple scene with physics.
config = new btDefaultCollisionConfiguration(); dispatcher = new btCollisionDispatcher(config); broadphase = new btDbvtBroadphase(); sol = new btSequentialImpulseConstraintSolver(); // Create the physics world phxWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, sol, config); phxWorld->setGravity({ 0, -9.8f, 0 });
Our physics world is called "phxWorld".
Now we will add some bodies. If the body mass<=0 then is a STATIC body. If the body have a inertia of 0 (default) then is a DYNAMIC body (no rotational reaction). Else is a RIGID body.
// box of size 2 by 2 by 2 boxShape = new btBoxShape({ 1, 1, 1 }); // Calculate the inertia else it will be DYNAMIC boxShape->calculateLocalInertia(1.0f, boxInertia); body = new btRigidBody(1.0f, motionstate, boxShape, boxInertia); // Prevent body for sleeping body->forceActivationState(DISABLE_DEACTIVATION); phxWorld->addRigidBody(body); // Static infinite plane for the ground default inertia is vecZero // is at -1 on Y planeShape = new btStaticPlaneShape({ 0, 1, 0 }, -1); body = new btRigidBody(0.0f, motionstate, planeShape); phxWorld->addRigidBody(body);
In the above code we create a rigid body and add it to the world but what is a motion state?
Motion state is an interface or a virtual class that bullet uses to give you the world transform post step and for you to give the initial transform of the object (if it is a static or kinematic object you will give it to bullet every frame). So what you have to do is inherit from btMotionState class. This is how a basic motion state is.
struct Transform : public btMotionState { glm::mat4 matrix; void getWorldTransform(btTransform& worldTrans) const override { worldTrans.setFromOpenGLMatrix(&matrix[0][0]); } //Bullet only calls the update of worldtransform for active objects void setWorldTransform(const btTransform& worldTrans) override { worldTrans.getOpenGLMatrix(&matrix[0][0]); } };
Alright now we need to actually simulate the world and that is done when every frame we call:
phxWorld->stepSimulation(in_deltaTime);
and with all this is ready to run the program!
NOTE: For more information about how to use bullet3d I strongly recomend this pdf.
That's all for this post, folks. Bye!

Comments