简体   繁体   English

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

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

I have the following RayCastCallback function in python 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

I use it by instantiating one for each angle and then saying 我通过为每个角度实例化一个然后说

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)

This works well, but my problem is that in the world I've set up, there are multiple different types of static and dynamic objects and I want it to exclude instances of a particular static object so that the RayCast just goes right through them. 这很好用,但是我的问题是,在我设置的世界中,存在多种不同类型的静态和动态对象,并且我希望它排除特定静态对象的实例,以便RayCast可以正确地通过它们。

How do I do this? 我该怎么做呢?

Usually in raycasting the following values are used as a return type 通常在光线投射中,以下值用作返回类型

-1 to filter, 0 to terminate, fraction to clip the ray for closest hit, 1 to continue -1进行过滤,0结束,小数点剪辑射线以使其最接近,1继续

You can check the object type and if it is static return -1 to have this object ignored. 您可以检查对象类型,如果它是静态的,则返回-1以使该对象被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM