From 53ded30e0b05608f74cac033be905cb9f08e21a2 Mon Sep 17 00:00:00 2001 From: yhh <359807859@qq.com> Date: Thu, 11 Jun 2020 10:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9C=86=E5=92=8C=E7=9F=A9?= =?UTF-8?q?=E5=BD=A2=E7=A2=B0=E6=92=9E=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- source/src/Physics/Collision.ts | 40 +++++---------------------------- 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 98211ee7..4f1642da 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ - [x] 数学库 - [x] 简易矩阵类 - [x] 简易2d 向量类 +- [x] BreadthFirst 寻路算法 +- [x] Dijkstra 寻路算法 ## 计划列表 @@ -31,8 +33,6 @@ - [ ] 被动系统 - [ ] 协调系统 - [ ] 高性能物理引擎 -- [ ] BreadthFirst 寻路算法 -- [ ] Dijkstra 寻路算法 - [ ] FSM 简易状态机 - [ ] 数学库 - [ ] 贝塞尔曲线 diff --git a/source/src/Physics/Collision.ts b/source/src/Physics/Collision.ts index 8532772b..4b153203 100644 --- a/source/src/Physics/Collision.ts +++ b/source/src/Physics/Collision.ts @@ -79,42 +79,12 @@ class Collisions { } public static isRectToCircle(rect: Rectangle, cPosition: Vector2, cRadius: number): boolean { - if (this.isRectToPoint(rect.x, rect.y, rect.width, rect.height, cPosition)) - return true; + let ew = rect.width * 0.5; + 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; - 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; + return vx * vx + vy * vy < cRadius * cRadius; } public static isRectToLine(rect: Rectangle, lineFrom: Vector2, lineTo: Vector2){