我刚刚开始使用pyBox2D并正在进行自上而下的游戏光线投射以查看对象,类似于http://ncase.me/sight-and-light/。
但是有些光线正在被投射到一个物体上并继续穿过物体并撞击其后面的其他物体
http://imgur.com/C3Wyg4T
正如你可以看到一些光线穿过物体,但当我移动动态框时,它开始工作(ish)但有时会出现故障
这是我的光线投射回调
class lightRayCasting(b2RayCastCallback):
def __init__(self, point, ignoreBody):
b2RayCastCallback.__init__(self)
self.hit = False
self.point = point
self.ignoreBody = ignoreBody
def ReportFixture(self, fixture, point, normal, fraction):
if fixture.body != self.ignoreBody and fixture.filterData.groupIndex != -1 and not self.hit:
self.hit = True
self.point = point
return 0
return 1
这就是我所说的光线投射
def lightRayCasting(self, position, **kwargs):
points = []
for ray in range(kwargs.get("n", 100)):
angle = (ray / kwargs.get("n", 100) * 360 * (b2_pi / 180))
rayDirection = b2Vec2(math.sin(angle), math.cos(angle)) * kwargs.get("length", 10)
callback = lightRayCasting(rayDirection, ignoreBody = kwargs.get("ignoreBody", None))
self.world.RayCast(callback, self.convertPosition(position), rayDirection)
points.append(self.convertPosition(callback.point, 1))
if kwargs.get("debugDraw", False):
if callback.point != rayDirection: pygame.draw.aaline(self.surface, (255, 0, 0), position, self.convertPosition(callback.point, 1))
else: pygame.draw.aaline(self.surface, (0, 255, 0), position, self.convertPosition(callback.point, 1))
if not kwargs.get("debugDraw", False):
pygame.gfxdraw.filled_polygon(self.surface, points, (255, 255, 255))
pygame.gfxdraw.aapolygon(self.surface, points, (255, 255, 255))
self.convertPosition只是将像素转换为米,以供Box2D使用。我不明白为什么它有时会起作用,但有时它不起作用。身体是否必须清醒以便进行光线投射才能对它们进行操作?
答案 0 :(得分:0)
我已经修复了我的问题,对于任何想知道我从回调中获取分数的人以及每次回调检查它是否小于当前,如果设置了交叉点到回调上的点
def ReportFixture(self, fixture, point, normal, fraction):
if fraction < self.fraction:
if not fixture in self.ignoreFixtures:
if not fixture.filterData.groupIndex in self.ignoreIndexes:
self.hit = True
self.point = point
self.fraction = fraction
return 1