新增shapecollision 用于计算多边形碰撞
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
abstract class Collider extends Component{
|
||||
public shape: Shape;
|
||||
public physicsLayer = 1 << 0;
|
||||
public isTrigger: boolean;
|
||||
|
||||
public get bounds(): Rectangle {
|
||||
return this.shape.bounds;
|
||||
|
||||
@@ -46,8 +46,59 @@ class Rectangle {
|
||||
this.top < value.bottom;
|
||||
}
|
||||
|
||||
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number){
|
||||
|
||||
public contains(value: Vector2) {
|
||||
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
||||
(this.y <= value.y)) &&
|
||||
(value.y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number) {
|
||||
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Point): {res: Vector2, edgeNormal: Vector2} {
|
||||
let edgeNormal = new Vector2(0, 0);
|
||||
|
||||
let res = new Vector2(0, 0);
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
||||
|
||||
if (this.contains(res)){
|
||||
let dl = res.x - this.left;
|
||||
let dr = this.right - res.x;
|
||||
let dt = res.y - this.top;
|
||||
let db = this.bottom - res.y;
|
||||
|
||||
let min = MathHelper.minOf(dl, dr, dt, db);
|
||||
if (min == dt){
|
||||
res.y = this.top;
|
||||
edgeNormal.y = -1;
|
||||
} else if(min == db){
|
||||
res.y = this.bottom;
|
||||
edgeNormal.y = 1;
|
||||
} else if(min == dl){
|
||||
res.x = this.left;
|
||||
edgeNormal.x = -1;
|
||||
} else{
|
||||
res.x = this.right;
|
||||
edgeNormal.x = 1;
|
||||
}
|
||||
} else {
|
||||
if (res.x == this.left){
|
||||
edgeNormal.x = -1;
|
||||
}
|
||||
if (res.x == this.right){
|
||||
edgeNormal.x = 1;
|
||||
}
|
||||
if (res.y == this.top){
|
||||
edgeNormal.y = -1;
|
||||
}
|
||||
if (res.y == this.bottom){
|
||||
edgeNormal.y = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return {res: res, edgeNormal: edgeNormal};
|
||||
}
|
||||
|
||||
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
|
||||
@@ -57,7 +108,7 @@ class Rectangle {
|
||||
this.y = parentPosition.y + position.y - origin.y * scale.y;
|
||||
this.width = width * scale.x;
|
||||
this.height = height * scale.y;
|
||||
}else{
|
||||
} else {
|
||||
let worldPosX = parentPosition.x + position.x;
|
||||
let worldPosY = parentPosition.y + position.y;
|
||||
|
||||
|
||||
@@ -52,6 +52,13 @@ class Vector2 {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
public static normalize(value: Vector2){
|
||||
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||
value.x *= val;
|
||||
value.y *= val;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量的点积
|
||||
* @param value1
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class Physics {
|
||||
private static _spatialHash: SpatialHash;
|
||||
|
||||
public static readonly allLayers: number = -1;
|
||||
|
||||
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ class Composite {
|
||||
public drawParticles: boolean = true;
|
||||
public drawConstraints: boolean = true;
|
||||
public particles: Particle[] = [];
|
||||
public collidesWithLayers = -1;
|
||||
public collidesWithLayers = Physics.allLayers;
|
||||
|
||||
/**
|
||||
* 处理解决所有约束条件
|
||||
|
||||
@@ -14,7 +14,8 @@ class VerletWorld {
|
||||
private _composites: Composite[] = [];
|
||||
private _fixedDeltaTimeSq: number;
|
||||
|
||||
private static _colliders = new Array(4);
|
||||
private static _colliders: Collider[] = new Array(4);
|
||||
private _tempCircle: Circle = new Circle(1);
|
||||
|
||||
constructor(simulationBounds?: Rectangle){
|
||||
this.simulationBounds = simulationBounds;
|
||||
@@ -43,8 +44,8 @@ class VerletWorld {
|
||||
this.constrainParticleToBounds(p);
|
||||
}
|
||||
|
||||
// if (p.collidesWithColliders)
|
||||
// this.handleCollisions(p, -1);
|
||||
if (p.collidesWithColliders)
|
||||
this.handleCollisions(p, composite.collidesWithLayers);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +53,26 @@ class VerletWorld {
|
||||
|
||||
private handleCollisions(p: Particle, collidesWithLayers: number){
|
||||
let collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
||||
// handle
|
||||
for (let i = 0; i < collidedCount; i ++){
|
||||
let collider = VerletWorld._colliders[i];
|
||||
if (collider.isTrigger)
|
||||
continue;
|
||||
|
||||
if (p.radius < 2){
|
||||
let collisionResult = collider.shape.pointCollidesWithShape(p.position);
|
||||
if (collisionResult){
|
||||
p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector);
|
||||
}
|
||||
}else{
|
||||
this._tempCircle.radius = p.radius;
|
||||
this._tempCircle.position = p.position;
|
||||
|
||||
let collisionResult = this._tempCircle.collidesWithShape(collider.shape);
|
||||
if (collisionResult){
|
||||
p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private constrainParticleToBounds(p: Particle){
|
||||
|
||||
Reference in New Issue
Block a user