我有一个碰撞检测类,它通过查找中心之间的距离以及该距离是否小到足以发生碰撞来工作(参见 Collision Detection error )。我的问题是试图让它真正起作用,椭圆碰撞。如有必要,我会解释更多。 谢谢
请您参考如下方法:
最好的方法是在图像重叠时实现逐像素碰撞检测,您可以在以下链接中阅读更多相关信息
http://www.codeproject.com/KB/game/collision3.aspx
Per-pixel collision problem in C#
几年前我也为一个项目做过这样的问题,当时我需要检测两个圆圈是否重叠,我使用了以下代码
public static bool Intersect(Rectangle rectangle1, Rectangle rectangle2)
{
if (((rectangle1.X < (rectangle2.X + rectangle2.Width)) && (rectangle2.X < (rectangle1.X + rectangle1.Width))) && (rectangle1.Y < (rectangle2.Y + rectangle2.Height)) && (rectangle2.Y < (rectangle1.Y + rectangle1.Height)))
{
Vector2 rect1Centre = new Vector2(rectangle1.X + rectangle1.Width / 2, rectangle1.Y + rectangle1.Height / 2);
Vector2 rect2Centre = new Vector2(rectangle2.X + rectangle2.Width / 2, rectangle2.Y + rectangle1.Height / 2);
double radius1 = ((rectangle1.Width / 2) + (rectangle1.Height / 2)) / 2;
double radius2 = ((rectangle2.Width / 2) + (rectangle2.Height / 2)) / 2;
double widthTri = rect1Centre.X - rect2Centre.X;
double heightTri = rect1Centre.Y - rect2Centre.Y;
double distance = Math.Sqrt(Math.Pow(widthTri, 2) + Math.Pow(heightTri, 2));
if (distance <= (radius1 + radius2))
return true;
}
return false;
}
不是很好的代码,但我写的是我的第一个 XNA 游戏
