新增shapecollision 用于计算多边形碰撞
This commit is contained in:
@@ -8,4 +8,16 @@ class Circle extends Shape {
|
||||
this.radius = radius;
|
||||
this._originalRadius = radius;
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
||||
return ShapeCollisions.pointToCicle(point, this);
|
||||
}
|
||||
|
||||
public collidesWithShape(other: Shape): CollisionResult{
|
||||
if (other instanceof Rect && (other as Rect).isUnrotated){
|
||||
return ShapeCollisions.circleToRect(this, other as Rect);
|
||||
}
|
||||
|
||||
throw new Error(`Collisions of Circle to ${other} are not supported`);
|
||||
}
|
||||
}
|
||||
5
source/src/Physics/Shapes/CollisionResult.ts
Normal file
5
source/src/Physics/Shapes/CollisionResult.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
class CollisionResult {
|
||||
public minimumTranslationVector: Vector2;
|
||||
public normal: Vector2;
|
||||
public point: Vector2;
|
||||
}
|
||||
@@ -1,25 +1,33 @@
|
||||
///<reference path="./Shape.ts" />
|
||||
class Polygon extends Shape {
|
||||
public points: Vector2[];
|
||||
public isUnrotated: boolean = true;
|
||||
private _polygonCenter: Vector2;
|
||||
private _areEdgeNormalsDirty = true;
|
||||
private _originalPoint: Vector2[]
|
||||
|
||||
constructor(vertCount: number, radius: number){
|
||||
constructor(vertCount: number, radius: number) {
|
||||
super();
|
||||
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
||||
}
|
||||
|
||||
public setPoints(points: Vector2[]){
|
||||
public setPoints(points: Vector2[]) {
|
||||
this.points = points;
|
||||
this.recalculateCenterAndEdgeNormals();
|
||||
|
||||
this._originalPoint = new Vector2[points.length];
|
||||
this._originalPoint = points;
|
||||
}
|
||||
|
||||
public recalculateCenterAndEdgeNormals(){
|
||||
|
||||
public recalculateCenterAndEdgeNormals() {
|
||||
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||
this._areEdgeNormalsDirty = true;
|
||||
}
|
||||
|
||||
public static findPolygonCenter(points: Vector2[]){
|
||||
public static findPolygonCenter(points: Vector2[]) {
|
||||
let x = 0, y = 0;
|
||||
|
||||
for (let i = 0; i < points.length; i++){
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
x += points[i].x;
|
||||
y += points[i].y;
|
||||
}
|
||||
@@ -27,10 +35,58 @@ class Polygon extends Shape {
|
||||
return new Vector2(x / points.length, y / points.length);
|
||||
}
|
||||
|
||||
public static buildSymmertricalPolygon(vertCount: number, radius: number){
|
||||
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } {
|
||||
let distanceSquared = Number.MAX_VALUE;
|
||||
let edgeNormal = new Vector2(0, 0);
|
||||
let closestPoint = new Vector2(0, 0);
|
||||
|
||||
let tempDistanceSquared;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
let j = i + 1;
|
||||
if (j == points.length)
|
||||
j = 0;
|
||||
|
||||
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
||||
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
||||
|
||||
if (tempDistanceSquared < distanceSquared) {
|
||||
distanceSquared = tempDistanceSquared;
|
||||
closestPoint = closest;
|
||||
|
||||
let line = Vector2.subtract(points[j], points[i]);
|
||||
edgeNormal.x = -line.y;
|
||||
edgeNormal.y = line.x;
|
||||
}
|
||||
}
|
||||
|
||||
edgeNormal = Vector2.normalize(edgeNormal);
|
||||
|
||||
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
|
||||
}
|
||||
|
||||
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
||||
return ShapeCollisions.pointToPoly(point, this);
|
||||
}
|
||||
|
||||
public containsPoint(point: Vector2) {
|
||||
point = Vector2.subtract(point, this.position);
|
||||
|
||||
let isInside = false;
|
||||
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
||||
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
|
||||
(point.x < (this.points[j].x - this.points[i].x) * (point.y - this.points[i].y) / (this.points[j].y - this.points[i].y) +
|
||||
this.points[i].x)) {
|
||||
isInside = !isInside;
|
||||
}
|
||||
}
|
||||
|
||||
return isInside;
|
||||
}
|
||||
|
||||
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
|
||||
let verts = new Vector2[vertCount];
|
||||
|
||||
for (let i = 0; i < vertCount; i ++){
|
||||
for (let i = 0; i < vertCount; i++) {
|
||||
let a = 2 * Math.PI * (i / vertCount);
|
||||
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
|
||||
}
|
||||
|
||||
8
source/src/Physics/Shapes/Rect.ts
Normal file
8
source/src/Physics/Shapes/Rect.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
class Rect extends Polygon {
|
||||
public containsPoint(point: Vector2){
|
||||
if (this.isUnrotated)
|
||||
return this.bounds.contains(point);
|
||||
|
||||
return super.containsPoint(point);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
abstract class Shape {
|
||||
public bounds: Rectangle;
|
||||
public position: Vector2;
|
||||
|
||||
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||
}
|
||||
60
source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts
Normal file
60
source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
class ShapeCollisions {
|
||||
public static circleToRect(circle: Circle, box: Rect): CollisionResult{
|
||||
let result = new CollisionResult();
|
||||
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||
|
||||
if (box.containsPoint(circle.position)){
|
||||
result.point = closestPointOnBounds;
|
||||
|
||||
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static pointToCicle(point: Vector2, circle: Circle){
|
||||
let result = new CollisionResult();
|
||||
|
||||
let distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||
let sumOfRadii = 1 + circle.radius;
|
||||
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||
if (collided){
|
||||
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
||||
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
||||
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
|
||||
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2){
|
||||
let v = Vector2.subtract(lineB, lineA);
|
||||
let w = Vector2.subtract(closestTo, lineA);
|
||||
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
|
||||
t = MathHelper.clamp(t, 0, 1);
|
||||
|
||||
return Vector2.add(lineA, Vector2.multiply(v, new Vector2(t, t)));
|
||||
}
|
||||
|
||||
public static pointToPoly(point: Vector2, poly: Polygon){
|
||||
let result = new CollisionResult();
|
||||
|
||||
if (poly.containsPoint(point)){
|
||||
let distanceSquared: number;
|
||||
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
||||
let closestPoint = gpp.closestPoint;
|
||||
distanceSquared = gpp.distanceSquared;
|
||||
result.normal = gpp.edgeNormal;
|
||||
|
||||
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
|
||||
result.point = Vector2.add(closestPoint, poly.position);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user