新增shape形状

This commit is contained in:
yhh
2020-06-11 20:36:36 +08:00
parent 53ded30e0b
commit ad68f0e1a0
14 changed files with 235 additions and 67 deletions

View File

@@ -541,6 +541,7 @@ declare class Rectangle {
location: Vector2;
constructor(x: number, y: number, width: number, height: number);
intersects(value: Rectangle): boolean;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): void;
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
}
declare class Vector2 {
@@ -585,6 +586,17 @@ declare class Physics {
private static _spatialHash;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
}
declare abstract class Shape {
bounds: Rectangle;
}
declare class Polygon extends Shape {
points: Vector2[];
constructor(vertCount: number, radius: number);
setPoints(points: Vector2[]): void;
recalculateCenterAndEdgeNormals(): void;
static findPolygonCenter(points: Vector2[]): Vector2;
static buildSymmertricalPolygon(vertCount: number, radius: number): any;
}
declare class Particle {
position: Vector2;
lastPosition: Vector2;
@@ -625,11 +637,13 @@ declare class Composite {
drawParticles: boolean;
drawConstraints: boolean;
particles: Particle[];
collidesWithLayers: number;
solveConstraints(): void;
addParticle(particle: Particle): Particle;
addConstraint<T extends Constraint>(constraint: T): T;
removeConstraint(constraint: Constraint): void;
updateParticles(deltaTimeSquared: number, gravity: Vector2): void;
handleConstraintCollisions(): void;
debugRender(graphics: egret.Graphics): void;
}
declare class Box extends Composite {
@@ -639,6 +653,7 @@ declare abstract class Constraint {
composite: Composite;
collidesWithColliders: boolean;
abstract solve(): any;
handleCollisions(collidesWithLayers: number): void;
debugRender(graphics: egret.Graphics): void;
}
declare class DistanceConstraint extends Constraint {
@@ -647,8 +662,10 @@ declare class DistanceConstraint extends Constraint {
tearSensitivity: number;
private _particleOne;
private _particleTwo;
private static _polygon;
constructor(first: Particle, second: Particle, stiffness: number, distance?: number);
setCollidesWithColliders(collidesWithColliders: boolean): this;
handleCollisions(collidersWithLayers: any): void;
solve(): void;
debugRender(graphics: egret.Graphics): void;
}

View File

@@ -2552,6 +2552,8 @@ var Rectangle = (function () {
value.top < this.bottom &&
this.top < value.bottom;
};
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
};
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
if (rotation == 0) {
this.x = parentPosition.x + position.x - origin.x * scale.x;
@@ -2707,36 +2709,11 @@ var Collisions = (function () {
return Vector2.distanceSquared(circleCenter, point) < radius * radius;
};
Collisions.isRectToCircle = function (rect, cPosition, cRadius) {
if (this.isRectToPoint(rect.x, rect.y, rect.width, rect.height, cPosition))
return true;
var edgeFrom;
var edgeTo;
var 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;
var ew = rect.width * 0.5;
var eh = rect.height * 0.5;
var vx = Math.max(0, Math.max(cPosition.x - rect.x) - ew);
var vy = Math.max(0, Math.max(cPosition.y - rect.y) - eh);
return vx * vx + vy * vy < cRadius * cRadius;
};
Collisions.isRectToLine = function (rect, lineFrom, lineTo) {
var fromSector = this.getSector(rect.x, rect.y, rect.width, rect.height, lineFrom);
@@ -2804,6 +2781,42 @@ var Physics = (function () {
};
return Physics;
}());
var Shape = (function () {
function Shape() {
}
return Shape;
}());
var Polygon = (function (_super) {
__extends(Polygon, _super);
function Polygon(vertCount, radius) {
var _this = _super.call(this) || this;
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
return _this;
}
Polygon.prototype.setPoints = function (points) {
this.points = points;
this.recalculateCenterAndEdgeNormals();
};
Polygon.prototype.recalculateCenterAndEdgeNormals = function () {
};
Polygon.findPolygonCenter = function (points) {
var x = 0, y = 0;
for (var i = 0; i < points.length; i++) {
x += points[i].x;
y += points[i].y;
}
return new Vector2(x / points.length, y / points.length);
};
Polygon.buildSymmertricalPolygon = function (vertCount, radius) {
var verts = new Vector2[vertCount];
for (var i = 0; i < vertCount; i++) {
var a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
}
return verts;
};
return Polygon;
}(Shape));
var Particle = (function () {
function Particle(position) {
this.position = new Vector2(0, 0);
@@ -2850,6 +2863,7 @@ var VerletWorld = (function () {
composite.solveConstraints();
}
composite.updateParticles(this._fixedDeltaTimeSq, this.gravity);
composite.handleConstraintCollisions();
for (var j = 0; j < composite.particles.length; j++) {
var p = composite.particles[j];
if (this.simulationBounds) {
@@ -2924,6 +2938,7 @@ var Composite = (function () {
this.drawParticles = true;
this.drawConstraints = true;
this.particles = [];
this.collidesWithLayers = -1;
}
Composite.prototype.solveConstraints = function () {
for (var i = this._constraints.length - 1; i >= 0; i--) {
@@ -2957,6 +2972,12 @@ var Composite = (function () {
p.acceleration.x = p.acceleration.y = 0;
}
};
Composite.prototype.handleConstraintCollisions = function () {
for (var i = this._constraints.length - 1; i >= 0; i--) {
if (this._constraints[i].collidesWithColliders)
this._constraints[i].handleCollisions(this.collidesWithLayers);
}
};
Composite.prototype.debugRender = function (graphics) {
if (this.drawConstraints) {
for (var i = 0; i < this._constraints.length; i++) {
@@ -2998,6 +3019,8 @@ var Constraint = (function () {
function Constraint() {
this.collidesWithColliders = true;
}
Constraint.prototype.handleCollisions = function (collidesWithLayers) {
};
Constraint.prototype.debugRender = function (graphics) { };
return Constraint;
}());
@@ -3024,6 +3047,12 @@ var DistanceConstraint = (function (_super) {
this.collidesWithColliders = collidesWithColliders;
return this;
};
DistanceConstraint.prototype.handleCollisions = function (collidersWithLayers) {
var minX = Math.min(this._particleOne.position.x, this._particleTwo.position.x);
var maxX = Math.max(this._particleOne.position.x, this._particleTwo.position.x);
var minY = Math.min(this._particleOne.position.y, this._particleTwo.position.y);
var maxY = Math.max(this._particleOne.position.y, this._particleTwo.position.y);
};
DistanceConstraint.prototype.solve = function () {
var diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position);
var d = diff.length();
@@ -3045,6 +3074,7 @@ var DistanceConstraint = (function (_super) {
graphics.lineTo(this._particleTwo.position.x, this._particleTwo.position.y);
graphics.endFill();
};
DistanceConstraint._polygon = new Polygon(2, 1);
return DistanceConstraint;
}(Constraint));
var Triangulator = (function () {

File diff suppressed because one or more lines are too long