具有多个对象组的物理引擎

physics engine with multiple object groups

我的 java 应用程序需要一个用于水平世界的 2d 物理引擎,但是,看看 jbox2d 和 dyn4j,它们似乎没有提供我需要的开箱即用的东西。具体来说,他们不支持定义哪些对象组可以与其他对象组发生碰撞。考虑这个简化模型:子弹可以与盒子碰撞。飞机穿过箱子,但它们可能会与子弹相撞。

如何排除某些对象组在物理引擎中发生碰撞?

box2d(和jbox2d分别)有一个解决方案。 “PreSolve”方法允许在碰撞前禁用接触。请参阅 this question on gamedev,它与此处描述的问题几乎相同。

来自文档:

Pre-Solve Event

This is called after collision detection, but before collision resolution. This gives you a chance to disable the contact based on the current configuration. For example, you can implement a one-sided platform using this callback and calling b2Contact::SetEnabled(false). The contact will be re-enabled each time through collision processing, so you will need to disable the contact every time-step. The pre-solve event may be fired multiple times per time step per contact due to continuous collision detection.

Dyn4j 具有来自 Dyn4j 存储库的 CategoryFilter. You create CategoryFilters with two longs and set those in your Fixtures. It's a little funny how the filters work because the category and mask are used in their binary forms to determine who can collide with who. To see this in practice check out this @Test。 Dyn4j 在 docs 中也提到了这一点:

There are three Filter implementations provided, the Filter.DEFAULT_FILTER, the CategoryFilter (just like Box2D’s collision filter, int category + mask), and the TypeFilter.

所以我假设 Box2D 也有这个(以及 jBox2d 的扩展)。我想说的是,几乎任何 Box2D 或 Dyn4j 级别的物理引擎都将以某种形式具有这种能力。