优化圆和矩形碰撞检测

This commit is contained in:
yhh
2020-06-11 10:06:29 +08:00
parent 2eec9a82f9
commit 53ded30e0b
2 changed files with 7 additions and 37 deletions

View File

@@ -11,6 +11,8 @@
- [x] 数学库 - [x] 数学库
- [x] 简易矩阵类 - [x] 简易矩阵类
- [x] 简易2d 向量类 - [x] 简易2d 向量类
- [x] BreadthFirst 寻路算法
- [x] Dijkstra 寻路算法
## 计划列表 ## 计划列表
@@ -31,8 +33,6 @@
- [ ] 被动系统 - [ ] 被动系统
- [ ] 协调系统 - [ ] 协调系统
- [ ] 高性能物理引擎 - [ ] 高性能物理引擎
- [ ] BreadthFirst 寻路算法
- [ ] Dijkstra 寻路算法
- [ ] FSM 简易状态机 - [ ] FSM 简易状态机
- [ ] 数学库 - [ ] 数学库
- [ ] 贝塞尔曲线 - [ ] 贝塞尔曲线

View File

@@ -79,42 +79,12 @@ class Collisions {
} }
public static isRectToCircle(rect: Rectangle, cPosition: Vector2, cRadius: number): boolean { public static isRectToCircle(rect: Rectangle, cPosition: Vector2, cRadius: number): boolean {
if (this.isRectToPoint(rect.x, rect.y, rect.width, rect.height, cPosition)) let ew = rect.width * 0.5;
return true; let eh = rect.height * 0.5;
let vx = Math.max(0, Math.max(cPosition.x - rect.x) - ew);
let vy = Math.max(0, Math.max(cPosition.y - rect.y) - eh);
let edgeFrom: Vector2; return vx * vx + vy * vy < cRadius * cRadius;
let edgeTo: Vector2;
let sector = this.getSector(rect.x, rect.y, rect.width, rect.height, cPosition);
if ((sector & PointSectors.top) != 0) {
edgeFrom = new Vector2(rect.x, rect.y);
edgeTo = new Vector2(rect.x + rect.width, rect.y);
if (this.isCircleToLine(cPosition, cRadius, edgeFrom, edgeTo))
return true;
}
if ((sector & PointSectors.bottom) != 0) {
edgeFrom = new Vector2(rect.x, rect.y + rect.height);
edgeTo = new Vector2(rect.x + rect.width, rect.y + rect.height);
if (this.isCircleToLine(cPosition, cRadius, edgeFrom, edgeTo))
return true;
}
if ((sector & PointSectors.left) != 0) {
edgeFrom = new Vector2(rect.x, rect.y);
edgeTo = new Vector2(rect.x, rect.y + rect.height);
if (this.isCircleToLine(cPosition, cRadius, edgeFrom, edgeTo))
return true;
}
if ((sector & PointSectors.right) != 0) {
edgeFrom = new Vector2(rect.x + rect.width, rect.y);
edgeTo = new Vector2(rect.x + rect.width, rect.y + rect.height);
if (this.isCircleToLine(cPosition, cRadius, edgeFrom, edgeTo))
return true;
}
return false;
} }
public static isRectToLine(rect: Rectangle, lineFrom: Vector2, lineTo: Vector2){ public static isRectToLine(rect: Rectangle, lineFrom: Vector2, lineTo: Vector2){