开发者

How would i set up collision using the Bullet Physics Library?

开发者 https://www.devze.com 2023-02-22 21:21 出处:网络
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 ha

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 j开发者_开发知识库ust wondering if anyone knows how i would be able to implement collision in my game.


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.


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:

  1. Download bullet physics... put it into a folder like the following:

*(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
  1. Goto your MyProject.xcproj and open in Xcode...

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...

  1. 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...

  1. Next goto the MyProjectFolder/bullet-2.78/src and drag the src folder into your project.
  2. Delete the folder named BulletMultiThread...it will eliminate the error of trying to compile some openCL (.cl) files

  3. 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

  4. Build and Run... should compile smoothly to iOS and Mac now...

0

精彩评论

暂无评论...
验证码 换一张
取 消