新增shapecollision 用于计算多边形碰撞
This commit is contained in:
39
demo/libs/framework/framework.d.ts
vendored
39
demo/libs/framework/framework.d.ts
vendored
@@ -379,6 +379,7 @@ declare class SpriteRenderer extends RenderableComponent {
|
|||||||
declare abstract class Collider extends Component {
|
declare abstract class Collider extends Component {
|
||||||
shape: Shape;
|
shape: Shape;
|
||||||
physicsLayer: number;
|
physicsLayer: number;
|
||||||
|
isTrigger: boolean;
|
||||||
readonly bounds: Rectangle;
|
readonly bounds: Rectangle;
|
||||||
}
|
}
|
||||||
declare class EntitySystem {
|
declare class EntitySystem {
|
||||||
@@ -554,7 +555,12 @@ declare class Rectangle {
|
|||||||
location: Vector2;
|
location: Vector2;
|
||||||
constructor(x: number, y: number, width: number, height: number);
|
constructor(x: number, y: number, width: number, height: number);
|
||||||
intersects(value: Rectangle): boolean;
|
intersects(value: Rectangle): boolean;
|
||||||
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): void;
|
contains(value: Vector2): boolean;
|
||||||
|
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
|
||||||
|
getClosestPointOnRectangleBorderToPoint(point: Point): {
|
||||||
|
res: Vector2;
|
||||||
|
edgeNormal: Vector2;
|
||||||
|
};
|
||||||
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
|
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
|
||||||
}
|
}
|
||||||
declare class Vector2 {
|
declare class Vector2 {
|
||||||
@@ -567,6 +573,7 @@ declare class Vector2 {
|
|||||||
static subtract(value1: Vector2, value2: Vector2): Vector2;
|
static subtract(value1: Vector2, value2: Vector2): Vector2;
|
||||||
normalize(): void;
|
normalize(): void;
|
||||||
length(): number;
|
length(): number;
|
||||||
|
static normalize(value: Vector2): Vector2;
|
||||||
static dot(value1: Vector2, value2: Vector2): number;
|
static dot(value1: Vector2, value2: Vector2): number;
|
||||||
static distanceSquared(value1: Vector2, value2: Vector2): number;
|
static distanceSquared(value1: Vector2, value2: Vector2): number;
|
||||||
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
||||||
@@ -597,25 +604,54 @@ declare class Collisions {
|
|||||||
}
|
}
|
||||||
declare class Physics {
|
declare class Physics {
|
||||||
private static _spatialHash;
|
private static _spatialHash;
|
||||||
|
static readonly allLayers: number;
|
||||||
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
|
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
|
||||||
}
|
}
|
||||||
declare abstract class Shape {
|
declare abstract class Shape {
|
||||||
bounds: Rectangle;
|
bounds: Rectangle;
|
||||||
position: Vector2;
|
position: Vector2;
|
||||||
|
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
}
|
}
|
||||||
declare class Circle extends Shape {
|
declare class Circle extends Shape {
|
||||||
radius: number;
|
radius: number;
|
||||||
private _originalRadius;
|
private _originalRadius;
|
||||||
constructor(radius: number);
|
constructor(radius: number);
|
||||||
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
|
}
|
||||||
|
declare class CollisionResult {
|
||||||
|
minimumTranslationVector: Vector2;
|
||||||
|
normal: Vector2;
|
||||||
|
point: Vector2;
|
||||||
}
|
}
|
||||||
declare class Polygon extends Shape {
|
declare class Polygon extends Shape {
|
||||||
points: Vector2[];
|
points: Vector2[];
|
||||||
|
isUnrotated: boolean;
|
||||||
|
private _polygonCenter;
|
||||||
|
private _areEdgeNormalsDirty;
|
||||||
|
private _originalPoint;
|
||||||
constructor(vertCount: number, radius: number);
|
constructor(vertCount: number, radius: number);
|
||||||
setPoints(points: Vector2[]): void;
|
setPoints(points: Vector2[]): void;
|
||||||
recalculateCenterAndEdgeNormals(): void;
|
recalculateCenterAndEdgeNormals(): void;
|
||||||
static findPolygonCenter(points: Vector2[]): Vector2;
|
static findPolygonCenter(points: Vector2[]): Vector2;
|
||||||
|
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
||||||
|
closestPoint: any;
|
||||||
|
distanceSquared: any;
|
||||||
|
edgeNormal: any;
|
||||||
|
};
|
||||||
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
containsPoint(point: Vector2): boolean;
|
||||||
static buildSymmertricalPolygon(vertCount: number, radius: number): any;
|
static buildSymmertricalPolygon(vertCount: number, radius: number): any;
|
||||||
}
|
}
|
||||||
|
declare class Rect extends Polygon {
|
||||||
|
containsPoint(point: Vector2): boolean;
|
||||||
|
}
|
||||||
|
declare class ShapeCollisions {
|
||||||
|
static circleToRect(circle: Circle, box: Rect): CollisionResult;
|
||||||
|
static pointToCicle(point: Vector2, circle: Circle): CollisionResult;
|
||||||
|
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
||||||
|
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
||||||
|
}
|
||||||
declare class Particle {
|
declare class Particle {
|
||||||
position: Vector2;
|
position: Vector2;
|
||||||
lastPosition: Vector2;
|
lastPosition: Vector2;
|
||||||
@@ -663,6 +699,7 @@ declare class VerletWorld {
|
|||||||
private _composites;
|
private _composites;
|
||||||
private _fixedDeltaTimeSq;
|
private _fixedDeltaTimeSq;
|
||||||
private static _colliders;
|
private static _colliders;
|
||||||
|
private _tempCircle;
|
||||||
constructor(simulationBounds?: Rectangle);
|
constructor(simulationBounds?: Rectangle);
|
||||||
update(): void;
|
update(): void;
|
||||||
private handleCollisions;
|
private handleCollisions;
|
||||||
|
|||||||
@@ -2593,7 +2593,57 @@ var Rectangle = (function () {
|
|||||||
value.top < this.bottom &&
|
value.top < this.bottom &&
|
||||||
this.top < value.bottom;
|
this.top < value.bottom;
|
||||||
};
|
};
|
||||||
|
Rectangle.prototype.contains = function (value) {
|
||||||
|
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
||||||
|
(this.y <= value.y)) &&
|
||||||
|
(value.y < (this.y + this.height)));
|
||||||
|
};
|
||||||
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
|
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
|
||||||
|
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||||
|
};
|
||||||
|
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point) {
|
||||||
|
var edgeNormal = new Vector2(0, 0);
|
||||||
|
var 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)) {
|
||||||
|
var dl = res.x - this.left;
|
||||||
|
var dr = this.right - res.x;
|
||||||
|
var dt = res.y - this.top;
|
||||||
|
var db = this.bottom - res.y;
|
||||||
|
var 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 };
|
||||||
};
|
};
|
||||||
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
||||||
if (rotation == 0) {
|
if (rotation == 0) {
|
||||||
@@ -2670,6 +2720,12 @@ var Vector2 = (function () {
|
|||||||
Vector2.prototype.length = function () {
|
Vector2.prototype.length = function () {
|
||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||||
};
|
};
|
||||||
|
Vector2.normalize = function (value) {
|
||||||
|
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||||
|
value.x *= val;
|
||||||
|
value.y *= val;
|
||||||
|
return value;
|
||||||
|
};
|
||||||
Vector2.dot = function (value1, value2) {
|
Vector2.dot = function (value1, value2) {
|
||||||
return (value1.x * value2.x) + (value1.y * value2.y);
|
return (value1.x * value2.x) + (value1.y * value2.y);
|
||||||
};
|
};
|
||||||
@@ -2820,6 +2876,7 @@ var Physics = (function () {
|
|||||||
if (layerMask === void 0) { layerMask = -1; }
|
if (layerMask === void 0) { layerMask = -1; }
|
||||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||||
};
|
};
|
||||||
|
Physics.allLayers = -1;
|
||||||
return Physics;
|
return Physics;
|
||||||
}());
|
}());
|
||||||
var Shape = (function () {
|
var Shape = (function () {
|
||||||
@@ -2835,20 +2892,40 @@ var Circle = (function (_super) {
|
|||||||
_this._originalRadius = radius;
|
_this._originalRadius = radius;
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
Circle.prototype.pointCollidesWithShape = function (point) {
|
||||||
|
return ShapeCollisions.pointToCicle(point, this);
|
||||||
|
};
|
||||||
|
Circle.prototype.collidesWithShape = function (other) {
|
||||||
|
if (other instanceof Rect && other.isUnrotated) {
|
||||||
|
return ShapeCollisions.circleToRect(this, other);
|
||||||
|
}
|
||||||
|
throw new Error("Collisions of Circle to " + other + " are not supported");
|
||||||
|
};
|
||||||
return Circle;
|
return Circle;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
|
var CollisionResult = (function () {
|
||||||
|
function CollisionResult() {
|
||||||
|
}
|
||||||
|
return CollisionResult;
|
||||||
|
}());
|
||||||
var Polygon = (function (_super) {
|
var Polygon = (function (_super) {
|
||||||
__extends(Polygon, _super);
|
__extends(Polygon, _super);
|
||||||
function Polygon(vertCount, radius) {
|
function Polygon(vertCount, radius) {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
|
_this.isUnrotated = true;
|
||||||
|
_this._areEdgeNormalsDirty = true;
|
||||||
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Polygon.prototype.setPoints = function (points) {
|
Polygon.prototype.setPoints = function (points) {
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.recalculateCenterAndEdgeNormals();
|
this.recalculateCenterAndEdgeNormals();
|
||||||
|
this._originalPoint = new Vector2[points.length];
|
||||||
|
this._originalPoint = points;
|
||||||
};
|
};
|
||||||
Polygon.prototype.recalculateCenterAndEdgeNormals = function () {
|
Polygon.prototype.recalculateCenterAndEdgeNormals = function () {
|
||||||
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||||
|
this._areEdgeNormalsDirty = true;
|
||||||
};
|
};
|
||||||
Polygon.findPolygonCenter = function (points) {
|
Polygon.findPolygonCenter = function (points) {
|
||||||
var x = 0, y = 0;
|
var x = 0, y = 0;
|
||||||
@@ -2858,6 +2935,43 @@ var Polygon = (function (_super) {
|
|||||||
}
|
}
|
||||||
return new Vector2(x / points.length, y / points.length);
|
return new Vector2(x / points.length, y / points.length);
|
||||||
};
|
};
|
||||||
|
Polygon.getClosestPointOnPolygonToPoint = function (points, point) {
|
||||||
|
var distanceSquared = Number.MAX_VALUE;
|
||||||
|
var edgeNormal = new Vector2(0, 0);
|
||||||
|
var closestPoint = new Vector2(0, 0);
|
||||||
|
var tempDistanceSquared;
|
||||||
|
for (var i = 0; i < points.length; i++) {
|
||||||
|
var j = i + 1;
|
||||||
|
if (j == points.length)
|
||||||
|
j = 0;
|
||||||
|
var closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
||||||
|
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
||||||
|
if (tempDistanceSquared < distanceSquared) {
|
||||||
|
distanceSquared = tempDistanceSquared;
|
||||||
|
closestPoint = closest;
|
||||||
|
var 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 };
|
||||||
|
};
|
||||||
|
Polygon.prototype.pointCollidesWithShape = function (point) {
|
||||||
|
return ShapeCollisions.pointToPoly(point, this);
|
||||||
|
};
|
||||||
|
Polygon.prototype.containsPoint = function (point) {
|
||||||
|
point = Vector2.subtract(point, this.position);
|
||||||
|
var isInside = false;
|
||||||
|
for (var 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;
|
||||||
|
};
|
||||||
Polygon.buildSymmertricalPolygon = function (vertCount, radius) {
|
Polygon.buildSymmertricalPolygon = function (vertCount, radius) {
|
||||||
var verts = new Vector2[vertCount];
|
var verts = new Vector2[vertCount];
|
||||||
for (var i = 0; i < vertCount; i++) {
|
for (var i = 0; i < vertCount; i++) {
|
||||||
@@ -2868,6 +2982,67 @@ var Polygon = (function (_super) {
|
|||||||
};
|
};
|
||||||
return Polygon;
|
return Polygon;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
|
var Rect = (function (_super) {
|
||||||
|
__extends(Rect, _super);
|
||||||
|
function Rect() {
|
||||||
|
return _super !== null && _super.apply(this, arguments) || this;
|
||||||
|
}
|
||||||
|
Rect.prototype.containsPoint = function (point) {
|
||||||
|
if (this.isUnrotated)
|
||||||
|
return this.bounds.contains(point);
|
||||||
|
return _super.prototype.containsPoint.call(this, point);
|
||||||
|
};
|
||||||
|
return Rect;
|
||||||
|
}(Polygon));
|
||||||
|
var ShapeCollisions = (function () {
|
||||||
|
function ShapeCollisions() {
|
||||||
|
}
|
||||||
|
ShapeCollisions.circleToRect = function (circle, box) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||||
|
if (box.containsPoint(circle.position)) {
|
||||||
|
result.point = closestPointOnBounds;
|
||||||
|
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
ShapeCollisions.pointToCicle = function (point, circle) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||||
|
var sumOfRadii = 1 + circle.radius;
|
||||||
|
var collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
|
if (collided) {
|
||||||
|
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
||||||
|
var 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;
|
||||||
|
};
|
||||||
|
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
||||||
|
var v = Vector2.subtract(lineB, lineA);
|
||||||
|
var w = Vector2.subtract(closestTo, lineA);
|
||||||
|
var 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)));
|
||||||
|
};
|
||||||
|
ShapeCollisions.pointToPoly = function (point, poly) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
if (poly.containsPoint(point)) {
|
||||||
|
var distanceSquared = void 0;
|
||||||
|
var gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
||||||
|
var 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;
|
||||||
|
};
|
||||||
|
return ShapeCollisions;
|
||||||
|
}());
|
||||||
var Particle = (function () {
|
var Particle = (function () {
|
||||||
function Particle(position) {
|
function Particle(position) {
|
||||||
this.position = new Vector2(0, 0);
|
this.position = new Vector2(0, 0);
|
||||||
@@ -2984,6 +3159,7 @@ var VerletWorld = (function () {
|
|||||||
this._iterationSteps = 0;
|
this._iterationSteps = 0;
|
||||||
this._fixedDeltaTime = 1 / 60;
|
this._fixedDeltaTime = 1 / 60;
|
||||||
this._composites = [];
|
this._composites = [];
|
||||||
|
this._tempCircle = new Circle(1);
|
||||||
this.simulationBounds = simulationBounds;
|
this.simulationBounds = simulationBounds;
|
||||||
this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2);
|
this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2);
|
||||||
}
|
}
|
||||||
@@ -3002,12 +3178,33 @@ var VerletWorld = (function () {
|
|||||||
if (this.simulationBounds) {
|
if (this.simulationBounds) {
|
||||||
this.constrainParticleToBounds(p);
|
this.constrainParticleToBounds(p);
|
||||||
}
|
}
|
||||||
|
if (p.collidesWithColliders)
|
||||||
|
this.handleCollisions(p, composite.collidesWithLayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) {
|
VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) {
|
||||||
var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
||||||
|
for (var i = 0; i < collidedCount; i++) {
|
||||||
|
var collider = VerletWorld._colliders[i];
|
||||||
|
if (collider.isTrigger)
|
||||||
|
continue;
|
||||||
|
if (p.radius < 2) {
|
||||||
|
var 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;
|
||||||
|
var collisionResult = this._tempCircle.collidesWithShape(collider.shape);
|
||||||
|
if (collisionResult) {
|
||||||
|
p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
VerletWorld.prototype.constrainParticleToBounds = function (p) {
|
VerletWorld.prototype.constrainParticleToBounds = function (p) {
|
||||||
var tempPos = p.position;
|
var tempPos = p.position;
|
||||||
@@ -3071,7 +3268,7 @@ var Composite = (function () {
|
|||||||
this.drawParticles = true;
|
this.drawParticles = true;
|
||||||
this.drawConstraints = true;
|
this.drawConstraints = true;
|
||||||
this.particles = [];
|
this.particles = [];
|
||||||
this.collidesWithLayers = -1;
|
this.collidesWithLayers = Physics.allLayers;
|
||||||
}
|
}
|
||||||
Composite.prototype.solveConstraints = function () {
|
Composite.prototype.solveConstraints = function () {
|
||||||
for (var i = this._constraints.length - 1; i >= 0; i--) {
|
for (var i = this._constraints.length - 1; i >= 0; i--) {
|
||||||
|
|||||||
2
demo/libs/framework/framework.min.js
vendored
2
demo/libs/framework/framework.min.js
vendored
File diff suppressed because one or more lines are too long
39
source/bin/framework.d.ts
vendored
39
source/bin/framework.d.ts
vendored
@@ -379,6 +379,7 @@ declare class SpriteRenderer extends RenderableComponent {
|
|||||||
declare abstract class Collider extends Component {
|
declare abstract class Collider extends Component {
|
||||||
shape: Shape;
|
shape: Shape;
|
||||||
physicsLayer: number;
|
physicsLayer: number;
|
||||||
|
isTrigger: boolean;
|
||||||
readonly bounds: Rectangle;
|
readonly bounds: Rectangle;
|
||||||
}
|
}
|
||||||
declare class EntitySystem {
|
declare class EntitySystem {
|
||||||
@@ -554,7 +555,12 @@ declare class Rectangle {
|
|||||||
location: Vector2;
|
location: Vector2;
|
||||||
constructor(x: number, y: number, width: number, height: number);
|
constructor(x: number, y: number, width: number, height: number);
|
||||||
intersects(value: Rectangle): boolean;
|
intersects(value: Rectangle): boolean;
|
||||||
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): void;
|
contains(value: Vector2): boolean;
|
||||||
|
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
|
||||||
|
getClosestPointOnRectangleBorderToPoint(point: Point): {
|
||||||
|
res: Vector2;
|
||||||
|
edgeNormal: Vector2;
|
||||||
|
};
|
||||||
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
|
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
|
||||||
}
|
}
|
||||||
declare class Vector2 {
|
declare class Vector2 {
|
||||||
@@ -567,6 +573,7 @@ declare class Vector2 {
|
|||||||
static subtract(value1: Vector2, value2: Vector2): Vector2;
|
static subtract(value1: Vector2, value2: Vector2): Vector2;
|
||||||
normalize(): void;
|
normalize(): void;
|
||||||
length(): number;
|
length(): number;
|
||||||
|
static normalize(value: Vector2): Vector2;
|
||||||
static dot(value1: Vector2, value2: Vector2): number;
|
static dot(value1: Vector2, value2: Vector2): number;
|
||||||
static distanceSquared(value1: Vector2, value2: Vector2): number;
|
static distanceSquared(value1: Vector2, value2: Vector2): number;
|
||||||
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
static transform(position: Vector2, matrix: Matrix2D): Vector2;
|
||||||
@@ -597,25 +604,54 @@ declare class Collisions {
|
|||||||
}
|
}
|
||||||
declare class Physics {
|
declare class Physics {
|
||||||
private static _spatialHash;
|
private static _spatialHash;
|
||||||
|
static readonly allLayers: number;
|
||||||
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
|
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
|
||||||
}
|
}
|
||||||
declare abstract class Shape {
|
declare abstract class Shape {
|
||||||
bounds: Rectangle;
|
bounds: Rectangle;
|
||||||
position: Vector2;
|
position: Vector2;
|
||||||
|
abstract pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
}
|
}
|
||||||
declare class Circle extends Shape {
|
declare class Circle extends Shape {
|
||||||
radius: number;
|
radius: number;
|
||||||
private _originalRadius;
|
private _originalRadius;
|
||||||
constructor(radius: number);
|
constructor(radius: number);
|
||||||
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
collidesWithShape(other: Shape): CollisionResult;
|
||||||
|
}
|
||||||
|
declare class CollisionResult {
|
||||||
|
minimumTranslationVector: Vector2;
|
||||||
|
normal: Vector2;
|
||||||
|
point: Vector2;
|
||||||
}
|
}
|
||||||
declare class Polygon extends Shape {
|
declare class Polygon extends Shape {
|
||||||
points: Vector2[];
|
points: Vector2[];
|
||||||
|
isUnrotated: boolean;
|
||||||
|
private _polygonCenter;
|
||||||
|
private _areEdgeNormalsDirty;
|
||||||
|
private _originalPoint;
|
||||||
constructor(vertCount: number, radius: number);
|
constructor(vertCount: number, radius: number);
|
||||||
setPoints(points: Vector2[]): void;
|
setPoints(points: Vector2[]): void;
|
||||||
recalculateCenterAndEdgeNormals(): void;
|
recalculateCenterAndEdgeNormals(): void;
|
||||||
static findPolygonCenter(points: Vector2[]): Vector2;
|
static findPolygonCenter(points: Vector2[]): Vector2;
|
||||||
|
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): {
|
||||||
|
closestPoint: any;
|
||||||
|
distanceSquared: any;
|
||||||
|
edgeNormal: any;
|
||||||
|
};
|
||||||
|
pointCollidesWithShape(point: Vector2): CollisionResult;
|
||||||
|
containsPoint(point: Vector2): boolean;
|
||||||
static buildSymmertricalPolygon(vertCount: number, radius: number): any;
|
static buildSymmertricalPolygon(vertCount: number, radius: number): any;
|
||||||
}
|
}
|
||||||
|
declare class Rect extends Polygon {
|
||||||
|
containsPoint(point: Vector2): boolean;
|
||||||
|
}
|
||||||
|
declare class ShapeCollisions {
|
||||||
|
static circleToRect(circle: Circle, box: Rect): CollisionResult;
|
||||||
|
static pointToCicle(point: Vector2, circle: Circle): CollisionResult;
|
||||||
|
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
|
||||||
|
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
|
||||||
|
}
|
||||||
declare class Particle {
|
declare class Particle {
|
||||||
position: Vector2;
|
position: Vector2;
|
||||||
lastPosition: Vector2;
|
lastPosition: Vector2;
|
||||||
@@ -663,6 +699,7 @@ declare class VerletWorld {
|
|||||||
private _composites;
|
private _composites;
|
||||||
private _fixedDeltaTimeSq;
|
private _fixedDeltaTimeSq;
|
||||||
private static _colliders;
|
private static _colliders;
|
||||||
|
private _tempCircle;
|
||||||
constructor(simulationBounds?: Rectangle);
|
constructor(simulationBounds?: Rectangle);
|
||||||
update(): void;
|
update(): void;
|
||||||
private handleCollisions;
|
private handleCollisions;
|
||||||
|
|||||||
@@ -2593,7 +2593,57 @@ var Rectangle = (function () {
|
|||||||
value.top < this.bottom &&
|
value.top < this.bottom &&
|
||||||
this.top < value.bottom;
|
this.top < value.bottom;
|
||||||
};
|
};
|
||||||
|
Rectangle.prototype.contains = function (value) {
|
||||||
|
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
||||||
|
(this.y <= value.y)) &&
|
||||||
|
(value.y < (this.y + this.height)));
|
||||||
|
};
|
||||||
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
|
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
|
||||||
|
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||||
|
};
|
||||||
|
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point) {
|
||||||
|
var edgeNormal = new Vector2(0, 0);
|
||||||
|
var 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)) {
|
||||||
|
var dl = res.x - this.left;
|
||||||
|
var dr = this.right - res.x;
|
||||||
|
var dt = res.y - this.top;
|
||||||
|
var db = this.bottom - res.y;
|
||||||
|
var 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 };
|
||||||
};
|
};
|
||||||
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
|
||||||
if (rotation == 0) {
|
if (rotation == 0) {
|
||||||
@@ -2670,6 +2720,12 @@ var Vector2 = (function () {
|
|||||||
Vector2.prototype.length = function () {
|
Vector2.prototype.length = function () {
|
||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||||
};
|
};
|
||||||
|
Vector2.normalize = function (value) {
|
||||||
|
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||||
|
value.x *= val;
|
||||||
|
value.y *= val;
|
||||||
|
return value;
|
||||||
|
};
|
||||||
Vector2.dot = function (value1, value2) {
|
Vector2.dot = function (value1, value2) {
|
||||||
return (value1.x * value2.x) + (value1.y * value2.y);
|
return (value1.x * value2.x) + (value1.y * value2.y);
|
||||||
};
|
};
|
||||||
@@ -2820,6 +2876,7 @@ var Physics = (function () {
|
|||||||
if (layerMask === void 0) { layerMask = -1; }
|
if (layerMask === void 0) { layerMask = -1; }
|
||||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||||
};
|
};
|
||||||
|
Physics.allLayers = -1;
|
||||||
return Physics;
|
return Physics;
|
||||||
}());
|
}());
|
||||||
var Shape = (function () {
|
var Shape = (function () {
|
||||||
@@ -2835,20 +2892,40 @@ var Circle = (function (_super) {
|
|||||||
_this._originalRadius = radius;
|
_this._originalRadius = radius;
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
Circle.prototype.pointCollidesWithShape = function (point) {
|
||||||
|
return ShapeCollisions.pointToCicle(point, this);
|
||||||
|
};
|
||||||
|
Circle.prototype.collidesWithShape = function (other) {
|
||||||
|
if (other instanceof Rect && other.isUnrotated) {
|
||||||
|
return ShapeCollisions.circleToRect(this, other);
|
||||||
|
}
|
||||||
|
throw new Error("Collisions of Circle to " + other + " are not supported");
|
||||||
|
};
|
||||||
return Circle;
|
return Circle;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
|
var CollisionResult = (function () {
|
||||||
|
function CollisionResult() {
|
||||||
|
}
|
||||||
|
return CollisionResult;
|
||||||
|
}());
|
||||||
var Polygon = (function (_super) {
|
var Polygon = (function (_super) {
|
||||||
__extends(Polygon, _super);
|
__extends(Polygon, _super);
|
||||||
function Polygon(vertCount, radius) {
|
function Polygon(vertCount, radius) {
|
||||||
var _this = _super.call(this) || this;
|
var _this = _super.call(this) || this;
|
||||||
|
_this.isUnrotated = true;
|
||||||
|
_this._areEdgeNormalsDirty = true;
|
||||||
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
Polygon.prototype.setPoints = function (points) {
|
Polygon.prototype.setPoints = function (points) {
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.recalculateCenterAndEdgeNormals();
|
this.recalculateCenterAndEdgeNormals();
|
||||||
|
this._originalPoint = new Vector2[points.length];
|
||||||
|
this._originalPoint = points;
|
||||||
};
|
};
|
||||||
Polygon.prototype.recalculateCenterAndEdgeNormals = function () {
|
Polygon.prototype.recalculateCenterAndEdgeNormals = function () {
|
||||||
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
||||||
|
this._areEdgeNormalsDirty = true;
|
||||||
};
|
};
|
||||||
Polygon.findPolygonCenter = function (points) {
|
Polygon.findPolygonCenter = function (points) {
|
||||||
var x = 0, y = 0;
|
var x = 0, y = 0;
|
||||||
@@ -2858,6 +2935,43 @@ var Polygon = (function (_super) {
|
|||||||
}
|
}
|
||||||
return new Vector2(x / points.length, y / points.length);
|
return new Vector2(x / points.length, y / points.length);
|
||||||
};
|
};
|
||||||
|
Polygon.getClosestPointOnPolygonToPoint = function (points, point) {
|
||||||
|
var distanceSquared = Number.MAX_VALUE;
|
||||||
|
var edgeNormal = new Vector2(0, 0);
|
||||||
|
var closestPoint = new Vector2(0, 0);
|
||||||
|
var tempDistanceSquared;
|
||||||
|
for (var i = 0; i < points.length; i++) {
|
||||||
|
var j = i + 1;
|
||||||
|
if (j == points.length)
|
||||||
|
j = 0;
|
||||||
|
var closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
||||||
|
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
||||||
|
if (tempDistanceSquared < distanceSquared) {
|
||||||
|
distanceSquared = tempDistanceSquared;
|
||||||
|
closestPoint = closest;
|
||||||
|
var 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 };
|
||||||
|
};
|
||||||
|
Polygon.prototype.pointCollidesWithShape = function (point) {
|
||||||
|
return ShapeCollisions.pointToPoly(point, this);
|
||||||
|
};
|
||||||
|
Polygon.prototype.containsPoint = function (point) {
|
||||||
|
point = Vector2.subtract(point, this.position);
|
||||||
|
var isInside = false;
|
||||||
|
for (var 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;
|
||||||
|
};
|
||||||
Polygon.buildSymmertricalPolygon = function (vertCount, radius) {
|
Polygon.buildSymmertricalPolygon = function (vertCount, radius) {
|
||||||
var verts = new Vector2[vertCount];
|
var verts = new Vector2[vertCount];
|
||||||
for (var i = 0; i < vertCount; i++) {
|
for (var i = 0; i < vertCount; i++) {
|
||||||
@@ -2868,6 +2982,67 @@ var Polygon = (function (_super) {
|
|||||||
};
|
};
|
||||||
return Polygon;
|
return Polygon;
|
||||||
}(Shape));
|
}(Shape));
|
||||||
|
var Rect = (function (_super) {
|
||||||
|
__extends(Rect, _super);
|
||||||
|
function Rect() {
|
||||||
|
return _super !== null && _super.apply(this, arguments) || this;
|
||||||
|
}
|
||||||
|
Rect.prototype.containsPoint = function (point) {
|
||||||
|
if (this.isUnrotated)
|
||||||
|
return this.bounds.contains(point);
|
||||||
|
return _super.prototype.containsPoint.call(this, point);
|
||||||
|
};
|
||||||
|
return Rect;
|
||||||
|
}(Polygon));
|
||||||
|
var ShapeCollisions = (function () {
|
||||||
|
function ShapeCollisions() {
|
||||||
|
}
|
||||||
|
ShapeCollisions.circleToRect = function (circle, box) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
||||||
|
if (box.containsPoint(circle.position)) {
|
||||||
|
result.point = closestPointOnBounds;
|
||||||
|
var safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius, circle.radius)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
ShapeCollisions.pointToCicle = function (point, circle) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
var distanceSquared = Vector2.distanceSquared(point, circle.position);
|
||||||
|
var sumOfRadii = 1 + circle.radius;
|
||||||
|
var collided = distanceSquared < sumOfRadii * sumOfRadii;
|
||||||
|
if (collided) {
|
||||||
|
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
||||||
|
var 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;
|
||||||
|
};
|
||||||
|
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
|
||||||
|
var v = Vector2.subtract(lineB, lineA);
|
||||||
|
var w = Vector2.subtract(closestTo, lineA);
|
||||||
|
var 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)));
|
||||||
|
};
|
||||||
|
ShapeCollisions.pointToPoly = function (point, poly) {
|
||||||
|
var result = new CollisionResult();
|
||||||
|
if (poly.containsPoint(point)) {
|
||||||
|
var distanceSquared = void 0;
|
||||||
|
var gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
||||||
|
var 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;
|
||||||
|
};
|
||||||
|
return ShapeCollisions;
|
||||||
|
}());
|
||||||
var Particle = (function () {
|
var Particle = (function () {
|
||||||
function Particle(position) {
|
function Particle(position) {
|
||||||
this.position = new Vector2(0, 0);
|
this.position = new Vector2(0, 0);
|
||||||
@@ -2984,6 +3159,7 @@ var VerletWorld = (function () {
|
|||||||
this._iterationSteps = 0;
|
this._iterationSteps = 0;
|
||||||
this._fixedDeltaTime = 1 / 60;
|
this._fixedDeltaTime = 1 / 60;
|
||||||
this._composites = [];
|
this._composites = [];
|
||||||
|
this._tempCircle = new Circle(1);
|
||||||
this.simulationBounds = simulationBounds;
|
this.simulationBounds = simulationBounds;
|
||||||
this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2);
|
this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2);
|
||||||
}
|
}
|
||||||
@@ -3002,12 +3178,33 @@ var VerletWorld = (function () {
|
|||||||
if (this.simulationBounds) {
|
if (this.simulationBounds) {
|
||||||
this.constrainParticleToBounds(p);
|
this.constrainParticleToBounds(p);
|
||||||
}
|
}
|
||||||
|
if (p.collidesWithColliders)
|
||||||
|
this.handleCollisions(p, composite.collidesWithLayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) {
|
VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) {
|
||||||
var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
||||||
|
for (var i = 0; i < collidedCount; i++) {
|
||||||
|
var collider = VerletWorld._colliders[i];
|
||||||
|
if (collider.isTrigger)
|
||||||
|
continue;
|
||||||
|
if (p.radius < 2) {
|
||||||
|
var 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;
|
||||||
|
var collisionResult = this._tempCircle.collidesWithShape(collider.shape);
|
||||||
|
if (collisionResult) {
|
||||||
|
p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
VerletWorld.prototype.constrainParticleToBounds = function (p) {
|
VerletWorld.prototype.constrainParticleToBounds = function (p) {
|
||||||
var tempPos = p.position;
|
var tempPos = p.position;
|
||||||
@@ -3071,7 +3268,7 @@ var Composite = (function () {
|
|||||||
this.drawParticles = true;
|
this.drawParticles = true;
|
||||||
this.drawConstraints = true;
|
this.drawConstraints = true;
|
||||||
this.particles = [];
|
this.particles = [];
|
||||||
this.collidesWithLayers = -1;
|
this.collidesWithLayers = Physics.allLayers;
|
||||||
}
|
}
|
||||||
Composite.prototype.solveConstraints = function () {
|
Composite.prototype.solveConstraints = function () {
|
||||||
for (var i = this._constraints.length - 1; i >= 0; i--) {
|
for (var i = this._constraints.length - 1; i >= 0; i--) {
|
||||||
|
|||||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,6 +1,7 @@
|
|||||||
abstract class Collider extends Component{
|
abstract class Collider extends Component{
|
||||||
public shape: Shape;
|
public shape: Shape;
|
||||||
public physicsLayer = 1 << 0;
|
public physicsLayer = 1 << 0;
|
||||||
|
public isTrigger: boolean;
|
||||||
|
|
||||||
public get bounds(): Rectangle {
|
public get bounds(): Rectangle {
|
||||||
return this.shape.bounds;
|
return this.shape.bounds;
|
||||||
|
|||||||
@@ -46,8 +46,59 @@ class Rectangle {
|
|||||||
this.top < value.bottom;
|
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,
|
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.y = parentPosition.y + position.y - origin.y * scale.y;
|
||||||
this.width = width * scale.x;
|
this.width = width * scale.x;
|
||||||
this.height = height * scale.y;
|
this.height = height * scale.y;
|
||||||
}else{
|
} else {
|
||||||
let worldPosX = parentPosition.x + position.x;
|
let worldPosX = parentPosition.x + position.x;
|
||||||
let worldPosY = parentPosition.y + position.y;
|
let worldPosY = parentPosition.y + position.y;
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,13 @@ class Vector2 {
|
|||||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
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
|
* @param value1
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
class Physics {
|
class Physics {
|
||||||
private static _spatialHash: SpatialHash;
|
private static _spatialHash: SpatialHash;
|
||||||
|
|
||||||
|
public static readonly allLayers: number = -1;
|
||||||
|
|
||||||
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
||||||
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,4 +8,16 @@ class Circle extends Shape {
|
|||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
this._originalRadius = 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" />
|
///<reference path="./Shape.ts" />
|
||||||
class Polygon extends Shape {
|
class Polygon extends Shape {
|
||||||
public points: Vector2[];
|
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();
|
super();
|
||||||
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
||||||
}
|
}
|
||||||
|
|
||||||
public setPoints(points: Vector2[]){
|
public setPoints(points: Vector2[]) {
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.recalculateCenterAndEdgeNormals();
|
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;
|
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;
|
x += points[i].x;
|
||||||
y += points[i].y;
|
y += points[i].y;
|
||||||
}
|
}
|
||||||
@@ -27,10 +35,58 @@ class Polygon extends Shape {
|
|||||||
return new Vector2(x / points.length, y / points.length);
|
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];
|
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);
|
let a = 2 * Math.PI * (i / vertCount);
|
||||||
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
|
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 {
|
abstract class Shape {
|
||||||
public bounds: Rectangle;
|
public bounds: Rectangle;
|
||||||
public position: Vector2;
|
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 drawParticles: boolean = true;
|
||||||
public drawConstraints: boolean = true;
|
public drawConstraints: boolean = true;
|
||||||
public particles: Particle[] = [];
|
public particles: Particle[] = [];
|
||||||
public collidesWithLayers = -1;
|
public collidesWithLayers = Physics.allLayers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理解决所有约束条件
|
* 处理解决所有约束条件
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ class VerletWorld {
|
|||||||
private _composites: Composite[] = [];
|
private _composites: Composite[] = [];
|
||||||
private _fixedDeltaTimeSq: number;
|
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){
|
constructor(simulationBounds?: Rectangle){
|
||||||
this.simulationBounds = simulationBounds;
|
this.simulationBounds = simulationBounds;
|
||||||
@@ -43,8 +44,8 @@ class VerletWorld {
|
|||||||
this.constrainParticleToBounds(p);
|
this.constrainParticleToBounds(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (p.collidesWithColliders)
|
if (p.collidesWithColliders)
|
||||||
// this.handleCollisions(p, -1);
|
this.handleCollisions(p, composite.collidesWithLayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,7 +53,26 @@ class VerletWorld {
|
|||||||
|
|
||||||
private handleCollisions(p: Particle, collidesWithLayers: number){
|
private handleCollisions(p: Particle, collidesWithLayers: number){
|
||||||
let collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers);
|
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){
|
private constrainParticleToBounds(p: Particle){
|
||||||
|
|||||||
Reference in New Issue
Block a user