我有错误的算法来测试重叠矩形(?)
I have the wrong algorithm for testing overlapping rects (?)
给出一个矩形 s1 = {x, y, w, h}
和另一个矩形 s2 = {x, y, w, h}
和一个向量 v = {x, y}
。并假设 s1
已根据 v
移动,我想检查它是否重叠。
我有这个算法:
isOverlapping = not (s1.x + s1.w + v.x < s2.x
or s1.x + v.x > s2.x + s2.w
or s1.y + s1.h + v.y < s2.y
or s1.h + v.y > s2.y + s2.h)
但是好像不能正常工作,但我什至不能说它有什么问题,因为我完全不明白。由于它的性质,我什至无法将它分解成更小的部分。
isOverlapping = not (right_edge_of_s1_plus_move < left_edge_of_s2
or left_edge_of_s1_plus_move > right_edge_of_s2
or top_edge_of_s1_plus_move < bottom_edge_of_s2
or bottom_edge_of_s1_plus_move > top_edge_of_s2)
isOverlapping = not (s1_overlaps_s2_on_left_edge
or s1_overlaps_s2_on_right_edge
or s1_overlaps_s2_on_bottom_edge
or s1_overlaps_s2_on_top_edge)
这意味着一旦 s1 在一条边上与 s2 重叠,它们就不会重叠...什么?
奇怪的是,在我的程序中,只有当 s1 试图移动到 s2 下方时,它才不起作用。其他一切正常。
所以我的问题是:测试两个移动的盒子是否相互重叠 (AABB) 的正确算法是什么?我这里有什么样的算法?我知道我从某个地方得到它,但我找不到我的来源了。我刚刚添加了额外的运动向量。
错误是由一个简单的拼写错误引起的。最后比较中的第一个变量应该是 s1.y
而不是 s1.h
:
isOverlapping =
not (s1.x + s1.w + v.x < s2.x
or s1.x + v.x > s2.x + s2.w
or s1.y + s1.h + v.y < s2.y
or s1.y + v.y > s2.y + s2.h)
给出一个矩形 s1 = {x, y, w, h}
和另一个矩形 s2 = {x, y, w, h}
和一个向量 v = {x, y}
。并假设 s1
已根据 v
移动,我想检查它是否重叠。
我有这个算法:
isOverlapping = not (s1.x + s1.w + v.x < s2.x
or s1.x + v.x > s2.x + s2.w
or s1.y + s1.h + v.y < s2.y
or s1.h + v.y > s2.y + s2.h)
但是好像不能正常工作,但我什至不能说它有什么问题,因为我完全不明白。由于它的性质,我什至无法将它分解成更小的部分。
isOverlapping = not (right_edge_of_s1_plus_move < left_edge_of_s2
or left_edge_of_s1_plus_move > right_edge_of_s2
or top_edge_of_s1_plus_move < bottom_edge_of_s2
or bottom_edge_of_s1_plus_move > top_edge_of_s2)
isOverlapping = not (s1_overlaps_s2_on_left_edge
or s1_overlaps_s2_on_right_edge
or s1_overlaps_s2_on_bottom_edge
or s1_overlaps_s2_on_top_edge)
这意味着一旦 s1 在一条边上与 s2 重叠,它们就不会重叠...什么?
奇怪的是,在我的程序中,只有当 s1 试图移动到 s2 下方时,它才不起作用。其他一切正常。
所以我的问题是:测试两个移动的盒子是否相互重叠 (AABB) 的正确算法是什么?我这里有什么样的算法?我知道我从某个地方得到它,但我找不到我的来源了。我刚刚添加了额外的运动向量。
错误是由一个简单的拼写错误引起的。最后比较中的第一个变量应该是 s1.y
而不是 s1.h
:
isOverlapping =
not (s1.x + s1.w + v.x < s2.x
or s1.x + v.x > s2.x + s2.w
or s1.y + s1.h + v.y < s2.y
or s1.y + v.y > s2.y + s2.h)