Reputation: 1075
hey im having a bit of a "delay" in setting up some collision in my opengl/sfml game. its not much of an error, just asking for some help. Im using Bullet Physics(this is the API reference) and i have been looking at the different functions and classes. then i noticed that there are demos included in the lbrary, so while looking them over i dont completely understand them..
the main library that they recommend me use is CollisionInterfaceDemo since i have already used GLM for models in opengl, and sfml for 2D purposes and the window.
im just wondering if anyone knows how i would be able to implement collision in my game.
Upvotes: 1
Views: 4133
Reputation: 696
How to insert Bullet Physics into iOS: iOS OpenGL BulletPhysics Sample Code
Download the sample code or follow on a new project form Xcode with iOS openGL template:
*(notice that there is a folder for the project that contains the Xcode project file)
/MyProjectFolder/bullet-2.78/
/MyProjectFolder/MyProject/MyProject.xcproj
1.5. Run CMake in the physics directory to compile the frameworks (assuming you installed cmake already CMake) This step is optional with the sample code I uploaded since it already included the compiled frameworks in it....that made the file 100 megs but what is 100 megs these days anyway?
cmake . -G "Unix Makefiles" -DINSTALL_LIBS=ON -DBUILD_SHARED_LIBS=ON -DFRAMEWORK=ON -DCMAKE_OSX_ARCHITECTURES='i386;x86_64' -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/Library/Frameworks -DCMAKE_INSTALL_NAME_DIR=/Library/Frameworks -DBUILD_DEMOS:BOOL=OFF
make -j4
sudo make install
in XCode goto any file you wish to add the physics code to... you must understand that cpp files are c++ and .m .h files are generally cocoa. You must change the Class you wish to add the physics engine code to have a .mm extension signifying it should be compiled as Objective-C++ code...
In the particular class you want to add the physics which is now an Objective-C++ file or a cpp file, add the line
#include "btBulletDynamicsCommon.h"
and you should compile...the error is that the file is not found...
Delete the folder named BulletMultiThread...it will eliminate the error of trying to compile some openCL (.cl) files
Last step, copy the following frameworks from the src folder of your bullet physics installation into your project:
/MyProjectFolder/bullet-2.78/LinearMath/LinearMath.framework /MyProjectFolder/bullet-2.78/BulletCollision/BulletCollision.framework /MyProjectFolder/bullet-2.78/LinearMath/LinearMath.framework
Build and Run... should compile smoothly to iOS and Mac now...
Upvotes: 1
Reputation: 12459
Not sure if this is what you're after, but this is my setup code for basic rigid body physics:
#include "btBulletDynamicsCommon.h"
...
m_pBroadphase = new btDbvtBroadphase();
m_pCollisionConfig = new btDefaultCollisionConfiguration();
m_pCollisionDispatcher = new btCollisionDispatcher(m_pCollisionConfig);
m_pSolver = new btSequentialImpulseConstraintSolver();
m_pDynamicsWorld = new btDiscreteDynamicsWorld(m_pCollisionDispatcher,
m_pBroadphase,
m_pSolver,
m_pCollisionConfig);
After that it's just a matter of adding bodies to the world...
btRigidBody::btRigidBodyConstructionInfo info;
// set physical properties like mass, coefficient of restitution etc
info.m_mass = 10;
info.m_restitution = 0.5;
...
// Use a motion state to link the physics body with the graphics object.
// This is the glue between Bullet and your code, called by bullet to update the
// position & orientation of the object
info.m_motionState = new YourCustomMotionState(...) ;
btRigidBody* pRigidBody = new btRigidBody(info);
m_pDynamicsWorld->addRigidBody(pRigidBody);
...and then updating the world state every frame.
m_pDynamicsWorld->stepSimulation(deltaTime, m_maxSubSteps);
That will give you a simple physics simulation with rigid bodies that collide and bounce off each other, with Bullet in control of the way bodies move.
Upvotes: 2