Tuesday, July 17, 2018

c++ - How do I integrate bullet physics into my game?


I downloaded the release file found here, but I'm just not sure where to start. In my game I have a number of oblongs and a sphere, I want all of these to collide with one another. (They all have meshes and associated bounding volumes)


edit - I just realised the cuboids actually have AABB's associated with them. I guess I'll have to change that at least.


What are the steps I need to follow?




Answer



Here's the basic steps you'll need to folow:




  1. First create a world object (i.e. btDiscreteDynamicsWorld) to drive your physics simulation.




  2. You should already have a class such as GameObject that perhaps stores a model along with its bounding box and position/orientation in the world. Replace the position/orientation information with an instance of a physics body object (i.e. btRigidBody) and when rendering the model, use the information provided by the body instead. When creating the body you'll need to provide its mass and collision shape which should match the bounding volumes you already have. I've talked about this in your other question too.





  3. Add each of your game object's bodies to the world object.




  4. Update the simulation by stepping the world (i.e. calling stepSimulation on your world).




And take some time to read the manual as it has a lot of information too.




Edit to address comment




I cringe before I ask but... how do I do the very very basic stuff like simply including the right files in my game? If you need specifics I'm looking for the files that will deal with rigid body collisions. Also thank you very much for the help so far!



Well, it would appear from your comment that you've never worked with external C++ libraries before. It's been a while since I did this, but this is how I remember the process to be (in Visual Studio).


For starters I quote the manual which already explains what you need:


- #include “btBulletDynamicsCommon.h” in your source file 
- Required include path: Bullet/src folder
- Required libraries: BulletDynamics, BulletCollision, LinearMath

And here's the basic steps you will have to follow to fulfill the conditions above...


1. Preparation Step



First of all, extract the file you downloaded into a known directory, e.g. C:\Bullet. You'll need to know the path to this folder later.


Inside there are two folders that you will need to reference in your project. The first is the src folder which holds the .h header files that you will include in your source code. The second is the lib folder which will hold the .lib library files that you will link to your project. Notice that including and linking are two different things.


But the lib folder should be empty for now, because you haven't built the engine yet. So refer to the manual on how to build the project using CMake and Visual Studio for instance. It's on page 7.


After you succeed, you should have at least three files in the libfolder: BulletDynamics.lib, BulletCollision.lib and LinearMath.lib (I am guessing from the manual). Now to make the connection between Bullet and your project...


2. Include Headers


First of all, you'll need to add the C:\Bullet\src folder to your project's include path options. You can do so in Visual Studio by right-clicking on your project, navigating to Configuration Properties -> C/C++ -> General -> Additional Include Directories and writing the directory path in there.


After setting that up, you can simply do #include “btBulletDynamicsCommon.h” on your code. That header seems to serve as an hub to all features you require.


3. Link Libraries


Finally you'll need to link the libraries mentioned above. Frist, under Configuration Properties -> Linker -> General -> Additional Library Directories add the path to the lib folder, or in other words, C:\Bullet\lib.


Then, under Configuration Properties -> Linker -> Input-> Additional Dependencies, add the name of the libraries to the list of dependencies. Separate each library with a ; and don't forget the file extension. So for instance, you might add this to the end of the list: BulletDynamics.lib;BulletCollision.lib;LinearMath.lib.



After this you should be able to build and run your project.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...