我正在尝试在Gosu中用ruby编码Hitbox,并想检查2个范围是否满足(范围是坐标),我希望它简单地给出true或false
我调查了一下,找到了range.cover?代码,但是经过测试,这表明它仅检查一个范围是否完全适合另一个范围,而不检查它们是否仅部分连接。
#both sprites are arrays, with the following structure
#[image_data, sprite_x, sprite_y]
#image_data.width would return how wide the image is
#The x and y is the top left of the sprite
def hit_scan(sprite1, sprite2)
x_connect = (sprite1[1]..sprite1[1] + sprite1[0].width).cover?(sprite2[1]..(sprite2[1] + sprite2[0].width))
y_connect = (sprite1[2]..sprite1[2] + sprite1[0].height).cover?(sprite2[2]..(sprite2[2] + sprite2[0].height)
if x_connect == true
if y_connect == true
return true
else
return false
end
else
return false
end
end
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的方法,只有当整个精灵都在另一个精灵中时,它才返回true。
我希望每当精灵接触时,它都会返回一个true语句,但是只有当一个精灵位于另一个中时,它才会返回true。