新增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; 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;
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 {
@@ -585,6 +586,17 @@ declare class Physics {
private static _spatialHash; private static _spatialHash;
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 {
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 { declare class Particle {
position: Vector2; position: Vector2;
lastPosition: Vector2; lastPosition: Vector2;
@@ -625,11 +637,13 @@ declare class Composite {
drawParticles: boolean; drawParticles: boolean;
drawConstraints: boolean; drawConstraints: boolean;
particles: Particle[]; particles: Particle[];
collidesWithLayers: number;
solveConstraints(): void; solveConstraints(): void;
addParticle(particle: Particle): Particle; addParticle(particle: Particle): Particle;
addConstraint<T extends Constraint>(constraint: T): T; addConstraint<T extends Constraint>(constraint: T): T;
removeConstraint(constraint: Constraint): void; removeConstraint(constraint: Constraint): void;
updateParticles(deltaTimeSquared: number, gravity: Vector2): void; updateParticles(deltaTimeSquared: number, gravity: Vector2): void;
handleConstraintCollisions(): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }
declare class Box extends Composite { declare class Box extends Composite {
@@ -639,6 +653,7 @@ declare abstract class Constraint {
composite: Composite; composite: Composite;
collidesWithColliders: boolean; collidesWithColliders: boolean;
abstract solve(): any; abstract solve(): any;
handleCollisions(collidesWithLayers: number): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }
declare class DistanceConstraint extends Constraint { declare class DistanceConstraint extends Constraint {
@@ -647,8 +662,10 @@ declare class DistanceConstraint extends Constraint {
tearSensitivity: number; tearSensitivity: number;
private _particleOne; private _particleOne;
private _particleTwo; private _particleTwo;
private static _polygon;
constructor(first: Particle, second: Particle, stiffness: number, distance?: number); constructor(first: Particle, second: Particle, stiffness: number, distance?: number);
setCollidesWithColliders(collidesWithColliders: boolean): this; setCollidesWithColliders(collidesWithColliders: boolean): this;
handleCollisions(collidersWithLayers: any): void;
solve(): void; solve(): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }

View File

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

File diff suppressed because one or more lines are too long

View File

@@ -1,24 +1,26 @@
class VerletDemo extends RenderableComponent { class VerletDemo extends RenderableComponent {
private _world: VerletWorld; private _world: VerletWorld;
private _stage: egret.Stage;
protected getWidth(){ protected getWidth(){
return 800; return this.entity.scene.stage.stageWidth;
} }
protected getHeight(){ protected getHeight(){
return 600; return this.entity.scene.stage.stageHeight;
} }
public onAddedToEntity(){ public onAddedToEntity(){
this._world = new VerletWorld(new Rectangle(0, 0, 800, 600)); this._stage = this.entity.scene.stage;
this._world = new VerletWorld(new Rectangle(0, 0, this.width, this.height));
this._world.addComposite(new Box(new Vector2(100, 100), 50, 20)); this._world.addComposite(new Box(new Vector2(100, 100), 50, 20));
this._world.addComposite(new Box(new Vector2(10, 10), 200, 100)); this._world.addComposite(new Box(new Vector2(10, 10), 200, 100));
this._world.debugRender(this.entity.scene.stage);
} }
public update(){ public update(){
this._world.update(); this._world.update();
this._world.debugRender(this._stage);
} }
initialize() { initialize() {

View File

@@ -541,6 +541,7 @@ 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;
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 {
@@ -585,6 +586,17 @@ declare class Physics {
private static _spatialHash; private static _spatialHash;
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 {
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 { declare class Particle {
position: Vector2; position: Vector2;
lastPosition: Vector2; lastPosition: Vector2;
@@ -625,11 +637,13 @@ declare class Composite {
drawParticles: boolean; drawParticles: boolean;
drawConstraints: boolean; drawConstraints: boolean;
particles: Particle[]; particles: Particle[];
collidesWithLayers: number;
solveConstraints(): void; solveConstraints(): void;
addParticle(particle: Particle): Particle; addParticle(particle: Particle): Particle;
addConstraint<T extends Constraint>(constraint: T): T; addConstraint<T extends Constraint>(constraint: T): T;
removeConstraint(constraint: Constraint): void; removeConstraint(constraint: Constraint): void;
updateParticles(deltaTimeSquared: number, gravity: Vector2): void; updateParticles(deltaTimeSquared: number, gravity: Vector2): void;
handleConstraintCollisions(): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }
declare class Box extends Composite { declare class Box extends Composite {
@@ -639,6 +653,7 @@ declare abstract class Constraint {
composite: Composite; composite: Composite;
collidesWithColliders: boolean; collidesWithColliders: boolean;
abstract solve(): any; abstract solve(): any;
handleCollisions(collidesWithLayers: number): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }
declare class DistanceConstraint extends Constraint { declare class DistanceConstraint extends Constraint {
@@ -647,8 +662,10 @@ declare class DistanceConstraint extends Constraint {
tearSensitivity: number; tearSensitivity: number;
private _particleOne; private _particleOne;
private _particleTwo; private _particleTwo;
private static _polygon;
constructor(first: Particle, second: Particle, stiffness: number, distance?: number); constructor(first: Particle, second: Particle, stiffness: number, distance?: number);
setCollidesWithColliders(collidesWithColliders: boolean): this; setCollidesWithColliders(collidesWithColliders: boolean): this;
handleCollisions(collidersWithLayers: any): void;
solve(): void; solve(): void;
debugRender(graphics: egret.Graphics): void; debugRender(graphics: egret.Graphics): void;
} }

View File

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

File diff suppressed because one or more lines are too long

View File

@@ -46,6 +46,10 @@ class Rectangle {
this.top < value.bottom; this.top < value.bottom;
} }
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number){
}
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
rotation: number, width: number, height: number) { rotation: number, width: number, height: number) {
if (rotation == 0) { if (rotation == 0) {

View File

@@ -0,0 +1,40 @@
///<reference path="./Shape.ts" />
class Polygon extends Shape {
public points: Vector2[];
constructor(vertCount: number, radius: number){
super();
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
}
public setPoints(points: Vector2[]){
this.points = points;
this.recalculateCenterAndEdgeNormals();
}
public recalculateCenterAndEdgeNormals(){
}
public static findPolygonCenter(points: Vector2[]){
let x = 0, y = 0;
for (let i = 0; i < points.length; i++){
x += points[i].x;
y += points[i].y;
}
return new Vector2(x / points.length, y / points.length);
}
public static buildSymmertricalPolygon(vertCount: number, radius: number){
let verts = new Vector2[vertCount];
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);
}
return verts;
}
}

View File

@@ -0,0 +1,3 @@
abstract class Shape {
public bounds: Rectangle;
}

View File

@@ -5,6 +5,8 @@ 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;
/** /**
* 处理解决所有约束条件 * 处理解决所有约束条件
*/ */
@@ -48,6 +50,13 @@ class Composite {
} }
} }
public handleConstraintCollisions(){
for (let i = this._constraints.length - 1; i >= 0; i --){
if (this._constraints[i].collidesWithColliders)
this._constraints[i].handleCollisions(this.collidesWithLayers);
}
}
public debugRender(graphics: egret.Graphics){ public debugRender(graphics: egret.Graphics){
if (this.drawConstraints){ if (this.drawConstraints){
for (let i = 0; i < this._constraints.length; i ++){ for (let i = 0; i < this._constraints.length; i ++){

View File

@@ -4,5 +4,9 @@ abstract class Constraint {
public abstract solve(); public abstract solve();
public handleCollisions(collidesWithLayers: number){
}
public debugRender(graphics: egret.Graphics) {} public debugRender(graphics: egret.Graphics) {}
} }

View File

@@ -1,3 +1,4 @@
///<reference path="../../Shapes/Polygon.ts"/>
class DistanceConstraint extends Constraint { class DistanceConstraint extends Constraint {
public stiffness: number = 0; public stiffness: number = 0;
public restingDistance: number = 0; public restingDistance: number = 0;
@@ -5,6 +6,7 @@ class DistanceConstraint extends Constraint {
private _particleOne: Particle; private _particleOne: Particle;
private _particleTwo: Particle; private _particleTwo: Particle;
private static _polygon = new Polygon(2, 1);
constructor(first: Particle, second: Particle, stiffness: number, distance = -1){ constructor(first: Particle, second: Particle, stiffness: number, distance = -1){
super(); super();
@@ -25,6 +27,14 @@ class DistanceConstraint extends Constraint {
return this; return this;
} }
public handleCollisions(collidersWithLayers){
let minX = Math.min(this._particleOne.position.x, this._particleTwo.position.x);
let maxX = Math.max(this._particleOne.position.x, this._particleTwo.position.x);
let minY = Math.min(this._particleOne.position.y, this._particleTwo.position.y);
let maxY = Math.max(this._particleOne.position.y, this._particleTwo.position.y);
// DistanceConstraint._polygon.bounds = Rectangle.
}
public solve() { public solve() {
let diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position); let diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position);
let d = diff.length(); let d = diff.length();

View File

@@ -1,5 +1,5 @@
/** /**
* 基于verlet 物理引擎进行改造的物理引擎 ts重写 * 基于verlet进行改造的物理引擎 ts重写
* https://github.com/subprotocol/verlet-js * https://github.com/subprotocol/verlet-js
*/ */
class VerletWorld { class VerletWorld {
@@ -34,6 +34,8 @@ class VerletWorld {
composite.updateParticles(this._fixedDeltaTimeSq, this.gravity); composite.updateParticles(this._fixedDeltaTimeSq, this.gravity);
composite.handleConstraintCollisions();
for (let j = 0; j < composite.particles.length; j ++){ for (let j = 0; j < composite.particles.length; j ++){
let p = composite.particles[j]; let p = composite.particles[j];