Use Bhijn's algorithm for corner assist
This commit is contained in:
@ -144,6 +144,57 @@ export class Size {
|
||||
}
|
||||
}
|
||||
|
||||
export class Circle {
|
||||
readonly center: Point;
|
||||
readonly radius: number;
|
||||
|
||||
constructor(center: Point, radius: number) {
|
||||
this.center = center;
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
getContactWithRect(rect: Rect): Point | null {
|
||||
// port: https://www.jeffreythompson.org/collision-detection/circle-rect.php
|
||||
let cx = this.center.x;
|
||||
let cy = this.center.y;
|
||||
let testX = this.center.x;
|
||||
let testY = this.center.y;
|
||||
|
||||
let rx = rect.top.x;
|
||||
let ry = rect.top.y;
|
||||
let rw = rect.size.w;
|
||||
let rh = rect.size.h;
|
||||
|
||||
if (cx < rx) { testX = rx; }
|
||||
else if (cx > rx + rw) { testX = rx + rw; }
|
||||
if (cy < ry) { testY = ry; }
|
||||
else if (cy > ry + rh) { testY = ry + rh; }
|
||||
|
||||
let distX = cx - testX;
|
||||
let distY = cy - testY;
|
||||
let sqDistance = (distX * distX) + (distY * distY);
|
||||
|
||||
if (sqDistance <= this.radius * this.radius) {
|
||||
return new Point(testX, testY);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
overlappedCells(size: Size): Rect[] {
|
||||
let meAsRect = new Rect(
|
||||
this.center.offset(new Point(-this.radius, -this.radius)),
|
||||
new Size(this.radius * 2, this.radius * 2)
|
||||
);
|
||||
let all: Rect[] = [];
|
||||
for (let cell of meAsRect.overlappedCells(size).values()) {
|
||||
if (this.getContactWithRect(cell) != null) {
|
||||
all.push(cell);
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
||||
}
|
||||
|
||||
export class Rect {
|
||||
readonly top: Point;
|
||||
readonly size: Size;
|
||||
|
Reference in New Issue
Block a user