二维平面内的碰撞检测【二】

这篇我们将讲解圆与矩形的碰撞;

圆与矩形的碰撞检测

通常在svg或者canvas中我们会这样表示一个圆: 圆心(cx,cy),半径r;

表示矩形:中心点坐标(0,0) width="250" height="250" x=-width/2 y=-height/2 平移(rectX,rectY)并以几何中心旋转任意角度

展示的矩形即几何中心点(rectX,rectY)width="250" height="250";

你可能会问,如果矩形被旋转了怎么办?
我们可以在矩形的中心点建立一个新的坐标系统。以宽平行方向为x轴方向,以高平行方向为Y轴方向

clipboard.png

得到在新的坐标系统中圆的坐标:(cx-rectX,cy-rectY)

在新坐标系统中计算圆中心点的投影即圆中心点的坐标。那么我们发现,

clipboard.png

图中L=r/Math.sqrt(r);

我们可以看到当两边投影 x 方向 小于等于width+L && y方向小于等于heigth/2 或者 x方向小于等于width/2 && y方向上小于等于 height+L的时候即碰撞

//(cx,cy) 矩形 width height 中心点(rectX,rectY)
var L=r/Math.sqrt(r);
if((Math.abs(cx-rectX)<= width/2+L && Math.abs(cy-rectY)<= height/2)||(Math.abs(cx-rectX)<= width/2 && Math.abs(cy-rectY)<= height/2+L)){
//碰撞
}else{
//无碰撞
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 二维平面运动引擎主要用于实现游戏中物体在平面上的运动、碰撞检测、物理效果等功能。以下是一个简单的二维平面运动引擎示例: ```csharp public class Vector2D { public float X { get; set; } public float Y { get; set; } public Vector2D(float x, float y) { X = x; Y = y; } public static Vector2D operator +(Vector2D a, Vector2D b) { return new Vector2D(a.X + b.X, a.Y + b.Y); } public static Vector2D operator -(Vector2D a, Vector2D b) { return new Vector2D(a.X - b.X, a.Y - b.Y); } public static Vector2D operator *(Vector2D a, float scalar) { return new Vector2D(a.X * scalar, a.Y * scalar); } public static Vector2D operator /(Vector2D a, float scalar) { return new Vector2D(a.X / scalar, a.Y / scalar); } public float Length() { return (float)Math.Sqrt(X * X + Y * Y); } public Vector2D Normalize() { float length = Length(); return new Vector2D(X / length, Y / length); } public float Dot(Vector2D other) { return X * other.X + Y * other.Y; } public float Cross(Vector2D other) { return X * other.Y - Y * other.X; } } public class GameObject { public Vector2D Position { get; set; } public Vector2D Velocity { get; set; } public float Mass { get; set; } public float Radius { get; set; } public void Update(float deltaTime) { Position += Velocity * deltaTime; } public bool IsColliding(GameObject other) { float distance = (Position - other.Position).Length(); return distance < Radius + other.Radius; } public void ResolveCollision(GameObject other) { Vector2D normal = (other.Position - Position).Normalize(); Vector2D relativeVelocity = other.Velocity - Velocity; float impulse = -2 * relativeVelocity.Dot(normal) / (Mass + other.Mass); Velocity -= impulse * other.Mass * normal; other.Velocity += impulse * Mass * normal; } } public class PhysicsEngine { private List<GameObject> objects = new List<GameObject>(); public void AddObject(GameObject obj) { objects.Add(obj); } public void RemoveObject(GameObject obj) { objects.Remove(obj); } public void Update(float deltaTime) { for (int i = 0; i < objects.Count; i++) { objects[i].Update(deltaTime); } for (int i = 0; i < objects.Count; i++) { for (int j = i + 1; j < objects.Count; j++) { if (objects[i].IsColliding(objects[j])) { objects[i].ResolveCollision(objects[j]); } } } } } ``` 这个引擎包含了三个类:`Vector2D`、`GameObject` 和 `PhysicsEngine`。`Vector2D` 类表示平面上的二维向量,包含了向量加、减、乘、除、求长度、归一化、点积、叉积等基本操作。`GameObject` 类表示游戏中的物体,包含了位置、速度、质量、半径等属性,以及更新、碰撞检测、碰撞响应等方法。`PhysicsEngine` 类表示物理引擎,包含了游戏中所有物体的列表,以及更新物体位置、检测碰撞、响应碰撞等方法。 使用时,可以先创建一个物理引擎实例,然后向其中添加物体,每帧调用引擎的 `Update` 方法更新物体位置、检测碰撞、响应碰撞等操作。例如: ```csharp PhysicsEngine engine = new PhysicsEngine(); GameObject ball1 = new GameObject(); ball1.Position = new Vector2D(100, 100); ball1.Velocity = new Vector2D(50, 0); ball1.Mass = 1; ball1.Radius = 10; GameObject ball2 = new GameObject(); ball2.Position = new Vector2D(200, 100); ball2.Velocity = new Vector2D(-50, 0); ball2.Mass = 1; ball2.Radius = 10; engine.AddObject(ball1); engine.AddObject(ball2); while (true) { engine.Update(0.01f); // 渲染物体... } ``` 这个示例创建了两个球体,分别从左右两侧向中间运动,当两个球体碰撞时会发生弹性碰撞并反弹。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值