我使用 box2d 创建了平台。我想要 box2d 身体检测而不是动态检测。
Box2d 形状将执行以下操作
- 在某些条件下检测碰撞
- 在某些情况下不检测碰撞
两种条件都适用于同一 box2d 对象中的不同条件。
任何想法都会有很大帮助。提前致谢。
有多种方法可以控制 Box2d 中发生碰撞时发生的情况。首先要做的是阅读这篇文章 http://www.iforce2d.net/b2dtut/collision-filtering并决定您想要以何种方式控制它。
这为您提供了多种选择。以下是我在上一款游戏中所做的重要工作:
- 游戏中的每个角色(太空飞船、子弹等)都有一个与其关联的特定 C++ 类(称之为“控制器") 这使我能够将 Box2d 主体的逻辑和容器结合在一起。
- 当我创建 Box2d 主体时,我使用用户标签来保存指向控制器的指针。
- 当发生碰撞时,我可以查看两个碰撞的物体并当场决定该怎么做。
要确定“碰撞何时发生”,请使用接触过滤:
/// Implement this class to provide collision filtering. In other words, you can implement
/// this class if you want finer control over contact creation.
class b2ContactFilter
{
public:
virtual ~b2ContactFilter() {}
/// Return true if contact calculations should be performed between these two shapes.
/// @warning for performance reasons this is only called when the AABBs begin to overlap.
virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
};
使用上面的类作为您自己的联系人类的基类,并使用如下内容重写 ShouldCollide(...) 方法:
virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB)
{
bool result = true;
Controller* controllerA = fixtureA->->GetBody()->GetUserData();
Controller* controllerB = fixtureA->->GetBody()->GetUserData();
**Either write code here directly or invoke other function to use controllerA and controllerB to decide if a collision should occur. This can be as simple or complicated as needed. You need to set the value of "result".
return result;
}
当您创建主体时,调用
fixture->SetFilterData(filter);
在连接到要“过滤”的主体的每个固定装置上。过滤器必须“存在”在堆栈之外的其他位置(可能作为控制器本身的静态成员,以便您可以自定义它)。
这个有帮助吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)