如何让光线投射忽略 Box2d 中的对象?

How do I get raycasting to ignore an object in Box2d?

我在 python Box2D 中有以下 RayCastCallback 函数。

Class RayCastCallback(Box2D.b2.rayCastCallback):
  def ReportFixture(self, fixture, point, normal, fraction):
    if (fixture.filterData.categoryBits & 1) == 0:
      return 1
    self.p2 = point
    self.fraction = fraction
    return 0

我通过为每个角度实例化一个然后说

来使用它
ray_cast_callback.p1 = position
ray_cast_callback.fraction = 1.0
ray_cast_callback.p2 = (position[0] + math.cos(radians)*range, position[1] + math.sin(radians)*range)
world.RayCast(ray_cast_callback, ray_cast_callback.p1, ray_cast_callback.p2)

这很好,但我的问题是,在我设置的世界中,有多种不同类型的静态和动态对象,我希望它排除特定静态对象的实例,以便 RayCast直接穿过它们。

我该怎么做?

通常在光线投射中,以下值用作 return 类型

-1 to filter, 0 to terminate, fraction to clip the ray for closest hit, 1 to continue

您可以检查对象类型,如果它是静态的 return -1 以忽略此对象。