【发布时间】:2013-12-13 04:22:38
【问题描述】:
我尝试了很多事情但没有找到好的解决方案,所以我在这里。
在我的游戏(2D)中,我必须检查与Rotated Rectangle 内部图像的所有对象(房屋、车库..)的碰撞,在从Point A 到Point B 的射线之间。
我正在使用 Xna 并且有一些代码:
public void Update(List<Obstacle> Lob, DragObj Ldo)
{
bool inter = false;
Point A;
Point B;
A = new Point((int)pos.X, (int)pos.Y);
B = new Point((int)Ldo.Position.X, (int)Ldo.Position.Y);
for (int j = 0; j < Lob.Count(); j++)
{
if (inter = interclass.LineIntersectsRect(A, B, Lob[j].Shape)) // I have this for the moment, Shape is the rectangle but not rotated )
{
inter = true;
islight = false;
}
else
{
inter = false;
}
}
}
所以为了解决我的问题,我是否找到了一个解决方案,让 rotatedRectangle 对象具有检查与线碰撞的方法。是否完全不同,也许只检查 yy 直线和每个旋转的矩形轴之间的碰撞?
感谢您的建议。
【问题讨论】:
-
尝试旋转线,这只是一个小触发。
-
旋转线会改变我的二维空间组织,不是吗?我将尝试旋转我的 2 个点,但在我当前的代码中实现起来并不容易。
-
但是你可以想象的对吗?如果将两者放在一起并旋转使矩形是直的,则可以使用常规的轴对齐矩形功能
-
Y 我已经画了它来想象它,它还可以。但它有一个奇怪的行为。交叉点工作但不在正确的位置,我也尝试过弧度角。我用它来旋转我的观点:在 2D 中,您从 (x,y) 旋转角度 t 来制作 (X,Y):X = x cos t - y sin t Y = x sin t + y cos t
标签: c# algorithm xna collision-detection