XNA/Monogame 检测圆形和矩形之间的碰撞不起作用

zud*_*dsy 2 c# xna collision-detection game-physics monogame

所以我有一个Circle结构体,非常简单,如下所示:

public struct Circle
{
    public Circle(int x, int y, int radius) : this()
    {
        Center = new Point(x, y);
        Radius = radius;
    }

    public Point Center { get; private set; }
    public int Radius { get; private set; }

}
Run Code Online (Sandbox Code Playgroud)

我有一个PhysicsEnity看起来像这样的课程:

public class PhysicsEntity
{

    public int Width { get; protected set; }
    public int Height { get; protected set; }
    public Vector2 Position { get;  set; }
    public Vector2 Velocity { get; set; }
    public float Restitution { get; protected set; }
    public float Mass { get; protected set; }

    public virtual void Update(GameTime gameTime)
    {
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
        Velocity += ((Phys.Gravity * dt) * Mass);
        Position += Velocity * dt;
    }

    public virtual void Draw(SpriteBatch spriteBatch) { }

    public virtual void ApplyImpulse(Vector2 impulse)
    {
        Position += impulse;
    }

}
Run Code Online (Sandbox Code Playgroud)

我有两个继承自这个类的类。CircleEntity得到一个圆,RectangleEntity得到一个矩形,但没有其他变化。

为了检查碰撞,我有一个辅助函数,它接受两个PhysicsEntity对象,检查它们是什么类型(RectangleEntityCircleEntity),并为该特定碰撞类型调用该函数。碰撞检测函数只是boolean在它们发生碰撞时返回 a 。

然后我有一个名为 的函数ResolveCollision,它接受两个实体,如下所示:

public static void ResolveCollision(PhysicsEntity a, PhysicsEntity b)
{
    if (a.Mass + b.Mass == 0)
    {
        a.Velocity = Vector2.Zero;
        b.Velocity = Vector2.Zero;
        return;
    }
    var invMassA = a.Mass > 0 ? 1 / a.Mass : 0;
    var invMassB = b.Mass > 0 ? 1 / b.Mass : 0;
    Vector2 rv = b.Velocity - a.Velocity;
    Vector2 normal = Vector2.Normalize(b.Position - a.Position);
    float velAlongNormal = Vector2.Dot(rv, normal);
    if (velAlongNormal > 0) return;
    float e = MathHelper.Min(a.Restitution, b.Restitution);
    float j = (-(1 + e) * velAlongNormal) / (invMassA + invMassB);
    Vector2 impulse = j * normal;
    a.Velocity -= invMassA * impulse;
    b.Velocity += invMassB * impulse;
}
Run Code Online (Sandbox Code Playgroud)

Circle-Circle 碰撞和 Rectangle-Rectangle 碰撞工作得很好,但 Circle-Rectangle 绝对不行。我什至无法让它正确检测碰撞,它总是返回 false。这是矩形-圆碰撞检测:

public static bool RectangleCircleCollision(CircleEntity a, RectangleEntity b)
{
    Circle c = a.Circle;
    Rectangle r = b.Rectangle;
    Vector2 v = new Vector2(MathHelper.Clamp(c.Center.X, r.Left, r.Right),
                            MathHelper.Clamp(c.Center.Y, r.Top, r.Bottom));
    Vector2 direction = c.Center.ToVector2() - v;
    float distSquare = direction.LengthSquared();
    return ((distSquare > 0) && (distSquare < c.Radius * c.Radius));
}
Run Code Online (Sandbox Code Playgroud)

在这一点上,我完全不知所措。我不知道出了什么问题。我已经研究了几乎所有关于碰撞检测的教程,我只是不知道。

我在这里做错了什么?

编辑:我错了,矩形矩形也不起作用。如果有人能将我指向 2D 碰撞检测的白痴指南,我会非常感激。

Bli*_*n67 5

圆形和矩形重叠。

下图显示了我们可以在其中找到圆形和矩形的所有情况。

在此处输入图片说明

深蓝色是要测试的矩形。由其中心及其宽度和高度定义

  • A如果圆中心小于矩形中心(深蓝色)的宽度和高度的一半,我们知道它正在接触。
  • B圆心的 x 或 y 位置小于矩形中心宽度或高度的一半,另一个距离小于宽度或高度加上圆半径的一半。圆心在矩形之外但仍然接触
  • C圆靠近一个角落,它的中心在左上方,但与角落的距离小于半径,因此它是接触的。
  • D圆在矩形顶部和右侧边缘的半径距离内,但比角的半径距离更远。它不是触摸。
  • E不接触,因为圆心大于任何边缘的半径。

我们可以通过考虑对称性来简化解决方案。如果我们使圆到中心的 x 和 y 距离为正,那么我们只是在做一个角

private static bool DoRectangleCircleOverlap(Circle cir, Rectangle rect) {

    // Get the rectangle half width and height
    float rW = (rect.Width) / 2;
    float rH = (rect.Height) / 2;

    // Get the positive distance. This exploits the symmetry so that we now are
    // just solving for one corner of the rectangle (memory tell me it fabs for 
    // floats but I could be wrong and its abs)
    float distX = Math.Abs(cir.Center.X - (rect.Left + rW));
    float distY = Math.Abs(cir.Center.Y - (rect.Top + rH));

    if (distX >= cir.Radius + rW || distY >= cir.Radius + rH) {
        // Outside see diagram circle E
        return false;
    }
    if (distX < rW || distY < rH) {
        // Inside see diagram circles A and B
        return true; // touching
    }

    // Now only circles C and D left to test
    // get the distance to the corner
    distX -= rW;
    distY -= rH;

    // Find distance to corner and compare to circle radius 
    // (squared and the sqrt root is not needed
    if (distX * distX + distY * distY < cir.Radius * cir.Radius) {
        // Touching see diagram circle C
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)