如何使用 Bullet 物理库设置碰撞?

发布于 2024-10-30 18:41:21 字数 340 浏览 0 评论 0原文

嘿,我在 opengl/sfml 游戏中设置一些碰撞时遇到了一些“延迟”。这并不是什么大错误,只是寻求一些帮助。我正在使用子弹物理学(这是API参考),我一直在研究不同的功能和类。然后我注意到库中包含演示,所以在查看它们时我并不完全理解它们。

他们推荐我使用的主要库是 CollisionInterfaceDemo 因为我已经使用 GLM 用于 opengl 中的模型,并使用 sfml 用于 2D 目的和窗户。

我只是想知道是否有人知道我如何在游戏中实现碰撞。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-11-06 18:41:21

不确定这是否是您想要的,但这是我的基本刚体物理设置代码:

#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);

之后只需向世界添加物体......

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);

然后每帧更新世界状态即可。

m_pDynamicsWorld->stepSimulation(deltaTime, m_maxSubSteps);

这将为您提供一个简单的物理模拟,其中刚体相互碰撞和反弹,而 Bullet 则控制物体的移动方式。

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.

梦晓ヶ微光ヅ倾城 2024-11-06 18:41:21

如何将 BulletPhysics 插入 iOS:iOS OpenGL BulletPhysics 示例代码

下载示例代码或按照新项目进行操作使用 iOS openGL 模板形成 Xcode:

  1. 下载子弹物理... 将其放入如下文件夹中:

*(注意,有一个包含 Xcode 项目文件的项目文件夹)

/MyProjectFolder/bullet-2.78/
/MyProjectFolder/MyProject/MyProject.xcproj

1.5。在物理目录中运行 CMake 来编译框架(假设您已经安装了 cmake CMake )对于我上传的示例代码来说,此步骤是可选的,因为它已经包含了已编译的框架....这使得文件有 100 兆,但现在 100 兆是多少?

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. 转到您的 MyProject.xcproj 并在 Xcode 中打开...

在 XCode 中转到您想要添加物理代码的任何文件...您必须了解 cpp 文件是 c++,而 .m .h 文件通常是 cocoa。您必须更改要添加物理引擎代码的类,使其具有 .mm 扩展名,表示它应编译为 Objective-C++ 代码...

  1. 在您要添加现在是目标的物理的特定类中-C++ 文件或 cpp 文件,添加行

    <前><代码>#include“btBulletDynamicsCommon.h”

,您应该编译...错误是找不到该文件...

  1. 接下来转到 MyProjectFolder/bullet-2.78/src 并将 src 文件夹拖到您的项目中。
  2. 删除名为 BulletMultiThread 的文件夹...它将消除尝试编译某些 openCL (.cl) 文件的错误

  3. 最后一步,将以下框架从子弹物理安装的 src 文件夹复制到您的项目中:

    /MyProjectFolder/bullet-2.78/LinearMath/LinearMath.framework
    /MyProjectFolder/bullet-2.78/BulletCollision/BulletCollision.framework
    /MyProjectFolder/bullet-2.78/LinearMath/LinearMath.framework

  4. 构建并运行...现在应该可以顺利编译到 iOS 和 Mac...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文