gpt4 book ai didi

c++ - 我将如何使用 Bullet Physics Library 设置碰撞?

转载 作者:行者123 更新时间:2023-11-30 03:07:52 29 4
gpt4 key购买 nike

嘿,我在我的 opengl/sfml 游戏中设置碰撞时有点“延迟”。它没有太大的错误,只是寻求一些帮助。我正在使用 Bullet Physics (这是 API 引用)我一直在研究不同的函数和类。然后我注意到库中包含演示,所以在查看它们时我并不完全理解它们..

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

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

最佳答案

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

#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 控制物体的移动方式。

关于c++ - 我将如何使用 Bullet Physics Library 设置碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559482/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com