From 533a4c2e30762972406bab6fb1faac1b517cfbd6 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 14 Jun 2013 17:55:07 +0100 Subject: [PATCH] Commiting before a small refactor of Shapes and Joints --- Phaser/Phaser.csproj | 24 +- Phaser/physics/advanced/Body.ts | 12 +- Phaser/physics/advanced/Bounds.ts | 4 - Phaser/physics/advanced/Collision.ts | 4 +- Phaser/physics/advanced/Contact.ts | 2 +- Phaser/physics/advanced/ContactSolver.ts | 12 +- Phaser/physics/advanced/Manager.ts | 2 +- Phaser/physics/advanced/ShapePoly.ts | 8 +- Phaser/physics/advanced/Space.ts | 8 +- Phaser/physics/advanced/{ => joints}/Joint.ts | 10 +- Phaser/physics/advanced/shapes/Box.ts | 39 ++ Phaser/physics/advanced/shapes/Circle.ts | 101 ++++ .../physics/advanced/{ => shapes}/IShape.ts | 10 +- Phaser/physics/advanced/shapes/Poly.ts | 295 ++++++++++ Phaser/physics/advanced/shapes/Segment.ts | 205 +++++++ Phaser/physics/advanced/{ => shapes}/Shape.ts | 12 +- Phaser/physics/advanced/shapes/Triangle.ts | 25 + Tests/phaser.js | 36 +- Tests/physics/body1.js | 62 +- Tests/physics/body1.ts | 90 ++- build/phaser.d.ts | 110 +++- build/phaser.js | 557 ++++++++++++++++-- 22 files changed, 1499 insertions(+), 129 deletions(-) rename Phaser/physics/advanced/{ => joints}/Joint.ts (86%) create mode 100644 Phaser/physics/advanced/shapes/Box.ts create mode 100644 Phaser/physics/advanced/shapes/Circle.ts rename Phaser/physics/advanced/{ => shapes}/IShape.ts (75%) create mode 100644 Phaser/physics/advanced/shapes/Poly.ts create mode 100644 Phaser/physics/advanced/shapes/Segment.ts rename Phaser/physics/advanced/{ => shapes}/Shape.ts (79%) create mode 100644 Phaser/physics/advanced/shapes/Triangle.ts diff --git a/Phaser/Phaser.csproj b/Phaser/Phaser.csproj index 357dabaa8..2de59ef55 100644 --- a/Phaser/Phaser.csproj +++ b/Phaser/Phaser.csproj @@ -182,7 +182,6 @@ Body.ts - Bounds.ts @@ -199,25 +198,18 @@ ContactSolver.ts - - - IShape.ts - - - Joint.ts - IJoint.ts + + + Joint.ts + Manager.ts - - - Shape.ts - @@ -235,6 +227,14 @@ ShapeSegment.ts + + + + IShape.ts + + + Shape.ts + ShapeTriangle.ts diff --git a/Phaser/physics/advanced/Body.ts b/Phaser/physics/advanced/Body.ts index 1d97fe7df..a6cdf4c1f 100644 --- a/Phaser/physics/advanced/Body.ts +++ b/Phaser/physics/advanced/Body.ts @@ -4,10 +4,10 @@ /// /// /// -/// +/// /// /// -/// +/// /** * Phaser - Advanced Physics - Body @@ -147,7 +147,7 @@ module Phaser.Physics.Advanced { public duplicate() { - console.log('body duplicate called'); + //console.log('body duplicate called'); //var body = new Body(this.type, this.transform.t, this.angle); @@ -314,7 +314,7 @@ module Phaser.Physics.Advanced { var mass = shape.area() * shape.density; var inertia = shape.inertia(mass); - console.log('rmd', centroid, shape); + //console.log('rmd', centroid, shape); totalMassCentroid.multiplyAddByScalar(centroid, mass); totalMass += mass; @@ -368,7 +368,7 @@ module Phaser.Physics.Advanced { public cacheData() { - console.log('Body cacheData', this.name, 'len', this.shapes.length); + //console.log('Body cacheData', this.name, 'len', this.shapes.length); this.bounds.clear(); @@ -501,7 +501,7 @@ module Phaser.Physics.Advanced { } - public applyAngularImpulse(impulse) { + public applyAngularImpulse(impulse: number) { if (this.isDynamic == false) { diff --git a/Phaser/physics/advanced/Bounds.ts b/Phaser/physics/advanced/Bounds.ts index d73f3bc84..6e7ce2857 100644 --- a/Phaser/physics/advanced/Bounds.ts +++ b/Phaser/physics/advanced/Bounds.ts @@ -157,10 +157,6 @@ module Phaser.Physics.Advanced { return false; } - console.log('intersects TRUE'); - console.log(this); - console.log(b); - return true; } diff --git a/Phaser/physics/advanced/Collision.ts b/Phaser/physics/advanced/Collision.ts index 4393a4f48..333fa65a5 100644 --- a/Phaser/physics/advanced/Collision.ts +++ b/Phaser/physics/advanced/Collision.ts @@ -3,7 +3,7 @@ /// /// /// -/// +/// /// /// @@ -22,7 +22,7 @@ module Phaser.Physics.Advanced { public collide(a, b, contacts: Contact[]) { - console.log('collide', a.type, b.type); + //console.log('collide', a.type, b.type); // Circle (a is the circle) if (a.type == Manager.SHAPE_TYPE_CIRCLE) diff --git a/Phaser/physics/advanced/Contact.ts b/Phaser/physics/advanced/Contact.ts index d97e510ab..bf3855316 100644 --- a/Phaser/physics/advanced/Contact.ts +++ b/Phaser/physics/advanced/Contact.ts @@ -3,7 +3,7 @@ /// /// /// -/// +/// /** * Phaser - Advanced Physics - Contact diff --git a/Phaser/physics/advanced/ContactSolver.ts b/Phaser/physics/advanced/ContactSolver.ts index 62169ce47..965919a74 100644 --- a/Phaser/physics/advanced/ContactSolver.ts +++ b/Phaser/physics/advanced/ContactSolver.ts @@ -3,7 +3,7 @@ /// /// /// -/// +/// /// /** @@ -38,7 +38,7 @@ module Phaser.Physics.Advanced { constructor(shape1, shape2) { - console.log('ContactSolver super'); + //console.log('ContactSolver super'); this.shape1 = shape1; this.shape2 = shape2; @@ -99,8 +99,8 @@ module Phaser.Physics.Advanced { { var con: Contact = this.contacts[i]; - console.log('initSolver con'); - console.log(con); + //console.log('initSolver con'); + //console.log(con); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); @@ -149,10 +149,6 @@ module Phaser.Physics.Advanced { // bounce velocity dot n con.bounce = Phaser.Vec2Utils.dot(rv, con.normal) * this.elasticity; - console.log('bounce?', Phaser.Vec2Utils.dot(rv, con.normal), this.elasticity); - - console.log('con over'); - console.log(con); } } diff --git a/Phaser/physics/advanced/Manager.ts b/Phaser/physics/advanced/Manager.ts index d7749e61d..a83b73353 100644 --- a/Phaser/physics/advanced/Manager.ts +++ b/Phaser/physics/advanced/Manager.ts @@ -1,6 +1,6 @@ /// /// -/// +/// /** * Phaser - Advanced Physics Manager diff --git a/Phaser/physics/advanced/ShapePoly.ts b/Phaser/physics/advanced/ShapePoly.ts index 1202c64f4..c77d12963 100644 --- a/Phaser/physics/advanced/ShapePoly.ts +++ b/Phaser/physics/advanced/ShapePoly.ts @@ -31,7 +31,7 @@ module Phaser.Physics.Advanced { { for (var i = 0; i < verts.length; i++) { - console.log('cloning vert', i); + //console.log('cloning vert', i); this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; @@ -41,11 +41,11 @@ module Phaser.Physics.Advanced { } } - console.log('ShapePoly finished', this.verts); + //console.log('ShapePoly finished', this.verts); this.finishVerts(); - console.log('ShapePoly finished 2', this.verts); + //console.log('ShapePoly finished 2', this.verts); } @@ -146,7 +146,7 @@ module Phaser.Physics.Advanced { var numVerts = this.verts.length; - console.log('shapePoly cacheData', numVerts); + //console.log('shapePoly cacheData', numVerts); if (numVerts == 0) { diff --git a/Phaser/physics/advanced/Space.ts b/Phaser/physics/advanced/Space.ts index 97e8a43c9..cafd97982 100644 --- a/Phaser/physics/advanced/Space.ts +++ b/Phaser/physics/advanced/Space.ts @@ -3,7 +3,7 @@ /// /// /// -/// +/// /// /// /// @@ -85,7 +85,7 @@ module Phaser.Physics.Advanced { return; } - console.log('Body added to space', body.name); + //console.log('Body added to space', body.name); var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; @@ -449,7 +449,7 @@ module Phaser.Physics.Advanced { public genTemporalContactSolvers() { - console.log('genTemporalContactSolvers'); + //console.log('genTemporalContactSolvers'); //var t0 = Date.now(); @@ -510,7 +510,7 @@ module Phaser.Physics.Advanced { continue; } - console.log('>>>>>>>>>> intersects'); + //console.log('>>>>>>>>>> intersects'); for (var i = 0; i < body1.shapes.length; i++) { diff --git a/Phaser/physics/advanced/Joint.ts b/Phaser/physics/advanced/joints/Joint.ts similarity index 86% rename from Phaser/physics/advanced/Joint.ts rename to Phaser/physics/advanced/joints/Joint.ts index 8342eb9b3..6a64ae79a 100644 --- a/Phaser/physics/advanced/Joint.ts +++ b/Phaser/physics/advanced/joints/Joint.ts @@ -1,8 +1,8 @@ -/// -/// -/// -/// -/// +/// +/// +/// +/// +/// /** * Phaser - Advanced Physics - Joint diff --git a/Phaser/physics/advanced/shapes/Box.ts b/Phaser/physics/advanced/shapes/Box.ts new file mode 100644 index 000000000..c9ade9b1f --- /dev/null +++ b/Phaser/physics/advanced/shapes/Box.ts @@ -0,0 +1,39 @@ +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - Shapes - Box +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced.Shapes { + + export class Box extends Phaser.Physics.Advanced.ShapePoly { + + // Give in pixels + constructor(x, y, width, height) { + + x = Manager.pixelsToMeters(x); + y = Manager.pixelsToMeters(y); + width = Manager.pixelsToMeters(width); + height = Manager.pixelsToMeters(height); + + var hw = width * 0.5; + var hh = height * 0.5; + + super([ + new Phaser.Vec2(-hw + x, +hh + y), + new Phaser.Vec2(-hw + x, -hh + y), + new Phaser.Vec2(+hw + x, -hh + y), + new Phaser.Vec2(+hw + x, +hh + y) + ]); + + } + + } + +} diff --git a/Phaser/physics/advanced/shapes/Circle.ts b/Phaser/physics/advanced/shapes/Circle.ts new file mode 100644 index 000000000..408c60b1a --- /dev/null +++ b/Phaser/physics/advanced/shapes/Circle.ts @@ -0,0 +1,101 @@ +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - Shape - Circle +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced.Shapes { + + export class Circle extends Phaser.Physics.Advanced.Shape implements IShape { + + constructor(radius: number, x?: number = 0, y?: number = 0) { + + super(Manager.SHAPE_TYPE_CIRCLE); + + this.center = new Phaser.Vec2(x, y); + this.radius = radius; + this.tc = new Phaser.Vec2; + + this.finishVerts(); + + } + + public radius: number; + public center: Phaser.Vec2; + public tc: Phaser.Vec2; + + public finishVerts() { + this.radius = Math.abs(this.radius); + } + + public duplicate() { + return new ShapeCircle(this.center.x, this.center.y, this.radius); + } + + public recenter(c:Phaser.Vec2) { + this.center.subtract(c); + } + + public transform(xf: Transform) { + + Phaser.TransformUtils.transform(xf, this.center, this.center); + //this.center = xf.transform(this.center); + } + + public untransform(xf: Transform) { + Phaser.TransformUtils.untransform(xf, this.center, this.center); + //this.center = xf.untransform(this.center); + } + + public area(): number { + return Manager.areaForCircle(this.radius, 0); + } + + public centroid(): Phaser.Vec2 { + return Phaser.Vec2Utils.clone(this.center); + } + + public inertia(mass: number): number { + return Manager.inertiaForCircle(mass, this.center, this.radius, 0); + } + + public cacheData(xf: Transform) { + + Phaser.TransformUtils.transform(xf, this.center, this.tc); + //this.tc = xf.transform(this.center); + + this.bounds.mins.setTo(this.tc.x - this.radius, this.tc.y - this.radius); + this.bounds.maxs.setTo(this.tc.x + this.radius, this.tc.y + this.radius); + + } + + public pointQuery(p:Phaser.Vec2): bool { + //return vec2.distsq(this.tc, p) < (this.r * this.r); + return Phaser.Vec2Utils.distanceSq(this.tc, p) < (this.radius * this.radius); + } + + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { + + var dsq = minDist * minDist; + + if (Phaser.Vec2Utils.distanceSq(this.tc, p) < dsq) + { + return 0; + } + + return -1; + } + + public distanceOnPlane(n, d) { + Phaser.Vec2Utils.dot(n, this.tc) - this.radius - d; + } + + } + +} \ No newline at end of file diff --git a/Phaser/physics/advanced/IShape.ts b/Phaser/physics/advanced/shapes/IShape.ts similarity index 75% rename from Phaser/physics/advanced/IShape.ts rename to Phaser/physics/advanced/shapes/IShape.ts index 06fa8c169..1fe6ef32e 100644 --- a/Phaser/physics/advanced/IShape.ts +++ b/Phaser/physics/advanced/shapes/IShape.ts @@ -1,8 +1,8 @@ -/// -/// -/// -/// -/// +/// +/// +/// +/// +/// /// /** diff --git a/Phaser/physics/advanced/shapes/Poly.ts b/Phaser/physics/advanced/shapes/Poly.ts new file mode 100644 index 000000000..fb40b49fe --- /dev/null +++ b/Phaser/physics/advanced/shapes/Poly.ts @@ -0,0 +1,295 @@ +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - Shapes - Convex Polygon +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced.Shapes { + + export class Poly extends Phaser.Physics.Advanced.Shape implements IShape { + + constructor(verts?:Phaser.Vec2[]) { + + super(Manager.SHAPE_TYPE_POLY); + + this.verts = []; + this.planes = []; + + this.tverts = []; + this.tplanes = []; + + if (verts) + { + for (var i = 0; i < verts.length; i++) + { + this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); + this.tverts[i] = this.verts[i]; + + this.tplanes[i] = {}; + this.tplanes[i].n = new Phaser.Vec2; + this.tplanes[i].d = 0; + } + } + + this.finishVerts(); + + } + + public verts: Phaser.Vec2[]; + public planes; + + public tverts; + public tplanes; + + public convexity: bool; + + public finishVerts() { + + if (this.verts.length < 2) + { + this.convexity = false; + this.planes = []; + return; + } + + this.convexity = true; + this.tverts = []; + this.tplanes = []; + + // Must be counter-clockwise verts + for (var i = 0; i < this.verts.length; i++) + { + var a = this.verts[i]; + var b = this.verts[(i + 1) % this.verts.length]; + var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b))); + + this.planes[i] = {}; + this.planes[i].n = n; + this.planes[i].d = Phaser.Vec2Utils.dot(n, a); + + this.tverts[i] = this.verts[i]; + + this.tplanes[i] = {}; + this.tplanes[i].n = new Phaser.Vec2; + this.tplanes[i].d = 0; + } + + for (var i = 0; i < this.verts.length; i++) + { + var b = this.verts[(i + 2) % this.verts.length]; + var n = this.planes[i].n; + var d = this.planes[i].d; + + if (Phaser.Vec2Utils.dot(n, b) - d > 0) + { + this.convexity = false; + } + } + } + + public duplicate() { + return new ShapePoly(this.verts); + } + + public recenter(c) { + + for (var i = 0; i < this.verts.length; i++) + { + this.verts[i].subtract(c); + } + + } + + public transform(xf) { + for (var i = 0; i < this.verts.length; i++) + { + this.verts[i] = xf.transform(this.verts[i]); + } + } + + public untransform(xf) { + for (var i = 0; i < this.verts.length; i++) + { + this.verts[i] = xf.untransform(this.verts[i]); + } + } + + public area(): number { + return Manager.areaForPoly(this.verts); + } + + public centroid(): Phaser.Vec2 { + return Manager.centroidForPoly(this.verts); + } + + public inertia(mass: number): number { + return Manager.inertiaForPoly(mass, this.verts, new Phaser.Vec2); + } + + public cacheData(xf:Transform) { + + this.bounds.clear(); + + var numVerts = this.verts.length; + + //console.log('shapePoly cacheData', numVerts); + + if (numVerts == 0) + { + return; + } + + for (var i = 0; i < numVerts; i++) + { + Phaser.TransformUtils.transform(xf, this.tverts[i], this.tverts[i]); + //this.tverts[i] = xf.transform(this.verts[i]); + } + + if (numVerts < 2) + { + this.bounds.addPoint(this.tverts[0]); + return; + } + + for (var i = 0; i < numVerts; i++) + { + var a = this.tverts[i]; + var b = this.tverts[(i + 1) % numVerts]; + + var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b))); + + this.tplanes[i].n = n; + this.tplanes[i].d = Phaser.Vec2Utils.dot(n, a); + + this.bounds.addPoint(a); + } + + } + + public pointQuery(p: Phaser.Vec2): bool { + + if (!this.bounds.containPoint(p)) + { + return false; + } + + return this.containPoint(p); + } + + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { + + var dsq = minDist * minDist; + + for (var i = 0; i < this.tverts.length; i++) + { + if (Phaser.Vec2Utils.distanceSq(this.tverts[i], p) < dsq) + { + return i; + } + } + + return -1; + } + + public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number { + + var dsq = minDist * minDist; + var numVerts = this.tverts.length; + + for (var i = 0; i < this.tverts.length; i++) + { + var v1 = this.tverts[i]; + var v2 = this.tverts[(i + 1) % numVerts]; + var n = this.tplanes[i].n; + + var dtv1 = Phaser.Vec2Utils.cross(v1, n); + var dtv2 = Phaser.Vec2Utils.cross(v2, n); + var dt = Phaser.Vec2Utils.cross(p, n); + + if (dt > dtv1) + { + if (Phaser.Vec2Utils.distanceSq(v1, p) < dsq) + { + return i; + } + } + else if (dt < dtv2) + { + if (Phaser.Vec2Utils.distanceSq(v2, p) < dsq) + { + return i; + } + } + else + { + var dist = Phaser.Vec2Utils.dot(n, p) - Phaser.Vec2Utils.dot(n, v1); + + if (dist * dist < dsq) + { + return i; + } + } + } + + return -1; + + } + + public distanceOnPlane(n, d) { + + var min = 999999; + + for (var i = 0; i < this.verts.length; i++) + { + min = Math.min(min, Phaser.Vec2Utils.dot(n, this.tverts[i])); + } + + return min - d; + + } + + public containPoint(p) { + + for (var i = 0; i < this.verts.length; i++) + { + var plane = this.tplanes[i]; + + if (Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0) + { + return false; + } + } + + return true; + + } + + public containPointPartial(p, n) { + + for (var i = 0; i < this.verts.length; i++) + { + var plane = this.tplanes[i]; + + if (Phaser.Vec2Utils.dot(plane.n, n) < 0.0001) + { + continue; + } + + if (Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0) + { + return false; + } + } + + return true; + } + + } + +} \ No newline at end of file diff --git a/Phaser/physics/advanced/shapes/Segment.ts b/Phaser/physics/advanced/shapes/Segment.ts new file mode 100644 index 000000000..82922742b --- /dev/null +++ b/Phaser/physics/advanced/shapes/Segment.ts @@ -0,0 +1,205 @@ +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - Shapes - Segment +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced.Shapes { + + export class Segment extends Phaser.Physics.Advanced.Shape implements IShape { + + constructor(a, b, radius: number) { + + super(Manager.SHAPE_TYPE_SEGMENT); + + // What types are A and B??! + this.a = a.duplicate(); + this.b = b.duplicate(); + this.radius = radius; + + this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(b, a)); + this.normal.normalize(); + + this.ta = new Phaser.Vec2; + this.tb = new Phaser.Vec2; + this.tn = new Phaser.Vec2; + + this.finishVerts(); + + } + + public a: Phaser.Vec2; + public b: Phaser.Vec2; + public radius: number; + + public normal: Phaser.Vec2; + public ta: Phaser.Vec2; + public tb: Phaser.Vec2; + public tn: Phaser.Vec2; + + public finishVerts() { + + this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.b, this.a)); + this.normal.normalize(); + + this.radius = Math.abs(this.radius); + + } + + public duplicate() { + return new ShapeSegment(this.a, this.b, this.radius); + } + + public recenter(c) { + this.a.subtract(c); + this.b.subtract(c); + } + + public transform(xf:Transform) { + + Phaser.TransformUtils.transform(xf, this.a, this.a); + Phaser.TransformUtils.transform(xf, this.b, this.b); + + //this.a = xf.transform(this.a); + //this.b = xf.transform(this.b); + + } + + public untransform(xf:Transform) { + + Phaser.TransformUtils.untransform(xf, this.a, this.a); + Phaser.TransformUtils.untransform(xf, this.b, this.b); + + //this.a = xf.untransform(this.a); + //this.b = xf.untransform(this.b); + + } + + public area(): number { + return Manager.areaForSegment(this.a, this.b, this.radius); + } + + public centroid(): Phaser.Vec2 { + return Manager.centroidForSegment(this.a, this.b); + } + + public inertia(mass: number): number { + return Manager.inertiaForSegment(mass, this.a, this.b); + } + + public cacheData(xf:Transform) { + + Phaser.TransformUtils.transform(xf, this.a, this.ta); + Phaser.TransformUtils.transform(xf, this.b, this.tb); + + //this.ta = xf.transform(this.a); + //this.tb = xf.transform(this.b); + + this.tn = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.tb, this.ta)).normalize(); + + var l; + var r; + var t; + var b; + + if (this.ta.x < this.tb.x) + { + l = this.ta.x; + r = this.tb.x; + } + else + { + l = this.tb.x; + r = this.ta.x; + } + + if (this.ta.y < this.tb.y) + { + b = this.ta.y; + t = this.tb.y; + } else + { + b = this.tb.y; + t = this.ta.y; + } + + this.bounds.mins.setTo(l - this.radius, b - this.radius); + this.bounds.maxs.setTo(r + this.radius, t + this.radius); + + } + + public pointQuery(p: Phaser.Vec2): bool { + + if (!this.bounds.containPoint(p)) + { + return false; + } + + var dn = Phaser.Vec2Utils.dot(this.tn, p) - Phaser.Vec2Utils.dot(this.ta, this.tn); + var dist = Math.abs(dn); + + if (dist > this.radius) + { + return false; + } + + var dt = Phaser.Vec2Utils.cross(p, this.tn); + var dta = Phaser.Vec2Utils.cross(this.ta, this.tn); + var dtb = Phaser.Vec2Utils.cross(this.tb, this.tn); + + if (dt <= dta) + { + if (dt < dta - this.radius) + { + return false; + } + + return Phaser.Vec2Utils.distanceSq(this.ta, p) < (this.radius * this.radius); + } + else if (dt > dtb) + { + if (dt > dtb + this.radius) + { + return false; + } + + return Phaser.Vec2Utils.distanceSq(this.tb, p) < (this.radius * this.radius); + } + + return true; + } + + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { + + var dsq = minDist * minDist; + + if (Phaser.Vec2Utils.distanceSq(this.ta, p) < dsq) + { + return 0; + } + + if (Phaser.Vec2Utils.distanceSq(this.tb, p) < dsq) + { + return 1; + } + + return -1; + } + + public distanceOnPlane(n, d) { + + var a = Phaser.Vec2Utils.dot(n, this.ta) - this.radius; + var b = Phaser.Vec2Utils.dot(n, this.tb) - this.radius; + + return Math.min(a, b) - d; + } + + } + +} \ No newline at end of file diff --git a/Phaser/physics/advanced/Shape.ts b/Phaser/physics/advanced/shapes/Shape.ts similarity index 79% rename from Phaser/physics/advanced/Shape.ts rename to Phaser/physics/advanced/shapes/Shape.ts index 657fdf96f..7f2e60a1b 100644 --- a/Phaser/physics/advanced/Shape.ts +++ b/Phaser/physics/advanced/shapes/Shape.ts @@ -1,9 +1,9 @@ -/// -/// -/// -/// -/// -/// +/// +/// +/// +/// +/// +/// /// /** diff --git a/Phaser/physics/advanced/shapes/Triangle.ts b/Phaser/physics/advanced/shapes/Triangle.ts new file mode 100644 index 000000000..0ee3c16f6 --- /dev/null +++ b/Phaser/physics/advanced/shapes/Triangle.ts @@ -0,0 +1,25 @@ +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - Shapes - Triangle +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced { + + export class ShapeTriangle extends Phaser.Physics.Advanced.Shapes.Poly { + + constructor(p1, p2, p3) { + + super( [ new Phaser.Vec2(p1.x, p1.y), new Phaser.Vec2(p2.x, p2.y), new Phaser.Vec2(p3.x, p3.y) ] ); + + } + + } + +} diff --git a/Tests/phaser.js b/Tests/phaser.js index 440ad3dce..0378d4db3 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -19479,9 +19479,6 @@ var Phaser; if(this.mins.x > b.maxs.x || this.maxs.x < b.mins.x || this.mins.y > b.maxs.y || this.maxs.y < b.mins.y) { return false; } - console.log('intersects TRUE'); - console.log(this); - console.log(b); return true; }; Bounds.expand = function expand(b, ax, ay) { @@ -19613,7 +19610,7 @@ var Phaser; (function (Advanced) { var ContactSolver = (function () { function ContactSolver(shape1, shape2) { - console.log('ContactSolver super'); + //console.log('ContactSolver super'); this.shape1 = shape1; this.shape2 = shape2; this.contacts = []; @@ -19643,8 +19640,8 @@ var Phaser; var sum_m_inv = body1.massInverted + body2.massInverted; for(var i = 0; i < this.contacts.length; i++) { var con = this.contacts[i]; - console.log('initSolver con'); - console.log(con); + //console.log('initSolver con'); + //console.log(con); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); Phaser.Vec2Utils.subtract(con.point, body2.position, con.r2); @@ -19683,9 +19680,6 @@ var Phaser; //var rv = vec2.sub(v2, v1); // bounce velocity dot n con.bounce = Phaser.Vec2Utils.dot(rv, con.normal) * this.elasticity; - console.log('bounce?', Phaser.Vec2Utils.dot(rv, con.normal), this.elasticity); - console.log('con over'); - console.log(con); } }; ContactSolver.prototype.warmStart = function () { @@ -19933,7 +19927,7 @@ var Phaser; function Collision() { } Collision.prototype.collide = function (a, b, contacts) { - console.log('collide', a.type, b.type); + //console.log('collide', a.type, b.type); // Circle (a is the circle) if(a.type == Advanced.Manager.SHAPE_TYPE_CIRCLE) { if(b.type == Advanced.Manager.SHAPE_TYPE_CIRCLE) { @@ -20351,7 +20345,7 @@ var Phaser; if(this.bodyHash[body.id] != undefined) { return; } - console.log('Body added to space', body.name); + //console.log('Body added to space', body.name); var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; body.awake(true); @@ -20570,7 +20564,7 @@ var Phaser; return null; }; Space.prototype.genTemporalContactSolvers = function () { - console.log('genTemporalContactSolvers'); + //console.log('genTemporalContactSolvers'); //var t0 = Date.now(); var newContactSolverArr = []; this.numContacts = 0; @@ -20604,7 +20598,7 @@ var Phaser; if(!body1.bounds.intersectsBounds(body2.bounds)) { continue; } - console.log('>>>>>>>>>> intersects'); + //console.log('>>>>>>>>>> intersects'); for(var i = 0; i < body1.shapes.length; i++) { for(var j = 0; j < body2.shapes.length; j++) { var shape1 = body1.shapes[i]; @@ -20890,7 +20884,7 @@ var Phaser; this.stepCount = 0; } Body.prototype.duplicate = function () { - console.log('body duplicate called'); + //console.log('body duplicate called'); //var body = new Body(this.type, this.transform.t, this.angle); //for (var i = 0; i < this.shapes.length; i++) //{ @@ -21015,7 +21009,7 @@ var Phaser; var centroid = shape.centroid(); var mass = shape.area() * shape.density; var inertia = shape.inertia(mass); - console.log('rmd', centroid, shape); + //console.log('rmd', centroid, shape); totalMassCentroid.multiplyAddByScalar(centroid, mass); totalMass += mass; totalInertia += inertia; @@ -21050,7 +21044,7 @@ var Phaser; } }; Body.prototype.cacheData = function () { - console.log('Body cacheData', this.name, 'len', this.shapes.length); + //console.log('Body cacheData', this.name, 'len', this.shapes.length); this.bounds.clear(); for(var i = 0; i < this.shapes.length; i++) { var shape = this.shapes[i]; @@ -21218,7 +21212,7 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - console.log('cloning vert', i); + //console.log('cloning vert', i); this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; this.tplanes[i] = { @@ -21227,10 +21221,10 @@ var Phaser; this.tplanes[i].d = 0; } } - console.log('ShapePoly finished', this.verts); + //console.log('ShapePoly finished', this.verts); this.finishVerts(); - console.log('ShapePoly finished 2', this.verts); - } + //console.log('ShapePoly finished 2', this.verts); + } ShapePoly.prototype.finishVerts = function () { if(this.verts.length < 2) { this.convexity = false; @@ -21294,7 +21288,7 @@ var Phaser; ShapePoly.prototype.cacheData = function (xf) { this.bounds.clear(); var numVerts = this.verts.length; - console.log('shapePoly cacheData', numVerts); + //console.log('shapePoly cacheData', numVerts); if(numVerts == 0) { return; } diff --git a/Tests/physics/body1.js b/Tests/physics/body1.js index 07880cdca..0ad893bdc 100644 --- a/Tests/physics/body1.js +++ b/Tests/physics/body1.js @@ -5,22 +5,27 @@ (function () { var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render); function init() { - game.load.image('atari', 'assets/sprites/atari800xl.png'); + game.load.image('xatari', 'assets/sprites/atari800xl.png'); game.load.image('card', 'assets/sprites/mana_card.png'); + game.load.image('atari', 'assets/sprites/shinyball.png'); game.load.start(); } var atari; var card; var physics; var circle; + var walls; var ground; function create() { atari = game.add.sprite(200, 100, 'atari'); + atari.transform.origin.setTo(0.5, 0.5); //card = game.add.sprite(500, 300, 'card'); physics = new Phaser.Physics.Advanced.Manager(game); - var walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); + walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); walls.game = game; - ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 500, 800, 20)); + // position is in relation to the containing body! don't forget this + ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(400, 500, 500, 20)); + ground.friction = 10; //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 0.2, 20.48, 0.4)); //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 15.16, 20.48, 0.4)); //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(-10.04, 7.68, 0.4, 14.56)); @@ -31,8 +36,8 @@ circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, physics.pixelsToMeters(300), physics.pixelsToMeters(200)); circle.game = game; var shape = new Phaser.Physics.Advanced.ShapeCircle(0.4, 0, 0); - shape.elasticity = 0.5; - shape.friction = 1.0; + shape.elasticity = 0.8; + shape.friction = 10.0; shape.density = 1; circle.addShape(shape); circle.resetMassData(); @@ -42,12 +47,59 @@ physics.update(); atari.x = physics.metersToPixels(circle.position.x); atari.y = physics.metersToPixels(circle.position.y); + atari.rotation = physics.metersToPixels(circle.angle); + // force moves without rotating + if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { + circle.applyAngularImpulse(-0.02); + } else if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { + circle.applyAngularImpulse(0.02); + } + /* + if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) + { + circle.applyForceToCenter(new Phaser.Vec2(-8, 0)); + } + else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) + { + circle.applyForceToCenter(new Phaser.Vec2(8, 0)); + } + */ + if(game.input.keyboard.isDown(Phaser.Keyboard.UP)) { + circle.applyForceToCenter(new Phaser.Vec2(0, -10)); + } else if(game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { + circle.applyForceToCenter(new Phaser.Vec2(0, 5)); + } //console.log(circle.velocity.x, circle.velocity.y); //console.log('p', circle.position.x, circle.position.y); } + function renderCircle(shape) { + game.stage.context.beginPath(); + game.stage.context.arc(shape.tc.x * 50, shape.tc.y * 50, shape.radius * 50, 0, Math.PI * 2, false); + if(shape.body.isAwake) { + game.stage.context.fillStyle = 'rgba(0,255,0, 0.3)'; + } else { + game.stage.context.fillStyle = 'rgba(100,100,100, 0.1)'; + } + game.stage.context.fill(); + game.stage.context.closePath(); + } + function drawPolygon(ctx, shape, lineWidth, fillStyle) { + var verts = shape.verts; + ctx.beginPath(); + ctx.moveTo(verts[0].x * 50, verts[0].y * 50); + for(var i = 0; i < verts.length; i++) { + ctx.lineTo(verts[i].x * 50, verts[i].y * 50); + } + ctx.lineTo(verts[verts.length - 1].x * 50, verts[verts.length - 1].y * 50); + ctx.closePath(); + ctx.fillStyle = fillStyle; + ctx.fill(); + } function render() { game.stage.context.fillStyle = 'rgb(255,255,0)'; game.stage.context.fillText('x: ' + circle.position.x + ' y: ' + circle.position.y, 32, 32); game.stage.context.fillText('vx: ' + circle.velocity.x + ' vy: ' + circle.velocity.y, 32, 64); + renderCircle(circle.shapes[0]); + drawPolygon(game.stage.context, walls.shapes[0], 1, 'rgb(0,255,255)'); } })(); diff --git a/Tests/physics/body1.ts b/Tests/physics/body1.ts index e1be8c4bd..ee39c9ef4 100644 --- a/Tests/physics/body1.ts +++ b/Tests/physics/body1.ts @@ -9,8 +9,9 @@ function init() { - game.load.image('atari', 'assets/sprites/atari800xl.png'); + game.load.image('xatari', 'assets/sprites/atari800xl.png'); game.load.image('card', 'assets/sprites/mana_card.png'); + game.load.image('atari', 'assets/sprites/shinyball.png'); game.load.start(); } @@ -19,21 +20,24 @@ var card: Phaser.Sprite; var physics: Phaser.Physics.Advanced.Manager; var circle: Phaser.Physics.Advanced.Body; + var walls: Phaser.Physics.Advanced.Body; var ground: Phaser.Physics.Advanced.ShapeBox; function create() { atari = game.add.sprite(200, 100, 'atari'); + atari.transform.origin.setTo(0.5, 0.5); //card = game.add.sprite(500, 300, 'card'); physics = new Phaser.Physics.Advanced.Manager(game); - var walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); + walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); walls.game = game; // position is in relation to the containing body! don't forget this - ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 500, 800, 20)); + ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(400, 500, 500, 20)); + ground.friction = 10; //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 0.2, 20.48, 0.4)); //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 15.16, 20.48, 0.4)); @@ -49,8 +53,8 @@ circle.game = game; var shape = new Phaser.Physics.Advanced.ShapeCircle(0.4, 0, 0); - shape.elasticity = 0.5; - shape.friction = 1.0; + shape.elasticity = 0.8; + shape.friction = 10.0; shape.density = 1; circle.addShape(shape); circle.resetMassData(); @@ -65,17 +69,93 @@ atari.x = physics.metersToPixels(circle.position.x); atari.y = physics.metersToPixels(circle.position.y); + atari.rotation = physics.metersToPixels(circle.angle); + + // force moves without rotating + + if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) + { + circle.applyAngularImpulse(-0.02); + } + else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) + { + circle.applyAngularImpulse(0.02); + } + + + /* + if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) + { + circle.applyForceToCenter(new Phaser.Vec2(-8, 0)); + } + else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) + { + circle.applyForceToCenter(new Phaser.Vec2(8, 0)); + } + */ + + if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) + { + circle.applyForceToCenter(new Phaser.Vec2(0, -10)); + } + else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) + { + circle.applyForceToCenter(new Phaser.Vec2(0, 5)); + } //console.log(circle.velocity.x, circle.velocity.y); //console.log('p', circle.position.x, circle.position.y); } + function renderCircle(shape) { + + game.stage.context.beginPath(); + game.stage.context.arc(shape.tc.x * 50, shape.tc.y * 50, shape.radius * 50, 0, Math.PI * 2, false); + + if (shape.body.isAwake) + { + game.stage.context.fillStyle = 'rgba(0,255,0, 0.3)'; + } + else + { + game.stage.context.fillStyle = 'rgba(100,100,100, 0.1)'; + } + + game.stage.context.fill(); + game.stage.context.closePath(); + + } + + function drawPolygon(ctx, shape, lineWidth, fillStyle) { + + var verts = shape.verts; + + ctx.beginPath(); + ctx.moveTo(verts[0].x * 50, verts[0].y * 50); + + for (var i = 0; i < verts.length; i++) { + ctx.lineTo(verts[i].x * 50, verts[i].y * 50); + } + + ctx.lineTo(verts[verts.length - 1].x * 50, verts[verts.length - 1].y * 50); + + ctx.closePath(); + + ctx.fillStyle = fillStyle; + ctx.fill(); + + } + function render() { game.stage.context.fillStyle = 'rgb(255,255,0)'; game.stage.context.fillText('x: ' + circle.position.x + ' y: ' + circle.position.y, 32, 32); game.stage.context.fillText('vx: ' + circle.velocity.x + ' vy: ' + circle.velocity.y, 32, 64); + renderCircle(circle.shapes[0]); + + drawPolygon(game.stage.context, walls.shapes[0], 1, 'rgb(0,255,255)'); + } })(); diff --git a/build/phaser.d.ts b/build/phaser.d.ts index d2228202b..2e09d52ec 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -9964,7 +9964,7 @@ module Phaser.Physics.Advanced { public applyForceToCenter(force): void; public applyTorque(torque): void; public applyLinearImpulse(impulse, p): void; - public applyAngularImpulse(impulse): void; + public applyAngularImpulse(impulse: number): void; public kineticEnergy(): number; public isAwake : bool; public awake(flag): void; @@ -10051,6 +10051,114 @@ module Phaser.Physics.Advanced { } } /** +* Phaser - Advanced Physics - Shapes - Box +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Box extends ShapePoly { + constructor(x, y, width, height); + } +} +/** +* Phaser - Advanced Physics - Shape - Circle +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Circle extends Shape implements IShape { + constructor(radius: number, x?: number, y?: number); + public radius: number; + public center: Vec2; + public tc: Vec2; + public finishVerts(): void; + public duplicate(): ShapeCircle; + public recenter(c: Vec2): void; + public transform(xf: Transform): void; + public untransform(xf: Transform): void; + public area(): number; + public centroid(): Vec2; + public inertia(mass: number): number; + public cacheData(xf: Transform): void; + public pointQuery(p: Vec2): bool; + public findVertexByPoint(p: Vec2, minDist: number): number; + public distanceOnPlane(n, d): void; + } +} +/** +* Phaser - Advanced Physics - Shapes - Convex Polygon +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Poly extends Shape implements IShape { + constructor(verts?: Vec2[]); + public verts: Vec2[]; + public planes; + public tverts; + public tplanes; + public convexity: bool; + public finishVerts(): void; + public duplicate(): ShapePoly; + public recenter(c): void; + public transform(xf): void; + public untransform(xf): void; + public area(): number; + public centroid(): Vec2; + public inertia(mass: number): number; + public cacheData(xf: Transform): void; + public pointQuery(p: Vec2): bool; + public findVertexByPoint(p: Vec2, minDist: number): number; + public findEdgeByPoint(p: Vec2, minDist: number): number; + public distanceOnPlane(n, d): number; + public containPoint(p): bool; + public containPointPartial(p, n): bool; + } +} +/** +* Phaser - Advanced Physics - Shapes - Segment +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Segment extends Shape implements IShape { + constructor(a, b, radius: number); + public a: Vec2; + public b: Vec2; + public radius: number; + public normal: Vec2; + public ta: Vec2; + public tb: Vec2; + public tn: Vec2; + public finishVerts(): void; + public duplicate(): ShapeSegment; + public recenter(c): void; + public transform(xf: Transform): void; + public untransform(xf: Transform): void; + public area(): number; + public centroid(): Vec2; + public inertia(mass: number): number; + public cacheData(xf: Transform): void; + public pointQuery(p: Vec2): bool; + public findVertexByPoint(p: Vec2, minDist: number): number; + public distanceOnPlane(n, d): number; + } +} +interface IPoint { + getDist(): number; +} +module Shapes { + class Point implements IPoint { + public x: number; + public y: number; + constructor(x: number, y: number); + public getDist(): number; + static origin: Point; + } +} +var p: IPoint; +var dist: number; +/** * Phaser - PixelUtils * * A collection of methods useful for manipulating pixels. diff --git a/build/phaser.js b/build/phaser.js index 440ad3dce..3af0bbd29 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -19075,11 +19075,11 @@ var Phaser; var Phaser; (function (Phaser) { (function (Physics) { - /// - /// - /// - /// - /// + /// + /// + /// + /// + /// /** * Phaser - Advanced Physics - Joint * @@ -19121,7 +19121,7 @@ var Phaser; (function (Physics) { /// /// - /// + /// /** * Phaser - Advanced Physics Manager * @@ -19479,9 +19479,6 @@ var Phaser; if(this.mins.x > b.maxs.x || this.maxs.x < b.mins.x || this.mins.y > b.maxs.y || this.maxs.y < b.mins.y) { return false; } - console.log('intersects TRUE'); - console.log(this); - console.log(b); return true; }; Bounds.expand = function expand(b, ax, ay) { @@ -19506,12 +19503,12 @@ var Phaser; var Phaser; (function (Phaser) { (function (Physics) { - /// - /// - /// - /// - /// - /// + /// + /// + /// + /// + /// + /// /// /** * Phaser - Advanced Physics - Shape @@ -19548,7 +19545,7 @@ var Phaser; /// /// /// - /// + /// /** * Phaser - Advanced Physics - Contact * @@ -19584,7 +19581,7 @@ var Phaser; /// /// /// - /// + /// /// /** * Phaser - Advanced Physics - ContactSolver @@ -19613,7 +19610,7 @@ var Phaser; (function (Advanced) { var ContactSolver = (function () { function ContactSolver(shape1, shape2) { - console.log('ContactSolver super'); + //console.log('ContactSolver super'); this.shape1 = shape1; this.shape2 = shape2; this.contacts = []; @@ -19643,8 +19640,8 @@ var Phaser; var sum_m_inv = body1.massInverted + body2.massInverted; for(var i = 0; i < this.contacts.length; i++) { var con = this.contacts[i]; - console.log('initSolver con'); - console.log(con); + //console.log('initSolver con'); + //console.log(con); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); Phaser.Vec2Utils.subtract(con.point, body2.position, con.r2); @@ -19683,9 +19680,6 @@ var Phaser; //var rv = vec2.sub(v2, v1); // bounce velocity dot n con.bounce = Phaser.Vec2Utils.dot(rv, con.normal) * this.elasticity; - console.log('bounce?', Phaser.Vec2Utils.dot(rv, con.normal), this.elasticity); - console.log('con over'); - console.log(con); } }; ContactSolver.prototype.warmStart = function () { @@ -19920,7 +19914,7 @@ var Phaser; /// /// /// - /// + /// /// /// /** @@ -19933,7 +19927,7 @@ var Phaser; function Collision() { } Collision.prototype.collide = function (a, b, contacts) { - console.log('collide', a.type, b.type); + //console.log('collide', a.type, b.type); // Circle (a is the circle) if(a.type == Advanced.Manager.SHAPE_TYPE_CIRCLE) { if(b.type == Advanced.Manager.SHAPE_TYPE_CIRCLE) { @@ -20300,7 +20294,7 @@ var Phaser; /// /// /// - /// + /// /// /// /// @@ -20351,7 +20345,7 @@ var Phaser; if(this.bodyHash[body.id] != undefined) { return; } - console.log('Body added to space', body.name); + //console.log('Body added to space', body.name); var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; body.awake(true); @@ -20570,7 +20564,7 @@ var Phaser; return null; }; Space.prototype.genTemporalContactSolvers = function () { - console.log('genTemporalContactSolvers'); + //console.log('genTemporalContactSolvers'); //var t0 = Date.now(); var newContactSolverArr = []; this.numContacts = 0; @@ -20604,7 +20598,7 @@ var Phaser; if(!body1.bounds.intersectsBounds(body2.bounds)) { continue; } - console.log('>>>>>>>>>> intersects'); + //console.log('>>>>>>>>>> intersects'); for(var i = 0; i < body1.shapes.length; i++) { for(var j = 0; j < body2.shapes.length; j++) { var shape1 = body1.shapes[i]; @@ -20832,10 +20826,10 @@ var Phaser; /// /// /// - /// + /// /// /// - /// + /// /** * Phaser - Advanced Physics - Body * @@ -20890,7 +20884,7 @@ var Phaser; this.stepCount = 0; } Body.prototype.duplicate = function () { - console.log('body duplicate called'); + //console.log('body duplicate called'); //var body = new Body(this.type, this.transform.t, this.angle); //for (var i = 0; i < this.shapes.length; i++) //{ @@ -21015,7 +21009,7 @@ var Phaser; var centroid = shape.centroid(); var mass = shape.area() * shape.density; var inertia = shape.inertia(mass); - console.log('rmd', centroid, shape); + //console.log('rmd', centroid, shape); totalMassCentroid.multiplyAddByScalar(centroid, mass); totalMass += mass; totalInertia += inertia; @@ -21050,7 +21044,7 @@ var Phaser; } }; Body.prototype.cacheData = function () { - console.log('Body cacheData', this.name, 'len', this.shapes.length); + //console.log('Body cacheData', this.name, 'len', this.shapes.length); this.bounds.clear(); for(var i = 0; i < this.shapes.length; i++) { var shape = this.shapes[i]; @@ -21218,7 +21212,7 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - console.log('cloning vert', i); + //console.log('cloning vert', i); this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; this.tplanes[i] = { @@ -21227,10 +21221,10 @@ var Phaser; this.tplanes[i].d = 0; } } - console.log('ShapePoly finished', this.verts); + //console.log('ShapePoly finished', this.verts); this.finishVerts(); - console.log('ShapePoly finished 2', this.verts); - } + //console.log('ShapePoly finished 2', this.verts); + } ShapePoly.prototype.finishVerts = function () { if(this.verts.length < 2) { this.convexity = false; @@ -21294,7 +21288,7 @@ var Phaser; ShapePoly.prototype.cacheData = function (xf) { this.bounds.clear(); var numVerts = this.verts.length; - console.log('shapePoly cacheData', numVerts); + //console.log('shapePoly cacheData', numVerts); if(numVerts == 0) { return; } @@ -21606,6 +21600,491 @@ var Phaser; })(Phaser.Physics || (Phaser.Physics = {})); var Physics = Phaser.Physics; })(Phaser || (Phaser = {})); +var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shapes - Box + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Box = (function (_super) { + __extends(Box, _super); + // Give in pixels + function Box(x, y, width, height) { + x = Advanced.Manager.pixelsToMeters(x); + y = Advanced.Manager.pixelsToMeters(y); + width = Advanced.Manager.pixelsToMeters(width); + height = Advanced.Manager.pixelsToMeters(height); + var hw = width * 0.5; + var hh = height * 0.5; + _super.call(this, [ + new Phaser.Vec2(-hw + x, +hh + y), + new Phaser.Vec2(-hw + x, -hh + y), + new Phaser.Vec2(+hw + x, -hh + y), + new Phaser.Vec2(+hw + x, +hh + y) + ]); + } + return Box; + })(Phaser.Physics.Advanced.ShapePoly); + Shapes.Box = Box; + })(Advanced.Shapes || (Advanced.Shapes = {})); + var Shapes = Advanced.Shapes; + })(Physics.Advanced || (Physics.Advanced = {})); + var Advanced = Physics.Advanced; + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shape - Circle + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Circle = (function (_super) { + __extends(Circle, _super); + function Circle(radius, x, y) { + if (typeof x === "undefined") { x = 0; } + if (typeof y === "undefined") { y = 0; } + _super.call(this, Advanced.Manager.SHAPE_TYPE_CIRCLE); + this.center = new Phaser.Vec2(x, y); + this.radius = radius; + this.tc = new Phaser.Vec2(); + this.finishVerts(); + } + Circle.prototype.finishVerts = function () { + this.radius = Math.abs(this.radius); + }; + Circle.prototype.duplicate = function () { + return new Advanced.ShapeCircle(this.center.x, this.center.y, this.radius); + }; + Circle.prototype.recenter = function (c) { + this.center.subtract(c); + }; + Circle.prototype.transform = function (xf) { + Phaser.TransformUtils.transform(xf, this.center, this.center); + //this.center = xf.transform(this.center); + }; + Circle.prototype.untransform = function (xf) { + Phaser.TransformUtils.untransform(xf, this.center, this.center); + //this.center = xf.untransform(this.center); + }; + Circle.prototype.area = function () { + return Advanced.Manager.areaForCircle(this.radius, 0); + }; + Circle.prototype.centroid = function () { + return Phaser.Vec2Utils.clone(this.center); + }; + Circle.prototype.inertia = function (mass) { + return Advanced.Manager.inertiaForCircle(mass, this.center, this.radius, 0); + }; + Circle.prototype.cacheData = function (xf) { + Phaser.TransformUtils.transform(xf, this.center, this.tc); + //this.tc = xf.transform(this.center); + this.bounds.mins.setTo(this.tc.x - this.radius, this.tc.y - this.radius); + this.bounds.maxs.setTo(this.tc.x + this.radius, this.tc.y + this.radius); + }; + Circle.prototype.pointQuery = function (p) { + //return vec2.distsq(this.tc, p) < (this.r * this.r); + return Phaser.Vec2Utils.distanceSq(this.tc, p) < (this.radius * this.radius); + }; + Circle.prototype.findVertexByPoint = function (p, minDist) { + var dsq = minDist * minDist; + if(Phaser.Vec2Utils.distanceSq(this.tc, p) < dsq) { + return 0; + } + return -1; + }; + Circle.prototype.distanceOnPlane = function (n, d) { + Phaser.Vec2Utils.dot(n, this.tc) - this.radius - d; + }; + return Circle; + })(Phaser.Physics.Advanced.Shape); + Shapes.Circle = Circle; + })(Advanced.Shapes || (Advanced.Shapes = {})); + var Shapes = Advanced.Shapes; + })(Physics.Advanced || (Physics.Advanced = {})); + var Advanced = Physics.Advanced; + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shapes - Convex Polygon + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Poly = (function (_super) { + __extends(Poly, _super); + function Poly(verts) { + _super.call(this, Advanced.Manager.SHAPE_TYPE_POLY); + this.verts = []; + this.planes = []; + this.tverts = []; + this.tplanes = []; + if(verts) { + for(var i = 0; i < verts.length; i++) { + this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); + this.tverts[i] = this.verts[i]; + this.tplanes[i] = { + }; + this.tplanes[i].n = new Phaser.Vec2(); + this.tplanes[i].d = 0; + } + } + this.finishVerts(); + } + Poly.prototype.finishVerts = function () { + if(this.verts.length < 2) { + this.convexity = false; + this.planes = []; + return; + } + this.convexity = true; + this.tverts = []; + this.tplanes = []; + // Must be counter-clockwise verts + for(var i = 0; i < this.verts.length; i++) { + var a = this.verts[i]; + var b = this.verts[(i + 1) % this.verts.length]; + var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b))); + this.planes[i] = { + }; + this.planes[i].n = n; + this.planes[i].d = Phaser.Vec2Utils.dot(n, a); + this.tverts[i] = this.verts[i]; + this.tplanes[i] = { + }; + this.tplanes[i].n = new Phaser.Vec2(); + this.tplanes[i].d = 0; + } + for(var i = 0; i < this.verts.length; i++) { + var b = this.verts[(i + 2) % this.verts.length]; + var n = this.planes[i].n; + var d = this.planes[i].d; + if(Phaser.Vec2Utils.dot(n, b) - d > 0) { + this.convexity = false; + } + } + }; + Poly.prototype.duplicate = function () { + return new Advanced.ShapePoly(this.verts); + }; + Poly.prototype.recenter = function (c) { + for(var i = 0; i < this.verts.length; i++) { + this.verts[i].subtract(c); + } + }; + Poly.prototype.transform = function (xf) { + for(var i = 0; i < this.verts.length; i++) { + this.verts[i] = xf.transform(this.verts[i]); + } + }; + Poly.prototype.untransform = function (xf) { + for(var i = 0; i < this.verts.length; i++) { + this.verts[i] = xf.untransform(this.verts[i]); + } + }; + Poly.prototype.area = function () { + return Advanced.Manager.areaForPoly(this.verts); + }; + Poly.prototype.centroid = function () { + return Advanced.Manager.centroidForPoly(this.verts); + }; + Poly.prototype.inertia = function (mass) { + return Advanced.Manager.inertiaForPoly(mass, this.verts, new Phaser.Vec2()); + }; + Poly.prototype.cacheData = function (xf) { + this.bounds.clear(); + var numVerts = this.verts.length; + //console.log('shapePoly cacheData', numVerts); + if(numVerts == 0) { + return; + } + for(var i = 0; i < numVerts; i++) { + Phaser.TransformUtils.transform(xf, this.tverts[i], this.tverts[i]); + //this.tverts[i] = xf.transform(this.verts[i]); + } + if(numVerts < 2) { + this.bounds.addPoint(this.tverts[0]); + return; + } + for(var i = 0; i < numVerts; i++) { + var a = this.tverts[i]; + var b = this.tverts[(i + 1) % numVerts]; + var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b))); + this.tplanes[i].n = n; + this.tplanes[i].d = Phaser.Vec2Utils.dot(n, a); + this.bounds.addPoint(a); + } + }; + Poly.prototype.pointQuery = function (p) { + if(!this.bounds.containPoint(p)) { + return false; + } + return this.containPoint(p); + }; + Poly.prototype.findVertexByPoint = function (p, minDist) { + var dsq = minDist * minDist; + for(var i = 0; i < this.tverts.length; i++) { + if(Phaser.Vec2Utils.distanceSq(this.tverts[i], p) < dsq) { + return i; + } + } + return -1; + }; + Poly.prototype.findEdgeByPoint = function (p, minDist) { + var dsq = minDist * minDist; + var numVerts = this.tverts.length; + for(var i = 0; i < this.tverts.length; i++) { + var v1 = this.tverts[i]; + var v2 = this.tverts[(i + 1) % numVerts]; + var n = this.tplanes[i].n; + var dtv1 = Phaser.Vec2Utils.cross(v1, n); + var dtv2 = Phaser.Vec2Utils.cross(v2, n); + var dt = Phaser.Vec2Utils.cross(p, n); + if(dt > dtv1) { + if(Phaser.Vec2Utils.distanceSq(v1, p) < dsq) { + return i; + } + } else if(dt < dtv2) { + if(Phaser.Vec2Utils.distanceSq(v2, p) < dsq) { + return i; + } + } else { + var dist = Phaser.Vec2Utils.dot(n, p) - Phaser.Vec2Utils.dot(n, v1); + if(dist * dist < dsq) { + return i; + } + } + } + return -1; + }; + Poly.prototype.distanceOnPlane = function (n, d) { + var min = 999999; + for(var i = 0; i < this.verts.length; i++) { + min = Math.min(min, Phaser.Vec2Utils.dot(n, this.tverts[i])); + } + return min - d; + }; + Poly.prototype.containPoint = function (p) { + for(var i = 0; i < this.verts.length; i++) { + var plane = this.tplanes[i]; + if(Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0) { + return false; + } + } + return true; + }; + Poly.prototype.containPointPartial = function (p, n) { + for(var i = 0; i < this.verts.length; i++) { + var plane = this.tplanes[i]; + if(Phaser.Vec2Utils.dot(plane.n, n) < 0.0001) { + continue; + } + if(Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0) { + return false; + } + } + return true; + }; + return Poly; + })(Phaser.Physics.Advanced.Shape); + Shapes.Poly = Poly; + })(Advanced.Shapes || (Advanced.Shapes = {})); + var Shapes = Advanced.Shapes; + })(Physics.Advanced || (Physics.Advanced = {})); + var Advanced = Physics.Advanced; + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shapes - Segment + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Segment = (function (_super) { + __extends(Segment, _super); + function Segment(a, b, radius) { + _super.call(this, Advanced.Manager.SHAPE_TYPE_SEGMENT); + // What types are A and B??! + this.a = a.duplicate(); + this.b = b.duplicate(); + this.radius = radius; + this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(b, a)); + this.normal.normalize(); + this.ta = new Phaser.Vec2(); + this.tb = new Phaser.Vec2(); + this.tn = new Phaser.Vec2(); + this.finishVerts(); + } + Segment.prototype.finishVerts = function () { + this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.b, this.a)); + this.normal.normalize(); + this.radius = Math.abs(this.radius); + }; + Segment.prototype.duplicate = function () { + return new Advanced.ShapeSegment(this.a, this.b, this.radius); + }; + Segment.prototype.recenter = function (c) { + this.a.subtract(c); + this.b.subtract(c); + }; + Segment.prototype.transform = function (xf) { + Phaser.TransformUtils.transform(xf, this.a, this.a); + Phaser.TransformUtils.transform(xf, this.b, this.b); + //this.a = xf.transform(this.a); + //this.b = xf.transform(this.b); + }; + Segment.prototype.untransform = function (xf) { + Phaser.TransformUtils.untransform(xf, this.a, this.a); + Phaser.TransformUtils.untransform(xf, this.b, this.b); + //this.a = xf.untransform(this.a); + //this.b = xf.untransform(this.b); + }; + Segment.prototype.area = function () { + return Advanced.Manager.areaForSegment(this.a, this.b, this.radius); + }; + Segment.prototype.centroid = function () { + return Advanced.Manager.centroidForSegment(this.a, this.b); + }; + Segment.prototype.inertia = function (mass) { + return Advanced.Manager.inertiaForSegment(mass, this.a, this.b); + }; + Segment.prototype.cacheData = function (xf) { + Phaser.TransformUtils.transform(xf, this.a, this.ta); + Phaser.TransformUtils.transform(xf, this.b, this.tb); + //this.ta = xf.transform(this.a); + //this.tb = xf.transform(this.b); + this.tn = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.tb, this.ta)).normalize(); + var l; + var r; + var t; + var b; + if(this.ta.x < this.tb.x) { + l = this.ta.x; + r = this.tb.x; + } else { + l = this.tb.x; + r = this.ta.x; + } + if(this.ta.y < this.tb.y) { + b = this.ta.y; + t = this.tb.y; + } else { + b = this.tb.y; + t = this.ta.y; + } + this.bounds.mins.setTo(l - this.radius, b - this.radius); + this.bounds.maxs.setTo(r + this.radius, t + this.radius); + }; + Segment.prototype.pointQuery = function (p) { + if(!this.bounds.containPoint(p)) { + return false; + } + var dn = Phaser.Vec2Utils.dot(this.tn, p) - Phaser.Vec2Utils.dot(this.ta, this.tn); + var dist = Math.abs(dn); + if(dist > this.radius) { + return false; + } + var dt = Phaser.Vec2Utils.cross(p, this.tn); + var dta = Phaser.Vec2Utils.cross(this.ta, this.tn); + var dtb = Phaser.Vec2Utils.cross(this.tb, this.tn); + if(dt <= dta) { + if(dt < dta - this.radius) { + return false; + } + return Phaser.Vec2Utils.distanceSq(this.ta, p) < (this.radius * this.radius); + } else if(dt > dtb) { + if(dt > dtb + this.radius) { + return false; + } + return Phaser.Vec2Utils.distanceSq(this.tb, p) < (this.radius * this.radius); + } + return true; + }; + Segment.prototype.findVertexByPoint = function (p, minDist) { + var dsq = minDist * minDist; + if(Phaser.Vec2Utils.distanceSq(this.ta, p) < dsq) { + return 0; + } + if(Phaser.Vec2Utils.distanceSq(this.tb, p) < dsq) { + return 1; + } + return -1; + }; + Segment.prototype.distanceOnPlane = function (n, d) { + var a = Phaser.Vec2Utils.dot(n, this.ta) - this.radius; + var b = Phaser.Vec2Utils.dot(n, this.tb) - this.radius; + return Math.min(a, b) - d; + }; + return Segment; + })(Phaser.Physics.Advanced.Shape); + Shapes.Segment = Segment; + })(Advanced.Shapes || (Advanced.Shapes = {})); + var Shapes = Advanced.Shapes; + })(Physics.Advanced || (Physics.Advanced = {})); + var Advanced = Physics.Advanced; + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +// Module +var Shapes; +(function (Shapes) { + // Class + var Point = (function () { + // Constructor + function Point(x, y) { + this.x = x; + this.y = y; + } + Point.prototype.getDist = // Instance member + function () { + return Math.sqrt(this.x * this.x + this.y * this.y); + }; + Point.origin = new Point(0, 0); + return Point; + })(); + Shapes.Point = Point; +})(Shapes || (Shapes = {})); +// Local variables +var p = new Shapes.Point(3, 4); +var dist = p.getDist(); /// /// ///