sky*_*all 6 python pygame artificial-intelligence collision-detection
我尝试在 Pygame 中使用预定义的轨道掩码实现光束碰撞检测。我的最终目标是让 AI 汽车模型能够看到它所行驶的轨道:
这是我当前的代码,我发射光束来掩盖并尝试找到重叠:
import math
import sys
import pygame as pg
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pg.init()
beam_surface = pg.Surface((500, 500), pg.SRCALPHA)
def draw_beam(surface, angle, pos):
# compute beam final point
x_dest = 250 + 500 * math.cos(math.radians(angle))
y_dest = 250 + 500 * math.sin(math.radians(angle))
beam_surface.fill((0, 0, 0, 0))
# draw a single beam to the beam surface based on computed final point
pg.draw.line(beam_surface, BLUE, (250, 250), (x_dest, y_dest))
beam_mask = pg.mask.from_surface(beam_surface)
# find overlap between "global mask" and current beam mask
hit = mask.overlap(beam_mask, (pos[0] - 250, pos[1] - 250))
if hit is not None:
pg.draw.line(surface, BLUE, mouse_pos, hit)
pg.draw.circle(surface, GREEN, hit, 3)
surface = pg.display.set_mode((500, 500))
mask_surface = pg.image.load("../assets/mask.png")
mask = pg.mask.from_surface(mask_surface)
clock = pg.time.Clock()
while True:
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
sys.exit()
mouse_pos = pg.mouse.get_pos()
surface.fill((0, 0, 0))
surface.blit(mask_surface, mask_surface.get_rect())
for angle in range(0, 120, 30):
draw_beam(surface, angle, mouse_pos)
pg.display.update()
clock.tick(30)
Run Code Online (Sandbox Code Playgroud)
让我们描述一下代码片段中发生的事情。一个接一个地,我绘制光束beam_surface,用它们制作蒙版,并找到与由一个矩形和一个圆形(gif 中的黑色)定义的背景蒙版重叠的部分。如果有一个“命中点”(两个面具之间的重叠点),我会用一条连接命中点和鼠标位置的线来绘制它。
它适用于角度<0,90>:
但它不适用于范围内的角度<90,360>:
Pygame 的overlap()文档说明了这一点:
从左上角开始,它检查第一行的位 0 到 W - 1 ((0, 0) 到 (W - 1, 0)) 然后继续到下一行 ((0, 1) 到 (W - 1 , 1))。检查完整个列块后,它会继续下一个(W 到 2 * W - 1)。
这意味着只有当光束大约从左上角击中掩模时,这种方法才会起作用。您对如何使其适用于所有情况有什么建议吗?这通常是解决这个问题的好方法吗?
如果光线轴的 x 和 y 分量指向正方向,则您的方法工作正常,但如果指向负方向,则方法失败。正如您所指出的,这是由pygame.mask.Mask.overlap 的工作方式引起的:
从左上角开始,它检查第一行的位 0 到 W - 1 ((0, 0) 到 (W - 1, 0)) 然后继续到下一行 ((0, 1) 到 (W - 1 , 1))。检查完整个列块后,它会继续下一个(W 到 2 * W - 1)。
要使算法起作用,您必须确保光线始终指向正方向。因此,如果光线指向负 x 方向,则垂直翻转遮罩和光线,如果光线指向负 y 方向而不是水平翻转光线。
使用pygame.transform.flip()顶部创建 4 个蒙版。不翻转、水平翻转、垂直翻转和垂直和水平翻转:
mask = pg.mask.from_surface(mask_surface)
mask_fx = pg.mask.from_surface(pg.transform.flip(mask_surface, True, False))
mask_fy = pg.mask.from_surface(pg.transform.flip(mask_surface, False, True))
mask_fx_fy = pg.mask.from_surface(pg.transform.flip(mask_surface, True, True))
flipped_masks = [[mask, mask_fy], [mask_fx, mask_fx_fy]]
Run Code Online (Sandbox Code Playgroud)
确定射线的方向:
c = math.cos(math.radians(angle))
s = math.sin(math.radians(angle))
Run Code Online (Sandbox Code Playgroud)
获取依赖于光线方向的翻转蒙版:
flip_x = c < 0
flip_y = s < 0
filpped_mask = flipped_masks[flip_x][flip_y]
Run Code Online (Sandbox Code Playgroud)
计算翻转的目标点:
x_dest = 250 + 500 * abs(c)
y_dest = 250 + 500 * abs(s)
Run Code Online (Sandbox Code Playgroud)
计算翻转的偏移量:
offset_x = 250 - pos[0] if flip_x else pos[0] - 250
offset_y = 250 - pos[1] if flip_y else pos[1] - 250
Run Code Online (Sandbox Code Playgroud)
获取最近翻转的光线和遮罩的交点并取消翻转交点:
hit = filpped_mask.overlap(beam_mask, (offset_x, offset_y))
if hit is not None and (hit[0] != pos[0] or hit[1] != pos[1]):
hx = 500 - hit[0] if flip_x else hit[0]
hy = 500 - hit[1] if flip_y else hit[1]
hit_pos = (hx, hy)
pg.draw.line(surface, BLUE, mouse_pos, hit_pos)
pg.draw.circle(surface, GREEN, hit_pos, 3)
Run Code Online (Sandbox Code Playgroud)
看例子:
repl.it/@Rabbid76/PyGame-PyGame-SurfaceLineMaskIntersect-2
import math
import sys
import pygame as pg
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pg.init()
beam_surface = pg.Surface((500, 500), pg.SRCALPHA)
def draw_beam(surface, angle, pos):
c = math.cos(math.radians(angle))
s = math.sin(math.radians(angle))
flip_x = c < 0
flip_y = s < 0
filpped_mask = flipped_masks[flip_x][flip_y]
# compute beam final point
x_dest = 250 + 500 * abs(c)
y_dest = 250 + 500 * abs(s)
beam_surface.fill((0, 0, 0, 0))
# draw a single beam to the beam surface based on computed final point
pg.draw.line(beam_surface, BLUE, (250, 250), (x_dest, y_dest))
beam_mask = pg.mask.from_surface(beam_surface)
# find overlap between "global mask" and current beam mask
offset_x = 250 - pos[0] if flip_x else pos[0] - 250
offset_y = 250 - pos[1] if flip_y else pos[1] - 250
hit = filpped_mask.overlap(beam_mask, (offset_x, offset_y))
if hit is not None and (hit[0] != pos[0] or hit[1] != pos[1]):
hx = 499 - hit[0] if flip_x else hit[0]
hy = 499 - hit[1] if flip_y else hit[1]
hit_pos = (hx, hy)
pg.draw.line(surface, BLUE, pos, hit_pos)
pg.draw.circle(surface, GREEN, hit_pos, 3)
#pg.draw.circle(surface, (255, 255, 0), mouse_pos, 3)
surface = pg.display.set_mode((500, 500))
#mask_surface = pg.image.load("../assets/mask.png")
mask_surface = pg.Surface((500, 500), pg.SRCALPHA)
mask_surface.fill((255, 0, 0))
pg.draw.circle(mask_surface, (0, 0, 0, 0), (250, 250), 100)
pg.draw.rect(mask_surface, (0, 0, 0, 0), (170, 170, 160, 160))
mask = pg.mask.from_surface(mask_surface)
mask_fx = pg.mask.from_surface(pg.transform.flip(mask_surface, True, False))
mask_fy = pg.mask.from_surface(pg.transform.flip(mask_surface, False, True))
mask_fx_fy = pg.mask.from_surface(pg.transform.flip(mask_surface, True, True))
flipped_masks = [[mask, mask_fy], [mask_fx, mask_fx_fy]]
clock = pg.time.Clock()
while True:
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
sys.exit()
mouse_pos = pg.mouse.get_pos()
surface.fill((0, 0, 0))
surface.blit(mask_surface, mask_surface.get_rect())
for angle in range(0, 359, 30):
draw_beam(surface, angle, mouse_pos)
pg.display.update()
clock.tick(30)
Run Code Online (Sandbox Code Playgroud)
不是,算法可以进一步改进。光线总是绘制在 的右下象限上beam_surface。因此不再需要其他 3 个象限,大小beam_surface可以减少到 250x250。射线的起点在 (0, 0) 而不是 (250, 250) 并且偏移的计算必须稍微调整:
beam_surface = pg.Surface((250, 250), pg.SRCALPHA)
def draw_beam(surface, angle, pos):
c = math.cos(math.radians(angle))
s = math.sin(math.radians(angle))
flip_x = c < 0
flip_y = s < 0
filpped_mask = flipped_masks[flip_x][flip_y]
# compute beam final point
x_dest = 500 * abs(c)
y_dest = 500 * abs(s)
beam_surface.fill((0, 0, 0, 0))
# draw a single beam to the beam surface based on computed final point
pg.draw.line(beam_surface, BLUE, (0, 0), (x_dest, y_dest))
beam_mask = pg.mask.from_surface(beam_surface)
# find overlap between "global mask" and current beam mask
offset_x = 499-pos[0] if flip_x else pos[0]
offset_y = 499-pos[1] if flip_y else pos[1]
hit = filpped_mask.overlap(beam_mask, (offset_x, offset_y))
if hit is not None and (hit[0] != pos[0] or hit[1] != pos[1]):
hx = 499 - hit[0] if flip_x else hit[0]
hy = 499 - hit[1] if flip_y else hit[1]
hit_pos = (hx, hy)
pg.draw.line(surface, BLUE, pos, hit_pos)
pg.draw.circle(surface, GREEN, hit_pos, 3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
267 次 |
| 最近记录: |