- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
最好不要使用任何类型的循环,因为这将在游戏中使用。
我希望将一条线与任意大小的矩形相交。但我也希望返回交点[s]。
这是可能的,我已经做了一些谷歌搜索,但仍然没有解决。
直线是使用 (x1,y1,x2,y2) 定义的。矩形也有这两点。
最佳答案
我建议简单地对构成矩形的每条线段(边)进行线段线段相交检查。这是我很久以前写的线段相交检测算法,是从我的一个旧 XNA 项目中挖掘出来的:
// a1 is line1 start, a2 is line1 end, b1 is line2 start, b2 is line2 end
static bool Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, out Vector2 intersection)
{
intersection = Vector2.Zero;
Vector2 b = a2 - a1;
Vector2 d = b2 - b1;
float bDotDPerp = b.X * d.Y - b.Y * d.X;
// if b dot d == 0, it means the lines are parallel so have infinite intersection points
if (bDotDPerp == 0)
return false;
Vector2 c = b1 - a1;
float t = (c.X * d.Y - c.Y * d.X) / bDotDPerp;
if (t < 0 || t > 1)
return false;
float u = (c.X * b.Y - c.Y * b.X) / bDotDPerp;
if (u < 0 || u > 1)
return false;
intersection = a1 + t * b;
return true;
}
我将把每条边输入上述方法并收集结果作为练习留给读者:)
看看 Cohen–Sutherland algorithm当你有一大组线,其中大多数线不与矩形相交时,可以有效地做到这一点。它使用 9 段网格,您将线的每个端点放在所述网格的区域中:
使用它我们可以判断是否不会有任何线交叉点:
例如,CD
不会与矩形(在第一张图中以红色显示)相交,因为 C
和 D
都在顶行,AB
也不会。对于那些线可能与矩形相交的地方,我们必须尝试线与线相交。
它们对部分进行编号/标记的方式允许我们简单地执行 x AND y != 0
(其中 x
和 y
是每个线端点的部分标签)以确定是否不会有交叉点。
使用这种方法意味着我们必须减少很多很多的线对线交叉点,这会大大加快整个过程。
关于c# - 线与 AABB 矩形相交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746274/
哪个交叉路口测试通常更快?一个与另一个轴对齐的边界框?或者带有三角形的轴对齐边界框。 我假设 AABB-AABB 但我听到了不同的意见。 最佳答案 可以在任何标准 CPU 上使用(最多)六个比较和六个
我有许多对象需要渲染到 HTML5 Canvas 上。我的输入是轴对齐边界框的有序列表。这些盒子经常重叠,但也经常在它们之间留下大面积的空白空间。 我想尽量减少我必须创建的 Canvas 表面区域的数
我有一组动态的 AABB 框,它们的位置和大小不断更新。我可以使用什么结构来获取其中哪些 AABB 框与任意 AABB 框发生碰撞? 最佳答案 使用动态 AABB 树。 Here's some sou
我正在寻找一种简单的算法来检测 aabb 的区域是否与圆弧(由绳索闭合)或饼图(通过圆心闭合)的区域重叠。 我已经找到了这个答案:Intersection of rectangle and circl
我有一个在对象空间中由中心点和半径表示的球体。球体通过变换矩阵变换到世界空间,变换矩阵可能包括缩放、旋转和平移。我需要为世界空间中的球体构建一个轴对齐的边界框,但我不知道该怎么做。 这是我目前的方法,
我希望计算应用了变换矩阵(旋转、缩放、平移等)的 2D 椭圆的轴对齐边界框 (AABB) 类似于此解决方案的内容:Calculating an AABB for a transformed spher
您好,我是 C++ SFML 的新手。我应该绘制一些矩形并在旋转时渲染它们的 AABB,我想检测为它们设置的尺寸是否与另一个旋转的 AABB 矩形相交。这是我用来检测它们的方法。如果它们在旋转,以这种
我需要一个由轴对齐边界框 (AABB) 填充的世界组成的引擎。将执行连续循环,执行以下操作: for box_a in world box_a = do_something(box_a)
我有一个问题,我需要将一个 AABB 分成许多小的 AABB。我需要在每个较小的 AABB 中找到最小值和最大值。 如果我们以这个长方体为例,我们可以看到它被分成了64个更小的长方体。我需要计算所有这
当我的 AABB 物理引擎解析交叉点时,它会找到穿透较小的轴,然后“推出”该轴上的实体。 考虑“向左跳跃”的例子: 如果 velocityX 大于 velocityY,AABB 会将实体推出 Y 轴,
因此,我目前正在通过尝试为我的游戏引擎制作一个简单的物理引擎来重新发明轮子(并学到很多东西)。我一直在网上搜索,试图(但未能)解决我当前的问题。关于这个主题有很多资源,但我发现的资源似乎都不适用于我的
我正在制作一个游戏,其中玩家是一个直立的圆柱体,世界是轴对齐的边界框。鉴于此,我如何检查圆柱体是否与盒子相交? 谢谢 最佳答案 这主要是一个二维问题。 对于每个 AABB,测试圆柱体的垂直尺寸是否与
旋转物体时,围绕腿的 AABB 不会随腿移动。相反,它们相对于物体停留在同一位置,只是简单地增加尺寸以将腿保持在 AABB 中,而不是随腿移动。这是由于这些 AABB 中的每一个都具有相同的位置,即主
更新 再次更改了碰撞代码并为AABB制作了一个组件,现在看来问题仅在于水平碰撞,它没有将物体推到它认为足够的位置但是与Y轴的代码相同所以它不应该成为一个问题。 (它确实检测到水平碰撞分辨率是问题) v
我正在学习PhysicsJS ,我尝试使用 union像这样: // Window bounds var rect1 = Physics.aabb(0, 100, 300, 200); var rec
我正在编写用于在 3D 空间中进行鼠标拾取的代码。到目前为止,我已经开设了 Ray 和 AABB 类(class)。我需要的是 Ray-AABB 相交的函数。我知道如何编写并使其工作,我的问题是哪个类
我在这里看到过这个话题,但它并没有真正帮助我解决问题:(使用 glLoadIdentity() 因为我正在使用 libqglviewer 并且由于一些奇怪的原因如果我使用 glLoadIdentity
我目前正在努力编写一个基于空间分割的系统(用于游戏),我需要能够测试一个圆是否完全包含一个正方形。 为了加分,我应该指出我的系统在 N 维中工作,所以如果你的算法通过循环遍历每个维度并做某事来工作,请
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我认为扫过意味着确定物体是否会在某个时刻发生碰撞,而不仅仅是它们当前是否正在碰撞,但如果我错了请告诉我。 我有带边界框的对象,它们在轴上对齐。物体的盒子可以有不同的大小,但它们总是矩形的。 我已经尝试
我是一名优秀的程序员,十分优秀!