user592419
user592419

Reputation: 5223

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

I have the following RayCastCallback function in python Box2D.

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.

How do I do this?

Upvotes: 2

Views: 531

Answers (1)

dfour
dfour

Reputation: 1376

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

You can check the object type and if it is static return -1 to have this object ignored.

Upvotes: 2

Related Questions