所以我有一个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; }
}
我有一个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;
}
}
我有两个继承自这个类的类。 CircleEntity
获得一个圆圈,RectangleEntity
获得一个矩形,但没有其他任何变化。
为了检查冲突,我有一个辅助函数,它接收两个PhysicsEntity
个对象,检查它们的类型(RectangleEntity
或CircleEntity
),然后调用该函数特定的碰撞类型。如果它们发生碰撞,碰撞检测功能只返回boolean
。
然后我有一个名为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;
}
Circle-Circle碰撞和Rectangle-Rectangle碰撞工作正常,但Circle-Rectangle绝对不行。我甚至无法正确检测到碰撞,它总是返回false。这是Rectangle-Circle碰撞检测:
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));
}
此刻我完全失败了。我不知道出了什么问题。我已经研究了几乎所有在阳光下进行碰撞检测的教程,我只是不知道。
我在这里做错了什么?
编辑:我错了,Rectangle-Rectangle也无效。如果有人能指出我对二维碰撞检测白痴指南的方向,我会非常感激。
答案 0 :(得分:3)
下图显示了我们可以找到圆形和矩形的所有情况。
深蓝色是要测试的矩形。由其中心及其宽度和高度定义
圆
我们可以通过考虑对称性来简化解决方案。如果我们将圆与中心的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;
}