diff --git a/Phaser/Phaser.csproj b/Phaser/Phaser.csproj index 2cd314bf..357dabaa 100644 --- a/Phaser/Phaser.csproj +++ b/Phaser/Phaser.csproj @@ -167,6 +167,10 @@ Transform.ts + + + TransformUtils.ts + Vec2.ts @@ -195,6 +199,10 @@ ContactSolver.ts + + + IShape.ts + Joint.ts diff --git a/Phaser/math/Transform.ts b/Phaser/math/Transform.ts index 37145545..4b5f5926 100644 --- a/Phaser/math/Transform.ts +++ b/Phaser/math/Transform.ts @@ -23,12 +23,8 @@ module Phaser { this.c = Math.cos(angle); this.s = Math.sin(angle); - this._tempVec = new Phaser.Vec2; - } - private _tempVec: Phaser.Vec2; - public t: Phaser.Vec2; public c: number; public s: number; @@ -68,28 +64,6 @@ module Phaser { } - public rotate(v:Phaser.Vec2):Phaser.Vec2 { - return this._tempVec.setTo(v.x * this.c - v.y * this.s, v.x * this.s + v.y * this.c); - } - - public unrotate(v:Phaser.Vec2):Phaser.Vec2 { - return this._tempVec.setTo(v.x * this.c + v.y * this.s, -v.x * this.s + v.y * this.c); - } - - public transform(v:Phaser.Vec2):Phaser.Vec2 { - return this._tempVec.setTo(v.x * this.c - v.y * this.s + this.t.x, v.x * this.s + v.y * this.c + this.t.y); - } - - public untransform(v:Phaser.Vec2):Phaser.Vec2 { - - var px = v.x - this.t.x; - var py = v.y - this.t.y; - - // expensive - check for alternatives - return this._tempVec.setTo(px * this.c + py * this.s, -px * this.s + py * this.c); - - } - } } \ No newline at end of file diff --git a/Phaser/math/TransformUtils.ts b/Phaser/math/TransformUtils.ts new file mode 100644 index 00000000..583911f2 --- /dev/null +++ b/Phaser/math/TransformUtils.ts @@ -0,0 +1,39 @@ +/// +/// +/// + +/** +* Phaser - TransformUtils +* +* A collection of methods useful for manipulating and performing operations on 2D Transforms. +* +*/ + +module Phaser { + + export class TransformUtils { + + public static rotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 { + return out.setTo(v.x * t.c - v.y * t.s, v.x * t.s + v.y * t.c); + } + + public static unrotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 { + return out.setTo(v.x * t.c + v.y * t.s, -v.x * t.s + v.y * t.c); + } + + public static transform(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 { + return out.setTo(v.x * t.c - v.y * t.s + t.t.x, v.x * t.s + v.y * t.c + t.t.y); + } + + public static untransform(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 { + + var px = v.x - t.t.x; + var py = v.y - t.t.y; + + return out.setTo(px * t.c + py * t.s, -px * t.s + py * t.c); + + } + + } + +} \ No newline at end of file diff --git a/Phaser/math/Vec2Utils.ts b/Phaser/math/Vec2Utils.ts index 6c1ff49d..ec46d7b4 100644 --- a/Phaser/math/Vec2Utils.ts +++ b/Phaser/math/Vec2Utils.ts @@ -1,5 +1,5 @@ /// -/// +/// /** * Phaser - Vec2Utils diff --git a/Phaser/physics/advanced/Body.ts b/Phaser/physics/advanced/Body.ts index b35ec1da..1d97fe7d 100644 --- a/Phaser/physics/advanced/Body.ts +++ b/Phaser/physics/advanced/Body.ts @@ -2,10 +2,12 @@ /// /// /// +/// /// /// /// /// +/// /** * Phaser - Advanced Physics - Body @@ -128,14 +130,14 @@ module Phaser.Physics.Advanced { public awaked: bool; // Shapes - public shapes = []; + public shapes: IShape[] = []; // Joints - public joints = []; + public joints: IJoint[] = []; public jointHash = {}; // Bounds of all shapes - public bounds; + public bounds: Bounds; public fixedRotation = false; public categoryBits = 0x0001; @@ -143,22 +145,22 @@ module Phaser.Physics.Advanced { public stepCount = 0; public space: Space; - /* public duplicate() { - var body = new Body(this.type, this.transform.t, this.angle); + 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++) - { - body.addShape(this.shapes[i].duplicate()); - } + //for (var i = 0; i < this.shapes.length; i++) + //{ + // body.addShape(this.shapes[i].duplicate()); + //} - body.resetMassData(); + //body.resetMassData(); - return body; + //return body; } - */ public get isDisabled(): bool { return this.type == Phaser.Types.BODY_DISABLED ? true : false; @@ -197,8 +199,11 @@ module Phaser.Physics.Advanced { // Check not already part of this body shape.body = this; + this.shapes.push(shape); + return shape; + } public removeShape(shape) { @@ -234,8 +239,10 @@ module Phaser.Physics.Advanced { public setTransform(pos, angle) { - this.transform.setTo(pos, angle) - this.position = this.transform.transform(this.centroid); + this.transform.setTo(pos, angle); + // inject the transform into this.position + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); this.angle = angle; } @@ -243,26 +250,37 @@ module Phaser.Physics.Advanced { public syncTransform() { this.transform.setRotation(this.angle); + + //var rotc: Phaser.Vec2 = this.transform.rotate(this.centroid); + //var sub: Phaser.Vec2 = Phaser.Vec2Utils.subtract(this.position, rotc); + //this.transform.setPosition(sub); + // this.transform.setPosition(vec2.sub(this.position, this.transform.rotate(this.centroid))); - Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + //Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + + // OPTIMISE: Creating new vector + Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t); } public getWorldPoint(p:Phaser.Vec2) { - // This is returning a new vector - check it's actually used in that way - return this.transform.transform(p) + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.transform(this.transform, p); } public getWorldVector(v) { - return this.transform.rotate(v) + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.rotate(this.transform, v); } public getLocalPoint(p) { - return this.transform.untransform(p) + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.untransform(this.transform, p); } public getLocalVector(v) { - return this.transform.unrotate(v) + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.unrotate(this.transform, v); } public setFixedRotation(flag) { @@ -280,7 +298,8 @@ module Phaser.Physics.Advanced { if (this.isDynamic == false) { - this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); return; } @@ -317,7 +336,8 @@ module Phaser.Physics.Advanced { // Move center of mass var oldPosition: Phaser.Vec2 = Phaser.Vec2Utils.clone(this.position); - this.position = this.transform.transform(this.centroid); + //this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); // Update center of mass velocity @@ -347,6 +367,9 @@ module Phaser.Physics.Advanced { } public cacheData() { + + console.log('Body cacheData', this.name, 'len', this.shapes.length); + this.bounds.clear(); for (var i = 0; i < this.shapes.length; i++) @@ -385,9 +408,15 @@ module Phaser.Physics.Advanced { public updatePosition(dt) { + //console.log('body update pos', this.position.y); + //console.log('pre add temp', this._tempVec2.y); + //this.position.addself(vec2.scale(this.velocity, dt)); this.position.add(Phaser.Vec2Utils.scale(this.velocity, dt, this._tempVec2)); + //console.log('post add temp', this._tempVec2.y); + //console.log('post add', this.position.y); + this.angle += this.angularVelocity * dt; } diff --git a/Phaser/physics/advanced/Bounds.ts b/Phaser/physics/advanced/Bounds.ts index 314cf8b2..d73f3bc8 100644 --- a/Phaser/physics/advanced/Bounds.ts +++ b/Phaser/physics/advanced/Bounds.ts @@ -47,21 +47,28 @@ module Phaser.Physics.Advanced { return ["mins:", this.mins.toString(), "maxs:", this.maxs.toString()].join(" "); } - public setTo(mins, maxs) { + public setTo(mins: Phaser.Vec2, maxs: Phaser.Vec2) { + this.mins.setTo(mins.x, mins.y); this.maxs.setTo(maxs.x, maxs.y); + } - public copy(b:Bounds) { + public copy(b: Bounds): Bounds { + this.mins.copyFrom(b.mins); this.maxs.copyFrom(b.maxs); + return this; } - public clear() { + public clear(): Bounds { + this.mins.setTo(999999, 999999); this.maxs.setTo(-999999, -999999); + return this; + } public isEmpty(): bool { @@ -78,23 +85,27 @@ module Phaser.Physics.Advanced { } */ - public getPerimeter() { + public getPerimeter(): number { return (this.maxs.x - this.mins.x + this.maxs.y - this.mins.y) * 2; } - public addPoint(p) { + public addPoint(p: Phaser.Vec2): Bounds { + if (this.mins.x > p.x) this.mins.x = p.x; if (this.maxs.x < p.x) this.maxs.x = p.x; if (this.mins.y > p.y) this.mins.y = p.y; if (this.maxs.y < p.y) this.maxs.y = p.y; + return this; } - public addBounds(b) { + public addBounds(b: Bounds): Bounds { + if (this.mins.x > b.mins.x) this.mins.x = b.mins.x; if (this.maxs.x < b.maxs.x) this.maxs.x = b.maxs.x; if (this.mins.y > b.mins.y) this.mins.y = b.mins.y; if (this.maxs.y < b.maxs.y) this.maxs.y = b.maxs.y; + return this; } @@ -106,23 +117,29 @@ module Phaser.Physics.Advanced { return this; } - public addExtents(center, extent_x, extent_y) { + public addExtents(center: Phaser.Vec2, extent_x: number, extent_y: number): Bounds { + if (this.mins.x > center.x - extent_x) this.mins.x = center.x - extent_x; if (this.maxs.x < center.x + extent_x) this.maxs.x = center.x + extent_x; if (this.mins.y > center.y - extent_y) this.mins.y = center.y - extent_y; if (this.maxs.y < center.y + extent_y) this.maxs.y = center.y + extent_y; + return this; + } - public expand(ax, ay) { + public expand(ax: number, ay: number): Bounds { + this.mins.x -= ax; this.mins.y -= ay; this.maxs.x += ax; this.maxs.y += ay; + return this; + } - public containPoint(p) { + public containPoint(p: Phaser.Vec2): bool { if (p.x < this.mins.x || p.x > this.maxs.x || p.y < this.mins.y || p.y > this.maxs.y) { @@ -133,20 +150,26 @@ module Phaser.Physics.Advanced { } - public intersectsBounds(b) { - + public intersectsBounds(b: Bounds): bool { + 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; } - public static expand(b, ax, ay) { + public static expand(b: Bounds, ax, ay) { + var b = new Bounds(b.mins, b.maxs); b.expand(ax, ay); return b; + } } diff --git a/Phaser/physics/advanced/Collision.ts b/Phaser/physics/advanced/Collision.ts index 68d474f3..4393a4f4 100644 --- a/Phaser/physics/advanced/Collision.ts +++ b/Phaser/physics/advanced/Collision.ts @@ -22,6 +22,8 @@ module Phaser.Physics.Advanced { public collide(a, b, contacts: Contact[]) { + 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 0380db09..d97e510a 100644 --- a/Phaser/physics/advanced/Contact.ts +++ b/Phaser/physics/advanced/Contact.ts @@ -24,6 +24,11 @@ module Phaser.Physics.Advanced { this.lambdaNormal = 0; this.lambdaTangential = 0; + this.r1 = new Phaser.Vec2; + this.r2 = new Phaser.Vec2; + this.r1_local = new Phaser.Vec2; + this.r2_local = new Phaser.Vec2; + } public hash; @@ -31,8 +36,8 @@ module Phaser.Physics.Advanced { // Linear velocities at contact point public r1: Phaser.Vec2; public r2: Phaser.Vec2; - public r1_local; - public r2_local; + public r1_local: Phaser.Vec2; + public r2_local: Phaser.Vec2; // Bounce velocity public bounce; public emn; diff --git a/Phaser/physics/advanced/ContactSolver.ts b/Phaser/physics/advanced/ContactSolver.ts index ac6f16e8..62169ce4 100644 --- a/Phaser/physics/advanced/ContactSolver.ts +++ b/Phaser/physics/advanced/ContactSolver.ts @@ -38,6 +38,8 @@ module Phaser.Physics.Advanced { constructor(shape1, shape2) { + console.log('ContactSolver super'); + this.shape1 = shape1; this.shape2 = shape2; @@ -95,7 +97,10 @@ module Phaser.Physics.Advanced { for (var i = 0; i < this.contacts.length; i++) { - var con = this.contacts[i]; + var con: Contact = this.contacts[i]; + + console.log('initSolver con'); + console.log(con); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); @@ -104,8 +109,10 @@ module Phaser.Physics.Advanced { //con.r2 = vec2.sub(con.point, body2.p); // Local r1, r2 - con.r1_local = body1.transform.unrotate(con.r1); - con.r2_local = body2.transform.unrotate(con.r2); + Phaser.TransformUtils.unrotate(body1.transform, con.r1, con.r1_local); + Phaser.TransformUtils.unrotate(body2.transform, con.r2, con.r2_local); + //con.r1_local = body1.transform.unrotate(con.r1); + //con.r2_local = body2.transform.unrotate(con.r2); var n = con.normal; var t = Phaser.Vec2Utils.perp(con.normal); @@ -142,6 +149,10 @@ 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/IShape.ts b/Phaser/physics/advanced/IShape.ts new file mode 100644 index 00000000..06fa8c16 --- /dev/null +++ b/Phaser/physics/advanced/IShape.ts @@ -0,0 +1,39 @@ +/// +/// +/// +/// +/// +/// + +/** +* Phaser - Advanced Physics - IShape +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ + +module Phaser.Physics.Advanced { + + export interface IShape { + + id: number; + type: number; + + elasticity: number; + friction: number; + density: number; + + body: Body; + bounds: Bounds; + + area(): number; + centroid(): Phaser.Vec2; + inertia(mass: number): number; + cacheData(xf:Transform); + pointQuery(p: Phaser.Vec2): bool; + findEdgeByPoint(p: Phaser.Vec2, minDist: number): number; + findVertexByPoint(p: Phaser.Vec2, minDist: number): number; + + } + +} + diff --git a/Phaser/physics/advanced/Manager.ts b/Phaser/physics/advanced/Manager.ts index c4b779df..d7749e61 100644 --- a/Phaser/physics/advanced/Manager.ts +++ b/Phaser/physics/advanced/Manager.ts @@ -63,7 +63,7 @@ module Phaser.Physics.Advanced { public static shapeCounter: number = 0; public space: Space; - public lastTime: number = 0; + public lastTime: number = Date.now(); public frameRateHz: number = 60; public timeDelta: number = 0; public paused: bool = false; @@ -146,7 +146,13 @@ module Phaser.Physics.Advanced { + public pixelsToMeters(value: number): number { + return value * 0.02; + } + public metersToPixels(value: number): number { + return value * 50; + } public static pixelsToMeters(value: number): number { return value * 0.02; @@ -164,23 +170,23 @@ module Phaser.Physics.Advanced { return value * 50; } - public static areaForCircle(radius_outer, radius_inner) { + public static areaForCircle(radius_outer, radius_inner): number { return Math.PI * (radius_outer * radius_outer - radius_inner * radius_inner); } - public static inertiaForCircle(mass, center, radius_outer, radius_inner) { + public static inertiaForCircle(mass, center, radius_outer, radius_inner): number { return mass * ((radius_outer * radius_outer + radius_inner * radius_inner) * 0.5 + center.lengthSq()); } - public static areaForSegment(a, b, radius) { + public static areaForSegment(a, b, radius): number { return radius * (Math.PI * radius + 2 * Phaser.Vec2Utils.distance(a, b)); } - public static centroidForSegment(a, b) { + public static centroidForSegment(a, b): Phaser.Vec2 { return Phaser.Vec2Utils.scale(Phaser.Vec2Utils.add(a, b), 0.5); } - public static inertiaForSegment(mass, a, b) { + public static inertiaForSegment(mass, a, b): number { var distsq = Phaser.Vec2Utils.distanceSq(b, a); var offset: Phaser.Vec2 = Phaser.Vec2Utils.scale(Phaser.Vec2Utils.add(a, b), 0.5); @@ -188,7 +194,7 @@ module Phaser.Physics.Advanced { return mass * (distsq / 12 + offset.lengthSq()); } - public static areaForPoly(verts) { + public static areaForPoly(verts): number { var area = 0; @@ -200,7 +206,7 @@ module Phaser.Physics.Advanced { return area / 2; } - public static centroidForPoly(verts) { + public static centroidForPoly(verts): Phaser.Vec2 { var area = 0; var vsum = new Phaser.Vec2; @@ -220,7 +226,7 @@ module Phaser.Physics.Advanced { return Phaser.Vec2Utils.scale(vsum, 1 / (3 * area)); } - public static inertiaForPoly(mass, verts, offset) { + public static inertiaForPoly(mass, verts, offset): number { var sum1 = 0; var sum2 = 0; diff --git a/Phaser/physics/advanced/Shape.ts b/Phaser/physics/advanced/Shape.ts index 5af9f73e..657fdf96 100644 --- a/Phaser/physics/advanced/Shape.ts +++ b/Phaser/physics/advanced/Shape.ts @@ -4,6 +4,7 @@ /// /// /// +/// /** * Phaser - Advanced Physics - Shape @@ -30,6 +31,7 @@ module Phaser.Physics.Advanced { public id: number; public type: number; + public body: Body; // Coefficient of restitution (elasticity) public elasticity: number; @@ -43,6 +45,11 @@ module Phaser.Physics.Advanced { // Axis-aligned bounding box public bounds: Bounds; + // Over-ridden by ShapePoly + public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number { + return -1; + } + } } \ No newline at end of file diff --git a/Phaser/physics/advanced/ShapeBox.ts b/Phaser/physics/advanced/ShapeBox.ts index 4784a351..6121b1f0 100644 --- a/Phaser/physics/advanced/ShapeBox.ts +++ b/Phaser/physics/advanced/ShapeBox.ts @@ -16,8 +16,16 @@ module Phaser.Physics.Advanced { export class ShapeBox extends Phaser.Physics.Advanced.ShapePoly { + // Give in pixels constructor(x, y, width, height) { + console.log('creating box', 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; diff --git a/Phaser/physics/advanced/ShapeCircle.ts b/Phaser/physics/advanced/ShapeCircle.ts index f0b97f26..e9a8a25b 100644 --- a/Phaser/physics/advanced/ShapeCircle.ts +++ b/Phaser/physics/advanced/ShapeCircle.ts @@ -13,7 +13,7 @@ module Phaser.Physics.Advanced { - export class ShapeCircle extends Phaser.Physics.Advanced.Shape { + export class ShapeCircle extends Phaser.Physics.Advanced.Shape implements IShape { constructor(radius: number, x?: number = 0, y?: number = 0) { @@ -39,43 +39,49 @@ module Phaser.Physics.Advanced { return new ShapeCircle(this.center.x, this.center.y, this.radius); } - public recenter(c) { + public recenter(c:Phaser.Vec2) { this.center.subtract(c); } - public transform(xf) { - this.center = xf.transform(this.center); + public transform(xf: Transform) { + + Phaser.TransformUtils.transform(xf, this.center, this.center); + //this.center = xf.transform(this.center); } - public untransform(xf) { - this.center = xf.untransform(this.center); + public untransform(xf: Transform) { + Phaser.TransformUtils.untransform(xf, this.center, this.center); + //this.center = xf.untransform(this.center); } - public area() { + public area(): number { return Manager.areaForCircle(this.radius, 0); } - public centroid() { + public centroid(): Phaser.Vec2 { return Phaser.Vec2Utils.clone(this.center); } - public inertia(mass) { + 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); - public cacheData(xf) { - 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) { + 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, minDist) { + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { var dsq = minDist * minDist; diff --git a/Phaser/physics/advanced/ShapePoly.ts b/Phaser/physics/advanced/ShapePoly.ts index 918dfaeb..1202c64f 100644 --- a/Phaser/physics/advanced/ShapePoly.ts +++ b/Phaser/physics/advanced/ShapePoly.ts @@ -13,10 +13,12 @@ module Phaser.Physics.Advanced { - export class ShapePoly extends Phaser.Physics.Advanced.Shape { + export class ShapePoly extends Phaser.Physics.Advanced.Shape implements IShape { constructor(verts?:Phaser.Vec2[]) { + console.log('ShapePoly created', verts); + super(Manager.SHAPE_TYPE_POLY); this.verts = []; @@ -29,7 +31,8 @@ module Phaser.Physics.Advanced { { for (var i = 0; i < verts.length; i++) { - Phaser.Vec2Utils.clone(verts[i], this.verts[i]); + console.log('cloning vert', i); + this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; this.tplanes[i] = {}; @@ -38,8 +41,12 @@ module Phaser.Physics.Advanced { } } + console.log('ShapePoly finished', this.verts); + this.finishVerts(); + console.log('ShapePoly finished 2', this.verts); + } public verts: Phaser.Vec2[]; @@ -121,24 +128,26 @@ module Phaser.Physics.Advanced { } } - public area() { + public area(): number { return Manager.areaForPoly(this.verts); } - public centroid() { + public centroid(): Phaser.Vec2 { return Manager.centroidForPoly(this.verts); } - public inertia(mass) { + public inertia(mass: number): number { return Manager.inertiaForPoly(mass, this.verts, new Phaser.Vec2); } - public cacheData(xf) { + public cacheData(xf:Transform) { this.bounds.clear(); var numVerts = this.verts.length; + console.log('shapePoly cacheData', numVerts); + if (numVerts == 0) { return; @@ -146,7 +155,8 @@ module Phaser.Physics.Advanced { for (var i = 0; i < numVerts; i++) { - this.tverts[i] = xf.transform(this.verts[i]); + Phaser.TransformUtils.transform(xf, this.tverts[i], this.tverts[i]); + //this.tverts[i] = xf.transform(this.verts[i]); } if (numVerts < 2) @@ -170,7 +180,7 @@ module Phaser.Physics.Advanced { } - public pointQuery(p) { + public pointQuery(p: Phaser.Vec2): bool { if (!this.bounds.containPoint(p)) { @@ -180,7 +190,7 @@ module Phaser.Physics.Advanced { return this.containPoint(p); } - public findVertexByPoint(p, minDist) { + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { var dsq = minDist * minDist; @@ -195,7 +205,7 @@ module Phaser.Physics.Advanced { return -1; } - public findEdgeByPoint(p, minDist) { + public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number { var dsq = minDist * minDist; var numVerts = this.tverts.length; diff --git a/Phaser/physics/advanced/ShapeSegment.ts b/Phaser/physics/advanced/ShapeSegment.ts index 71703947..02adde37 100644 --- a/Phaser/physics/advanced/ShapeSegment.ts +++ b/Phaser/physics/advanced/ShapeSegment.ts @@ -13,7 +13,7 @@ module Phaser.Physics.Advanced { - export class ShapeSegment extends Phaser.Physics.Advanced.Shape { + export class ShapeSegment extends Phaser.Physics.Advanced.Shape implements IShape { constructor(a, b, radius: number) { @@ -62,32 +62,46 @@ module Phaser.Physics.Advanced { this.b.subtract(c); } - public transform(xf) { - this.a = xf.transform(this.a); - this.b = xf.transform(this.b); + 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) { - this.a = xf.untransform(this.a); - this.b = xf.untransform(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() { + public area(): number { return Manager.areaForSegment(this.a, this.b, this.radius); } - public centroid() { + public centroid(): Phaser.Vec2 { return Manager.centroidForSegment(this.a, this.b); } - public inertia(mass) { + public inertia(mass: number): number { return Manager.inertiaForSegment(mass, this.a, this.b); } - public cacheData(xf) { + 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.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; @@ -121,7 +135,7 @@ module Phaser.Physics.Advanced { } - public pointQuery(p) { + public pointQuery(p: Phaser.Vec2): bool { if (!this.bounds.containPoint(p)) { @@ -162,7 +176,7 @@ module Phaser.Physics.Advanced { return true; } - public findVertexByPoint(p, minDist) { + public findVertexByPoint(p:Phaser.Vec2, minDist: number): number { var dsq = minDist * minDist; diff --git a/Phaser/physics/advanced/Space.ts b/Phaser/physics/advanced/Space.ts index 340b204e..97e8a43c 100644 --- a/Phaser/physics/advanced/Space.ts +++ b/Phaser/physics/advanced/Space.ts @@ -32,7 +32,7 @@ module Phaser.Physics.Advanced { //this.postSolve(arb) { }; - this.gravity = new Phaser.Vec2; + this.gravity = new Phaser.Vec2(0, 10); this.damping = 0; } @@ -85,6 +85,8 @@ module Phaser.Physics.Advanced { return; } + console.log('Body added to space', body.name); + var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; @@ -447,6 +449,8 @@ module Phaser.Physics.Advanced { public genTemporalContactSolvers() { + console.log('genTemporalContactSolvers'); + //var t0 = Date.now(); var newContactSolverArr = []; @@ -455,7 +459,9 @@ module Phaser.Physics.Advanced { for (var body1_index = 0; body1_index < this.bodyArr.length; body1_index++) { - var body1 = this.bodyArr[body1_index]; + var body1: Body = this.bodyArr[body1_index]; + + //console.log('body1', body1_index, body1.type); if (!body1) { @@ -466,7 +472,10 @@ module Phaser.Physics.Advanced { for (var body2_index = 0; body2_index < this.bodyArr.length; body2_index++) { - var body2 = this.bodyArr[body2_index]; + var body2: Body = this.bodyArr[body2_index]; + + //console.log('body2', body2_index, body2.type); + if (!body2) { continue; @@ -477,6 +486,8 @@ module Phaser.Physics.Advanced { continue; } + //console.log('step'); + var active1 = body1.isAwake && !body1.isStatic; var active2 = body2.isAwake && !body2.isStatic; @@ -485,16 +496,22 @@ module Phaser.Physics.Advanced { continue; } + //console.log('active'); + if (!body1.isCollidable(body2)) { continue; } + //console.log('collideable'); + if (!body1.bounds.intersectsBounds(body2.bounds)) { continue; } + console.log('>>>>>>>>>> intersects'); + for (var i = 0; i < body1.shapes.length; i++) { for (var j = 0; j < body2.shapes.length; j++) @@ -532,8 +549,8 @@ module Phaser.Physics.Advanced { var newContactSolver = new ContactSolver(shape1, shape2); newContactSolver.contacts = contactArr; - newContactSolver.elasticity = Math.max(shape1.e, shape2.e); - newContactSolver.friction = Math.sqrt(shape1.u * shape2.u); + newContactSolver.elasticity = Math.max(shape1.elasticity, shape2.elasticity); + newContactSolver.friction = Math.sqrt(shape1.friction * shape2.friction); newContactSolverArr.push(newContactSolver); } } @@ -660,6 +677,7 @@ module Phaser.Physics.Advanced { for (var i = 0; i < this.bodyArr.length; i++) { var body = this.bodyArr[i]; + if (!body) { continue; @@ -757,7 +775,9 @@ module Phaser.Physics.Advanced { for (var i = 0; i < this.contactSolvers.length; i++) { var arb = this.contactSolvers[i]; - this.postSolve(arb); + + // Re-enable this + //this.postSolve(arb); } for (var i = 0; i < this.bodyArr.length; i++) diff --git a/Tests/phaser.js b/Tests/phaser.js index 3b388a4f..440ad3dc 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -3913,7 +3913,7 @@ var Phaser; var Components = Phaser.Components; })(Phaser || (Phaser = {})); /// -/// +/// /** * Phaser - Vec2Utils * @@ -18060,7 +18060,6 @@ var Phaser; this.t = Phaser.Vec2Utils.clone(pos); this.c = Math.cos(angle); this.s = Math.sin(angle); - this._tempVec = new Phaser.Vec2(); } Transform.prototype.setTo = function (pos, angle) { this.t.copyFrom(pos); @@ -18083,25 +18082,45 @@ var Phaser; this.s = 0; return this; }; - Transform.prototype.rotate = function (v) { - return this._tempVec.setTo(v.x * this.c - v.y * this.s, v.x * this.s + v.y * this.c); - }; - Transform.prototype.unrotate = function (v) { - return this._tempVec.setTo(v.x * this.c + v.y * this.s, -v.x * this.s + v.y * this.c); - }; - Transform.prototype.transform = function (v) { - return this._tempVec.setTo(v.x * this.c - v.y * this.s + this.t.x, v.x * this.s + v.y * this.c + this.t.y); - }; - Transform.prototype.untransform = function (v) { - var px = v.x - this.t.x; - var py = v.y - this.t.y; - // expensive - check for alternatives - return this._tempVec.setTo(px * this.c + py * this.s, -px * this.s + py * this.c); - }; return Transform; })(); Phaser.Transform = Transform; })(Phaser || (Phaser = {})); +/// +/// +/// +/** +* Phaser - TransformUtils +* +* A collection of methods useful for manipulating and performing operations on 2D Transforms. +* +*/ +var Phaser; +(function (Phaser) { + var TransformUtils = (function () { + function TransformUtils() { } + TransformUtils.rotate = function rotate(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c - v.y * t.s, v.x * t.s + v.y * t.c); + }; + TransformUtils.unrotate = function unrotate(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c + v.y * t.s, -v.x * t.s + v.y * t.c); + }; + TransformUtils.transform = function transform(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c - v.y * t.s + t.t.x, v.x * t.s + v.y * t.c + t.t.y); + }; + TransformUtils.untransform = function untransform(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + var px = v.x - t.t.x; + var py = v.y - t.t.y; + return out.setTo(px * t.c + py * t.s, -px * t.s + py * t.c); + }; + return TransformUtils; + })(); + Phaser.TransformUtils = TransformUtils; +})(Phaser || (Phaser = {})); var Phaser; (function (Phaser) { /// @@ -19112,7 +19131,7 @@ var Phaser; (function (Advanced) { var Manager = (function () { function Manager(game) { - this.lastTime = 0; + this.lastTime = Date.now(); this.frameRateHz = 60; this.timeDelta = 0; this.paused = false; @@ -19186,6 +19205,12 @@ var Phaser; } //frameCount++; }; + Manager.prototype.pixelsToMeters = function (value) { + return value * 0.02; + }; + Manager.prototype.metersToPixels = function (value) { + return value * 50; + }; Manager.pixelsToMeters = function pixelsToMeters(value) { return value * 0.02; }; @@ -19454,6 +19479,9 @@ 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) { @@ -19470,6 +19498,12 @@ var Phaser; var Physics = Phaser.Physics; })(Phaser || (Phaser = {})); var Phaser; +(function (Phaser) { + (function (Physics) { + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +var Phaser; (function (Phaser) { (function (Physics) { /// @@ -19478,6 +19512,7 @@ var Phaser; /// /// /// + /// /** * Phaser - Advanced Physics - Shape * @@ -19493,6 +19528,10 @@ var Phaser; this.density = 1; this.bounds = new Advanced.Bounds(); } + Shape.prototype.findEdgeByPoint = // Over-ridden by ShapePoly + function (p, minDist) { + return -1; + }; return Shape; })(); Advanced.Shape = Shape; @@ -19524,6 +19563,10 @@ var Phaser; this.depth = d; this.lambdaNormal = 0; this.lambdaTangential = 0; + this.r1 = new Phaser.Vec2(); + this.r2 = new Phaser.Vec2(); + this.r1_local = new Phaser.Vec2(); + this.r2_local = new Phaser.Vec2(); } return Contact; })(); @@ -19570,6 +19613,7 @@ var Phaser; (function (Advanced) { var ContactSolver = (function () { function ContactSolver(shape1, shape2) { + console.log('ContactSolver super'); this.shape1 = shape1; this.shape2 = shape2; this.contacts = []; @@ -19599,14 +19643,18 @@ 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); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); Phaser.Vec2Utils.subtract(con.point, body2.position, con.r2); //con.r1 = vec2.sub(con.point, body1.p); //con.r2 = vec2.sub(con.point, body2.p); // Local r1, r2 - con.r1_local = body1.transform.unrotate(con.r1); - con.r2_local = body2.transform.unrotate(con.r2); + Phaser.TransformUtils.unrotate(body1.transform, con.r1, con.r1_local); + Phaser.TransformUtils.unrotate(body2.transform, con.r2, con.r2_local); + //con.r1_local = body1.transform.unrotate(con.r1); + //con.r2_local = body2.transform.unrotate(con.r2); var n = con.normal; var t = Phaser.Vec2Utils.perp(con.normal); // invEMn = J * invM * JT @@ -19635,6 +19683,9 @@ 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 () { @@ -19817,11 +19868,13 @@ var Phaser; this.center.subtract(c); }; ShapeCircle.prototype.transform = function (xf) { - this.center = xf.transform(this.center); - }; + Phaser.TransformUtils.transform(xf, this.center, this.center); + //this.center = xf.transform(this.center); + }; ShapeCircle.prototype.untransform = function (xf) { - this.center = xf.untransform(this.center); - }; + Phaser.TransformUtils.untransform(xf, this.center, this.center); + //this.center = xf.untransform(this.center); + }; ShapeCircle.prototype.area = function () { return Advanced.Manager.areaForCircle(this.radius, 0); }; @@ -19832,7 +19885,8 @@ var Phaser; return Advanced.Manager.inertiaForCircle(mass, this.center, this.radius, 0); }; ShapeCircle.prototype.cacheData = function (xf) { - this.tc = xf.transform(this.center); + 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); }; @@ -19879,6 +19933,7 @@ var Phaser; function Collision() { } Collision.prototype.collide = function (a, b, contacts) { + 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) { @@ -20268,7 +20323,7 @@ var Phaser; this.numContacts = 0; this.contactSolvers = []; //this.postSolve(arb) { }; - this.gravity = new Phaser.Vec2(); + this.gravity = new Phaser.Vec2(0, 10); this.damping = 0; } Space.TIME_TO_SLEEP = 0.5; @@ -20296,6 +20351,7 @@ var Phaser; if(this.bodyHash[body.id] != undefined) { return; } + console.log('Body added to space', body.name); var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; body.awake(true); @@ -20514,34 +20570,41 @@ var Phaser; return null; }; Space.prototype.genTemporalContactSolvers = function () { + console.log('genTemporalContactSolvers'); //var t0 = Date.now(); var newContactSolverArr = []; this.numContacts = 0; for(var body1_index = 0; body1_index < this.bodyArr.length; body1_index++) { var body1 = this.bodyArr[body1_index]; + //console.log('body1', body1_index, body1.type); if(!body1) { continue; } body1.stepCount = this.stepCount; for(var body2_index = 0; body2_index < this.bodyArr.length; body2_index++) { var body2 = this.bodyArr[body2_index]; + //console.log('body2', body2_index, body2.type); if(!body2) { continue; } if(body1.stepCount == body2.stepCount) { continue; } + //console.log('step'); var active1 = body1.isAwake && !body1.isStatic; var active2 = body2.isAwake && !body2.isStatic; if(!active1 && !active2) { continue; } + //console.log('active'); if(!body1.isCollidable(body2)) { continue; } + //console.log('collideable'); if(!body1.bounds.intersectsBounds(body2.bounds)) { continue; } + 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]; @@ -20565,8 +20628,8 @@ var Phaser; body2.awake(true); var newContactSolver = new Advanced.ContactSolver(shape1, shape2); newContactSolver.contacts = contactArr; - newContactSolver.elasticity = Math.max(shape1.e, shape2.e); - newContactSolver.friction = Math.sqrt(shape1.u * shape2.u); + newContactSolver.elasticity = Math.max(shape1.elasticity, shape2.elasticity); + newContactSolver.friction = Math.sqrt(shape1.friction * shape2.friction); newContactSolverArr.push(newContactSolver); } } @@ -20708,8 +20771,9 @@ var Phaser; // Post solve collision callback for(var i = 0; i < this.contactSolvers.length; i++) { var arb = this.contactSolvers[i]; - this.postSolve(arb); - } + // Re-enable this + //this.postSolve(arb); + } for(var i = 0; i < this.bodyArr.length; i++) { var body = this.bodyArr[i]; if(!body) { @@ -20766,10 +20830,12 @@ var Phaser; /// /// /// + /// /// /// /// /// + /// /** * Phaser - Advanced Physics - Body * @@ -20823,24 +20889,18 @@ var Phaser; this.maskBits = 0xFFFF; this.stepCount = 0; } + Body.prototype.duplicate = function () { + 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++) + //{ + // body.addShape(this.shapes[i].duplicate()); + //} + //body.resetMassData(); + //return body; + }; Object.defineProperty(Body.prototype, "isDisabled", { - get: /* - public duplicate() { - - var body = new Body(this.type, this.transform.t, this.angle); - - for (var i = 0; i < this.shapes.length; i++) - { - body.addShape(this.shapes[i].duplicate()); - } - - body.resetMassData(); - - return body; - - } - */ - function () { + get: function () { return this.type == Phaser.Types.BODY_DISABLED ? true : false; }, enumerable: true, @@ -20882,6 +20942,7 @@ var Phaser; // Check not already part of this body shape.body = this; this.shapes.push(shape); + return shape; }; Body.prototype.removeShape = function (shape) { var index = this.shapes.indexOf(shape); @@ -20900,26 +20961,36 @@ var Phaser; }; Body.prototype.setTransform = function (pos, angle) { this.transform.setTo(pos, angle); - this.position = this.transform.transform(this.centroid); + // inject the transform into this.position + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); this.angle = angle; }; Body.prototype.syncTransform = function () { this.transform.setRotation(this.angle); + //var rotc: Phaser.Vec2 = this.transform.rotate(this.centroid); + //var sub: Phaser.Vec2 = Phaser.Vec2Utils.subtract(this.position, rotc); + //this.transform.setPosition(sub); // this.transform.setPosition(vec2.sub(this.position, this.transform.rotate(this.centroid))); - Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + //Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + // OPTIMISE: Creating new vector + Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t); }; Body.prototype.getWorldPoint = function (p) { - // This is returning a new vector - check it's actually used in that way - return this.transform.transform(p); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.transform(this.transform, p); }; Body.prototype.getWorldVector = function (v) { - return this.transform.rotate(v); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.rotate(this.transform, v); }; Body.prototype.getLocalPoint = function (p) { - return this.transform.untransform(p); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.untransform(this.transform, p); }; Body.prototype.getLocalVector = function (v) { - return this.transform.unrotate(v); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.unrotate(this.transform, v); }; Body.prototype.setFixedRotation = function (flag) { this.fixedRotation = flag; @@ -20932,7 +21003,8 @@ var Phaser; this.inertia = 0; this.inertiaInverted = 0; if(this.isDynamic == false) { - this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); return; } var totalMassCentroid = new Phaser.Vec2(0, 0); @@ -20958,7 +21030,8 @@ var Phaser; //console.log("mass = " + this.m + " inertia = " + this.i); // Move center of mass var oldPosition = Phaser.Vec2Utils.clone(this.position); - this.position = this.transform.transform(this.centroid); + //this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); // Update center of mass velocity //this.velocity.mad(vec2.perp(vec2.sub(this.position, old_p)), this.angularVelocity); oldPosition.subtract(this.position); @@ -20977,6 +21050,7 @@ var Phaser; } }; Body.prototype.cacheData = function () { + 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]; @@ -21002,8 +21076,12 @@ var Phaser; this.torque = 0; }; Body.prototype.updatePosition = function (dt) { + //console.log('body update pos', this.position.y); + //console.log('pre add temp', this._tempVec2.y); //this.position.addself(vec2.scale(this.velocity, dt)); this.position.add(Phaser.Vec2Utils.scale(this.velocity, dt, this._tempVec2)); + //console.log('post add temp', this._tempVec2.y); + //console.log('post add', this.position.y); this.angle += this.angularVelocity * dt; }; Body.prototype.resetForce = function () { @@ -21132,6 +21210,7 @@ var Phaser; var ShapePoly = (function (_super) { __extends(ShapePoly, _super); function ShapePoly(verts) { + console.log('ShapePoly created', verts); _super.call(this, Advanced.Manager.SHAPE_TYPE_POLY); this.verts = []; this.planes = []; @@ -21139,7 +21218,8 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - Phaser.Vec2Utils.clone(verts[i], this.verts[i]); + console.log('cloning vert', i); + this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; this.tplanes[i] = { }; @@ -21147,7 +21227,9 @@ var Phaser; this.tplanes[i].d = 0; } } + console.log('ShapePoly finished', this.verts); this.finishVerts(); + console.log('ShapePoly finished 2', this.verts); } ShapePoly.prototype.finishVerts = function () { if(this.verts.length < 2) { @@ -21212,12 +21294,14 @@ var Phaser; ShapePoly.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++) { - this.tverts[i] = xf.transform(this.verts[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; @@ -21327,7 +21411,13 @@ var Phaser; (function (Advanced) { var ShapeBox = (function (_super) { __extends(ShapeBox, _super); + // Give in pixels function ShapeBox(x, y, width, height) { + console.log('creating 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, [ @@ -21388,13 +21478,17 @@ var Phaser; this.b.subtract(c); }; ShapeSegment.prototype.transform = function (xf) { - this.a = xf.transform(this.a); - this.b = xf.transform(this.b); - }; + 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); + }; ShapeSegment.prototype.untransform = function (xf) { - this.a = xf.untransform(this.a); - this.b = xf.untransform(this.b); - }; + 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); + }; ShapeSegment.prototype.area = function () { return Advanced.Manager.areaForSegment(this.a, this.b, this.radius); }; @@ -21405,8 +21499,10 @@ var Phaser; return Advanced.Manager.inertiaForSegment(mass, this.a, this.b); }; ShapeSegment.prototype.cacheData = function (xf) { - this.ta = xf.transform(this.a); - this.tb = xf.transform(this.b); + 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; diff --git a/Tests/physics/body1.js b/Tests/physics/body1.js index 14b00b1c..07880cdc 100644 --- a/Tests/physics/body1.js +++ b/Tests/physics/body1.js @@ -13,20 +13,22 @@ var card; var physics; var circle; + var ground; function create() { atari = game.add.sprite(200, 100, 'atari'); //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.game = game; - 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)); - walls.addShape(new Phaser.Physics.Advanced.ShapeBox(10.04, 7.68, 0.4, 14.56)); + ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 500, 800, 20)); + //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)); + //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(10.04, 7.68, 0.4, 14.56)); walls.resetMassData(); physics.space.addBody(walls); // Add a circle - circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, 4, 4); + 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; @@ -38,7 +40,11 @@ } function update() { physics.update(); - } + atari.x = physics.metersToPixels(circle.position.x); + atari.y = physics.metersToPixels(circle.position.y); + //console.log(circle.velocity.x, circle.velocity.y); + //console.log('p', circle.position.x, circle.position.y); + } function render() { game.stage.context.fillStyle = 'rgb(255,255,0)'; game.stage.context.fillText('x: ' + circle.position.x + ' y: ' + circle.position.y, 32, 32); diff --git a/Tests/physics/body1.ts b/Tests/physics/body1.ts index 940b453f..e1be8c4b 100644 --- a/Tests/physics/body1.ts +++ b/Tests/physics/body1.ts @@ -20,6 +20,8 @@ var physics: Phaser.Physics.Advanced.Manager; var circle: Phaser.Physics.Advanced.Body; + var ground: Phaser.Physics.Advanced.ShapeBox; + function create() { atari = game.add.sprite(200, 100, 'atari'); @@ -30,17 +32,20 @@ var walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); walls.game = game; - 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)); - walls.addShape(new Phaser.Physics.Advanced.ShapeBox(10.04, 7.68, 0.4, 14.56)); + // position is in relation to the containing body! don't forget this + ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 500, 800, 20)); + + //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)); + //walls.addShape(new Phaser.Physics.Advanced.ShapeBox(10.04, 7.68, 0.4, 14.56)); walls.resetMassData(); physics.space.addBody(walls); // Add a circle - circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, 4, 4); + 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); @@ -55,7 +60,14 @@ } function update() { + physics.update(); + + atari.x = physics.metersToPixels(circle.position.x); + atari.y = physics.metersToPixels(circle.position.y); + + //console.log(circle.velocity.x, circle.velocity.y); + //console.log('p', circle.position.x, circle.position.y); } function render() { diff --git a/build/phaser.d.ts b/build/phaser.d.ts index ab8a9bcd..d2228202 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -9426,7 +9426,6 @@ module Phaser { * @return {Transform} This object **/ constructor(pos: Vec2, angle: number); - private _tempVec; public t: Vec2; public c: number; public s: number; @@ -9434,10 +9433,20 @@ module Phaser { public setRotation(angle: number): Transform; public setPosition(p: Vec2): Transform; public identity(): Transform; - public rotate(v: Vec2): Vec2; - public unrotate(v: Vec2): Vec2; - public transform(v: Vec2): Vec2; - public untransform(v: Vec2): Vec2; + } +} +/** +* Phaser - TransformUtils +* +* A collection of methods useful for manipulating and performing operations on 2D Transforms. +* +*/ +module Phaser { + class TransformUtils { + static rotate(t: Transform, v: Vec2, out?: Vec2): Vec2; + static unrotate(t: Transform, v: Vec2, out?: Vec2): Vec2; + static transform(t: Transform, v: Vec2, out?: Vec2): Vec2; + static untransform(t: Transform, v: Vec2, out?: Vec2): Vec2; } } /** @@ -9621,6 +9630,8 @@ module Phaser.Physics.Advanced { public allowSleep: bool; public warmStarting: bool; public update(): void; + public pixelsToMeters(value: number): number; + public metersToPixels(value: number): number; static pixelsToMeters(value: number): number; static metersToPixels(value: number): number; static p2m(value: number): number; @@ -9654,19 +9665,42 @@ module Phaser.Physics.Advanced { public mins: Vec2; public maxs: Vec2; public toString(): string; - public setTo(mins, maxs): void; + public setTo(mins: Vec2, maxs: Vec2): void; public copy(b: Bounds): Bounds; public clear(): Bounds; public isEmpty(): bool; public getPerimeter(): number; - public addPoint(p): Bounds; - public addBounds(b): Bounds; + public addPoint(p: Vec2): Bounds; + public addBounds(b: Bounds): Bounds; public addBounds2(mins, maxs): Bounds; - public addExtents(center, extent_x, extent_y): Bounds; - public expand(ax, ay): Bounds; - public containPoint(p): bool; - public intersectsBounds(b): bool; - static expand(b, ax, ay); + public addExtents(center: Vec2, extent_x: number, extent_y: number): Bounds; + public expand(ax: number, ay: number): Bounds; + public containPoint(p: Vec2): bool; + public intersectsBounds(b: Bounds): bool; + static expand(b: Bounds, ax, ay); + } +} +/** +* Phaser - Advanced Physics - IShape +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced { + interface IShape { + id: number; + type: number; + elasticity: number; + friction: number; + density: number; + body: Body; + bounds: Bounds; + area(): number; + centroid(): Vec2; + inertia(mass: number): number; + cacheData(xf: Transform); + pointQuery(p: Vec2): bool; + findEdgeByPoint(p: Vec2, minDist: number): number; + findVertexByPoint(p: Vec2, minDist: number): number; } } /** @@ -9679,10 +9713,12 @@ module Phaser.Physics.Advanced { constructor(type: number); public id: number; public type: number; + public body: Body; public elasticity: number; public friction: number; public density: number; public bounds: Bounds; + public findEdgeByPoint(p: Vec2, minDist: number): number; } } /** @@ -9696,8 +9732,8 @@ module Phaser.Physics.Advanced { public hash; public r1: Vec2; public r2: Vec2; - public r1_local; - public r2_local; + public r1_local: Vec2; + public r2_local: Vec2; public bounce; public emn; public emt; @@ -9730,22 +9766,22 @@ module Phaser.Physics.Advanced { * Based on the work Ju Hyung Lee started in JS PhyRus. */ module Phaser.Physics.Advanced { - class ShapeCircle extends Shape { + class ShapeCircle 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): void; - public transform(xf): void; - public untransform(xf): void; + 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; - public cacheData(xf): void; - public pointQuery(p): bool; - public findVertexByPoint(p, minDist): number; + 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; } } @@ -9829,7 +9865,7 @@ module Phaser.Physics.Advanced { public removeJoint(joint: IJoint): void; public findShapeByPoint(p, refShape); public findBodyByPoint(p, refBody: Body); - public shapeById(id); + public shapeById(id): IShape; public jointById(id): IJoint; public findVertexByPoint(p, minDist, refVertexId): number; public findEdgeByPoint(p, minDist, refEdgeId): number; @@ -9887,21 +9923,22 @@ module Phaser.Physics.Advanced { public angularDamping: number; public sleepTime: number; public awaked: bool; - public shapes: any[]; - public joints: any[]; + public shapes: IShape[]; + public joints: IJoint[]; public jointHash: {}; - public bounds; + public bounds: Bounds; public fixedRotation: bool; public categoryBits: number; public maskBits: number; public stepCount: number; public space: Space; + public duplicate(): void; public isDisabled : bool; public isStatic : bool; public isKinetic : bool; public isDynamic : bool; public setType(type: number): void; - public addShape(shape): void; + public addShape(shape); public removeShape(shape): void; public mass: number; public massInverted: number; @@ -9940,7 +9977,7 @@ module Phaser.Physics.Advanced { * Based on the work Ju Hyung Lee started in JS PhyRus. */ module Phaser.Physics.Advanced { - class ShapePoly extends Shape { + class ShapePoly extends Shape implements IShape { constructor(verts?: Vec2[]); public verts: Vec2[]; public planes; @@ -9954,11 +9991,11 @@ module Phaser.Physics.Advanced { public untransform(xf): void; public area(): number; public centroid(): Vec2; - public inertia(mass): number; - public cacheData(xf): void; - public pointQuery(p): bool; - public findVertexByPoint(p, minDist): number; - public findEdgeByPoint(p, minDist): number; + 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; @@ -9980,7 +10017,7 @@ module Phaser.Physics.Advanced { * Based on the work Ju Hyung Lee started in JS PhyRus. */ module Phaser.Physics.Advanced { - class ShapeSegment extends Shape { + class ShapeSegment extends Shape implements IShape { constructor(a, b, radius: number); public a: Vec2; public b: Vec2; @@ -9992,14 +10029,14 @@ module Phaser.Physics.Advanced { public finishVerts(): void; public duplicate(): ShapeSegment; public recenter(c): void; - public transform(xf): void; - public untransform(xf): void; + public transform(xf: Transform): void; + public untransform(xf: Transform): void; public area(): number; public centroid(): Vec2; - public inertia(mass): number; - public cacheData(xf): void; - public pointQuery(p): bool; - public findVertexByPoint(p, minDist): number; + 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; } } diff --git a/build/phaser.js b/build/phaser.js index 3b388a4f..440ad3dc 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -3913,7 +3913,7 @@ var Phaser; var Components = Phaser.Components; })(Phaser || (Phaser = {})); /// -/// +/// /** * Phaser - Vec2Utils * @@ -18060,7 +18060,6 @@ var Phaser; this.t = Phaser.Vec2Utils.clone(pos); this.c = Math.cos(angle); this.s = Math.sin(angle); - this._tempVec = new Phaser.Vec2(); } Transform.prototype.setTo = function (pos, angle) { this.t.copyFrom(pos); @@ -18083,25 +18082,45 @@ var Phaser; this.s = 0; return this; }; - Transform.prototype.rotate = function (v) { - return this._tempVec.setTo(v.x * this.c - v.y * this.s, v.x * this.s + v.y * this.c); - }; - Transform.prototype.unrotate = function (v) { - return this._tempVec.setTo(v.x * this.c + v.y * this.s, -v.x * this.s + v.y * this.c); - }; - Transform.prototype.transform = function (v) { - return this._tempVec.setTo(v.x * this.c - v.y * this.s + this.t.x, v.x * this.s + v.y * this.c + this.t.y); - }; - Transform.prototype.untransform = function (v) { - var px = v.x - this.t.x; - var py = v.y - this.t.y; - // expensive - check for alternatives - return this._tempVec.setTo(px * this.c + py * this.s, -px * this.s + py * this.c); - }; return Transform; })(); Phaser.Transform = Transform; })(Phaser || (Phaser = {})); +/// +/// +/// +/** +* Phaser - TransformUtils +* +* A collection of methods useful for manipulating and performing operations on 2D Transforms. +* +*/ +var Phaser; +(function (Phaser) { + var TransformUtils = (function () { + function TransformUtils() { } + TransformUtils.rotate = function rotate(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c - v.y * t.s, v.x * t.s + v.y * t.c); + }; + TransformUtils.unrotate = function unrotate(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c + v.y * t.s, -v.x * t.s + v.y * t.c); + }; + TransformUtils.transform = function transform(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + return out.setTo(v.x * t.c - v.y * t.s + t.t.x, v.x * t.s + v.y * t.c + t.t.y); + }; + TransformUtils.untransform = function untransform(t, v, out) { + if (typeof out === "undefined") { out = new Phaser.Vec2(); } + var px = v.x - t.t.x; + var py = v.y - t.t.y; + return out.setTo(px * t.c + py * t.s, -px * t.s + py * t.c); + }; + return TransformUtils; + })(); + Phaser.TransformUtils = TransformUtils; +})(Phaser || (Phaser = {})); var Phaser; (function (Phaser) { /// @@ -19112,7 +19131,7 @@ var Phaser; (function (Advanced) { var Manager = (function () { function Manager(game) { - this.lastTime = 0; + this.lastTime = Date.now(); this.frameRateHz = 60; this.timeDelta = 0; this.paused = false; @@ -19186,6 +19205,12 @@ var Phaser; } //frameCount++; }; + Manager.prototype.pixelsToMeters = function (value) { + return value * 0.02; + }; + Manager.prototype.metersToPixels = function (value) { + return value * 50; + }; Manager.pixelsToMeters = function pixelsToMeters(value) { return value * 0.02; }; @@ -19454,6 +19479,9 @@ 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) { @@ -19470,6 +19498,12 @@ var Phaser; var Physics = Phaser.Physics; })(Phaser || (Phaser = {})); var Phaser; +(function (Phaser) { + (function (Physics) { + })(Phaser.Physics || (Phaser.Physics = {})); + var Physics = Phaser.Physics; +})(Phaser || (Phaser = {})); +var Phaser; (function (Phaser) { (function (Physics) { /// @@ -19478,6 +19512,7 @@ var Phaser; /// /// /// + /// /** * Phaser - Advanced Physics - Shape * @@ -19493,6 +19528,10 @@ var Phaser; this.density = 1; this.bounds = new Advanced.Bounds(); } + Shape.prototype.findEdgeByPoint = // Over-ridden by ShapePoly + function (p, minDist) { + return -1; + }; return Shape; })(); Advanced.Shape = Shape; @@ -19524,6 +19563,10 @@ var Phaser; this.depth = d; this.lambdaNormal = 0; this.lambdaTangential = 0; + this.r1 = new Phaser.Vec2(); + this.r2 = new Phaser.Vec2(); + this.r1_local = new Phaser.Vec2(); + this.r2_local = new Phaser.Vec2(); } return Contact; })(); @@ -19570,6 +19613,7 @@ var Phaser; (function (Advanced) { var ContactSolver = (function () { function ContactSolver(shape1, shape2) { + console.log('ContactSolver super'); this.shape1 = shape1; this.shape2 = shape2; this.contacts = []; @@ -19599,14 +19643,18 @@ 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); // Transformed r1, r2 Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1); Phaser.Vec2Utils.subtract(con.point, body2.position, con.r2); //con.r1 = vec2.sub(con.point, body1.p); //con.r2 = vec2.sub(con.point, body2.p); // Local r1, r2 - con.r1_local = body1.transform.unrotate(con.r1); - con.r2_local = body2.transform.unrotate(con.r2); + Phaser.TransformUtils.unrotate(body1.transform, con.r1, con.r1_local); + Phaser.TransformUtils.unrotate(body2.transform, con.r2, con.r2_local); + //con.r1_local = body1.transform.unrotate(con.r1); + //con.r2_local = body2.transform.unrotate(con.r2); var n = con.normal; var t = Phaser.Vec2Utils.perp(con.normal); // invEMn = J * invM * JT @@ -19635,6 +19683,9 @@ 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 () { @@ -19817,11 +19868,13 @@ var Phaser; this.center.subtract(c); }; ShapeCircle.prototype.transform = function (xf) { - this.center = xf.transform(this.center); - }; + Phaser.TransformUtils.transform(xf, this.center, this.center); + //this.center = xf.transform(this.center); + }; ShapeCircle.prototype.untransform = function (xf) { - this.center = xf.untransform(this.center); - }; + Phaser.TransformUtils.untransform(xf, this.center, this.center); + //this.center = xf.untransform(this.center); + }; ShapeCircle.prototype.area = function () { return Advanced.Manager.areaForCircle(this.radius, 0); }; @@ -19832,7 +19885,8 @@ var Phaser; return Advanced.Manager.inertiaForCircle(mass, this.center, this.radius, 0); }; ShapeCircle.prototype.cacheData = function (xf) { - this.tc = xf.transform(this.center); + 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); }; @@ -19879,6 +19933,7 @@ var Phaser; function Collision() { } Collision.prototype.collide = function (a, b, contacts) { + 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) { @@ -20268,7 +20323,7 @@ var Phaser; this.numContacts = 0; this.contactSolvers = []; //this.postSolve(arb) { }; - this.gravity = new Phaser.Vec2(); + this.gravity = new Phaser.Vec2(0, 10); this.damping = 0; } Space.TIME_TO_SLEEP = 0.5; @@ -20296,6 +20351,7 @@ var Phaser; if(this.bodyHash[body.id] != undefined) { return; } + console.log('Body added to space', body.name); var index = this.bodyArr.push(body) - 1; this.bodyHash[body.id] = index; body.awake(true); @@ -20514,34 +20570,41 @@ var Phaser; return null; }; Space.prototype.genTemporalContactSolvers = function () { + console.log('genTemporalContactSolvers'); //var t0 = Date.now(); var newContactSolverArr = []; this.numContacts = 0; for(var body1_index = 0; body1_index < this.bodyArr.length; body1_index++) { var body1 = this.bodyArr[body1_index]; + //console.log('body1', body1_index, body1.type); if(!body1) { continue; } body1.stepCount = this.stepCount; for(var body2_index = 0; body2_index < this.bodyArr.length; body2_index++) { var body2 = this.bodyArr[body2_index]; + //console.log('body2', body2_index, body2.type); if(!body2) { continue; } if(body1.stepCount == body2.stepCount) { continue; } + //console.log('step'); var active1 = body1.isAwake && !body1.isStatic; var active2 = body2.isAwake && !body2.isStatic; if(!active1 && !active2) { continue; } + //console.log('active'); if(!body1.isCollidable(body2)) { continue; } + //console.log('collideable'); if(!body1.bounds.intersectsBounds(body2.bounds)) { continue; } + 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]; @@ -20565,8 +20628,8 @@ var Phaser; body2.awake(true); var newContactSolver = new Advanced.ContactSolver(shape1, shape2); newContactSolver.contacts = contactArr; - newContactSolver.elasticity = Math.max(shape1.e, shape2.e); - newContactSolver.friction = Math.sqrt(shape1.u * shape2.u); + newContactSolver.elasticity = Math.max(shape1.elasticity, shape2.elasticity); + newContactSolver.friction = Math.sqrt(shape1.friction * shape2.friction); newContactSolverArr.push(newContactSolver); } } @@ -20708,8 +20771,9 @@ var Phaser; // Post solve collision callback for(var i = 0; i < this.contactSolvers.length; i++) { var arb = this.contactSolvers[i]; - this.postSolve(arb); - } + // Re-enable this + //this.postSolve(arb); + } for(var i = 0; i < this.bodyArr.length; i++) { var body = this.bodyArr[i]; if(!body) { @@ -20766,10 +20830,12 @@ var Phaser; /// /// /// + /// /// /// /// /// + /// /** * Phaser - Advanced Physics - Body * @@ -20823,24 +20889,18 @@ var Phaser; this.maskBits = 0xFFFF; this.stepCount = 0; } + Body.prototype.duplicate = function () { + 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++) + //{ + // body.addShape(this.shapes[i].duplicate()); + //} + //body.resetMassData(); + //return body; + }; Object.defineProperty(Body.prototype, "isDisabled", { - get: /* - public duplicate() { - - var body = new Body(this.type, this.transform.t, this.angle); - - for (var i = 0; i < this.shapes.length; i++) - { - body.addShape(this.shapes[i].duplicate()); - } - - body.resetMassData(); - - return body; - - } - */ - function () { + get: function () { return this.type == Phaser.Types.BODY_DISABLED ? true : false; }, enumerable: true, @@ -20882,6 +20942,7 @@ var Phaser; // Check not already part of this body shape.body = this; this.shapes.push(shape); + return shape; }; Body.prototype.removeShape = function (shape) { var index = this.shapes.indexOf(shape); @@ -20900,26 +20961,36 @@ var Phaser; }; Body.prototype.setTransform = function (pos, angle) { this.transform.setTo(pos, angle); - this.position = this.transform.transform(this.centroid); + // inject the transform into this.position + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); this.angle = angle; }; Body.prototype.syncTransform = function () { this.transform.setRotation(this.angle); + //var rotc: Phaser.Vec2 = this.transform.rotate(this.centroid); + //var sub: Phaser.Vec2 = Phaser.Vec2Utils.subtract(this.position, rotc); + //this.transform.setPosition(sub); // this.transform.setPosition(vec2.sub(this.position, this.transform.rotate(this.centroid))); - Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + //Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t); + // OPTIMISE: Creating new vector + Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t); }; Body.prototype.getWorldPoint = function (p) { - // This is returning a new vector - check it's actually used in that way - return this.transform.transform(p); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.transform(this.transform, p); }; Body.prototype.getWorldVector = function (v) { - return this.transform.rotate(v); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.rotate(this.transform, v); }; Body.prototype.getLocalPoint = function (p) { - return this.transform.untransform(p); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.untransform(this.transform, p); }; Body.prototype.getLocalVector = function (v) { - return this.transform.unrotate(v); + // OPTIMISE: Creating new vector + return Phaser.TransformUtils.unrotate(this.transform, v); }; Body.prototype.setFixedRotation = function (flag) { this.fixedRotation = flag; @@ -20932,7 +21003,8 @@ var Phaser; this.inertia = 0; this.inertiaInverted = 0; if(this.isDynamic == false) { - this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); + //this.position.copyFrom(this.transform.transform(this.centroid)); return; } var totalMassCentroid = new Phaser.Vec2(0, 0); @@ -20958,7 +21030,8 @@ var Phaser; //console.log("mass = " + this.m + " inertia = " + this.i); // Move center of mass var oldPosition = Phaser.Vec2Utils.clone(this.position); - this.position = this.transform.transform(this.centroid); + //this.position.copyFrom(this.transform.transform(this.centroid)); + Phaser.TransformUtils.transform(this.transform, this.centroid, this.position); // Update center of mass velocity //this.velocity.mad(vec2.perp(vec2.sub(this.position, old_p)), this.angularVelocity); oldPosition.subtract(this.position); @@ -20977,6 +21050,7 @@ var Phaser; } }; Body.prototype.cacheData = function () { + 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]; @@ -21002,8 +21076,12 @@ var Phaser; this.torque = 0; }; Body.prototype.updatePosition = function (dt) { + //console.log('body update pos', this.position.y); + //console.log('pre add temp', this._tempVec2.y); //this.position.addself(vec2.scale(this.velocity, dt)); this.position.add(Phaser.Vec2Utils.scale(this.velocity, dt, this._tempVec2)); + //console.log('post add temp', this._tempVec2.y); + //console.log('post add', this.position.y); this.angle += this.angularVelocity * dt; }; Body.prototype.resetForce = function () { @@ -21132,6 +21210,7 @@ var Phaser; var ShapePoly = (function (_super) { __extends(ShapePoly, _super); function ShapePoly(verts) { + console.log('ShapePoly created', verts); _super.call(this, Advanced.Manager.SHAPE_TYPE_POLY); this.verts = []; this.planes = []; @@ -21139,7 +21218,8 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - Phaser.Vec2Utils.clone(verts[i], this.verts[i]); + console.log('cloning vert', i); + this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); this.tverts[i] = this.verts[i]; this.tplanes[i] = { }; @@ -21147,7 +21227,9 @@ var Phaser; this.tplanes[i].d = 0; } } + console.log('ShapePoly finished', this.verts); this.finishVerts(); + console.log('ShapePoly finished 2', this.verts); } ShapePoly.prototype.finishVerts = function () { if(this.verts.length < 2) { @@ -21212,12 +21294,14 @@ var Phaser; ShapePoly.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++) { - this.tverts[i] = xf.transform(this.verts[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; @@ -21327,7 +21411,13 @@ var Phaser; (function (Advanced) { var ShapeBox = (function (_super) { __extends(ShapeBox, _super); + // Give in pixels function ShapeBox(x, y, width, height) { + console.log('creating 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, [ @@ -21388,13 +21478,17 @@ var Phaser; this.b.subtract(c); }; ShapeSegment.prototype.transform = function (xf) { - this.a = xf.transform(this.a); - this.b = xf.transform(this.b); - }; + 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); + }; ShapeSegment.prototype.untransform = function (xf) { - this.a = xf.untransform(this.a); - this.b = xf.untransform(this.b); - }; + 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); + }; ShapeSegment.prototype.area = function () { return Advanced.Manager.areaForSegment(this.a, this.b, this.radius); }; @@ -21405,8 +21499,10 @@ var Phaser; return Advanced.Manager.inertiaForSegment(mass, this.a, this.b); }; ShapeSegment.prototype.cacheData = function (xf) { - this.ta = xf.transform(this.a); - this.tb = xf.transform(this.b); + 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;