From 829a1b00e4bed722e3b62435a24316c0f2ee5d8e Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 18 Jun 2013 19:37:09 +0100 Subject: [PATCH] Brand new physics system finally (mostly) working. Some poly issues to resolve, but it's running well. --- Phaser/input/Input.ts | 25 ++ Phaser/physics/advanced/Body.ts | 77 +++++- Phaser/physics/advanced/Bounds.ts | 16 ++ Phaser/physics/advanced/Space.ts | 1 + Phaser/physics/advanced/shapes/Box.ts | 8 +- Phaser/physics/advanced/shapes/Circle.ts | 4 + Phaser/physics/advanced/shapes/Poly.ts | 9 +- Phaser/physics/advanced/shapes/Triangle.ts | 11 +- Tests/assets/sprites/cokecan.png | Bin 0 -> 1381 bytes Tests/phaser.js | 292 +++++++++++++++------ Tests/physics/body1.js | 62 ++++- Tests/physics/body1.ts | 49 ++-- build/phaser.d.ts | 70 +++-- build/phaser.js | 292 +++++++++++++++------ 14 files changed, 684 insertions(+), 232 deletions(-) create mode 100644 Tests/assets/sprites/cokecan.png diff --git a/Phaser/input/Input.ts b/Phaser/input/Input.ts index 32ed80cd..26231fee 100644 --- a/Phaser/input/Input.ts +++ b/Phaser/input/Input.ts @@ -53,6 +53,11 @@ module Phaser { this.activePointer = this.mousePointer; this.currentPointers = 0; + this.hitCanvas = document.createElement('canvas'); + this.hitCanvas.width = 1; + this.hitCanvas.height = 1; + this.hitContext = this.hitCanvas.getContext('2d'); + } /** @@ -60,6 +65,18 @@ module Phaser { */ private _game: Game; + /** + * A 1x1 sized canvas used for pixel-perfect checks + * @type {HTMLCanvasElement} + */ + public hitCanvas: HTMLCanvasElement; + + /** + * The context of the 1x1 pixel check canvas + * @type {CanvasRenderingContext2D} + */ + public hitContext: CanvasRenderingContext2D; + /** * A vector object representing the previous position of the Pointer. * @property vector @@ -961,6 +978,14 @@ module Phaser { return Vec2Utils.angle(pointer1.position, pointer2.position); } + public pixelPerfectCheck(sprite: Phaser.Sprite, pointer: Phaser.Pointer, alpha: number = 255): bool { + + this.hitContext.clearRect(0, 0, 1, 1); + + return true; + + } + } } \ No newline at end of file diff --git a/Phaser/physics/advanced/Body.ts b/Phaser/physics/advanced/Body.ts index a6cdf4c1..fff4d13e 100644 --- a/Phaser/physics/advanced/Body.ts +++ b/Phaser/physics/advanced/Body.ts @@ -8,6 +8,11 @@ /// /// /// +/// +/// +/// +/// +/// /** * Phaser - Advanced Physics - Body @@ -29,12 +34,12 @@ module Phaser.Physics.Advanced { { this.sprite = sprite; this.game = sprite.game; - this.position = new Phaser.Vec2(sprite.x, sprite.y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.y)); this.angle = sprite.rotation; } else { - this.position = new Phaser.Vec2(x, y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(x), Phaser.Physics.Advanced.Manager.pixelsToMeters(y)); this.angle = 0; } @@ -64,6 +69,8 @@ module Phaser.Physics.Advanced { } + private _tempVec2: Phaser.Vec2 = new Phaser.Vec2; + /** * Reference to Phaser.Game */ @@ -139,6 +146,11 @@ module Phaser.Physics.Advanced { // Bounds of all shapes public bounds: Bounds; + public mass: number; + public massInverted: number; + public inertia: number; + public inertiaInverted: number; + public fixedRotation = false; public categoryBits = 0x0001; public maskBits = 0xFFFF; @@ -195,6 +207,62 @@ module Phaser.Physics.Advanced { } + public addPoly(verts, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Poly { + + var poly: Phaser.Physics.Advanced.Shapes.Poly = new Phaser.Physics.Advanced.Shapes.Poly(verts); + poly.elasticity = elasticity; + poly.friction = friction; + poly.density = density; + + this.addShape(poly); + this.resetMassData(); + + return poly; + + } + + public addTriangle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Triangle { + + var tri: Phaser.Physics.Advanced.Shapes.Triangle = new Phaser.Physics.Advanced.Shapes.Triangle(x1, y1, x2, y2, x3, y3); + tri.elasticity = elasticity; + tri.friction = friction; + tri.density = density; + + this.addShape(tri); + this.resetMassData(); + + return tri; + + } + + public addBox(x: number, y: number, width: number, height: number, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Box { + + var box: Phaser.Physics.Advanced.Shapes.Box = new Phaser.Physics.Advanced.Shapes.Box(x, y, width, height); + box.elasticity = elasticity; + box.friction = friction; + box.density = density; + + this.addShape(box); + this.resetMassData(); + + return box; + + } + + public addCircle(radius: number, x?: number = 0, y?: number = 0, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Circle { + + var circle: Phaser.Physics.Advanced.Shapes.Circle = new Phaser.Physics.Advanced.Shapes.Circle(radius, x, y); + circle.elasticity = elasticity; + circle.friction = friction; + circle.density = density; + + this.addShape(circle); + this.resetMassData(); + + return circle; + + } + public addShape(shape) { // Check not already part of this body @@ -218,10 +286,6 @@ module Phaser.Physics.Advanced { } - public mass: number; - public massInverted: number; - public inertia: number; - public inertiaInverted: number; private setMass(mass) { @@ -381,7 +445,6 @@ module Phaser.Physics.Advanced { } - private _tempVec2: Phaser.Vec2 = new Phaser.Vec2; public updateVelocity(gravity, dt, damping) { diff --git a/Phaser/physics/advanced/Bounds.ts b/Phaser/physics/advanced/Bounds.ts index 6e7ce285..b5eb34f8 100644 --- a/Phaser/physics/advanced/Bounds.ts +++ b/Phaser/physics/advanced/Bounds.ts @@ -71,6 +71,22 @@ module Phaser.Physics.Advanced { } + public get x(): number { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.x); + } + + public get y(): number { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.y); + } + + public get width(): number { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.x - this.mins.x); + } + + public get height(): number { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.y - this.mins.y); + } + public isEmpty(): bool { return (this.mins.x > this.maxs.x || this.mins.y > this.maxs.y); } diff --git a/Phaser/physics/advanced/Space.ts b/Phaser/physics/advanced/Space.ts index 52e09a5c..5bab1961 100644 --- a/Phaser/physics/advanced/Space.ts +++ b/Phaser/physics/advanced/Space.ts @@ -560,6 +560,7 @@ module Phaser.Physics.Advanced { //stats.timeCollision = Date.now() - t0; return newContactSolverArr; + } public initSolver(dt, dt_inv, warmStarting) { diff --git a/Phaser/physics/advanced/shapes/Box.ts b/Phaser/physics/advanced/shapes/Box.ts index 3159e317..5cbf2d95 100644 --- a/Phaser/physics/advanced/shapes/Box.ts +++ b/Phaser/physics/advanced/shapes/Box.ts @@ -26,10 +26,10 @@ module Phaser.Physics.Advanced.Shapes { 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) + { x: -hw + x, y: +hh + y }, + { x: -hw + x, y: -hh + y }, + { x: +hw + x, y: -hh + y }, + { x: +hw + x, y: +hh + y } ]); } diff --git a/Phaser/physics/advanced/shapes/Circle.ts b/Phaser/physics/advanced/shapes/Circle.ts index 6911f118..0f12673b 100644 --- a/Phaser/physics/advanced/shapes/Circle.ts +++ b/Phaser/physics/advanced/shapes/Circle.ts @@ -18,6 +18,10 @@ module Phaser.Physics.Advanced.Shapes { super(Manager.SHAPE_TYPE_CIRCLE); + x = Manager.pixelsToMeters(x); + y = Manager.pixelsToMeters(y); + radius = Manager.pixelsToMeters(radius); + this.center = new Phaser.Vec2(x, y); this.radius = radius; this.tc = new Phaser.Vec2; diff --git a/Phaser/physics/advanced/shapes/Poly.ts b/Phaser/physics/advanced/shapes/Poly.ts index f4d50699..f4c85523 100644 --- a/Phaser/physics/advanced/shapes/Poly.ts +++ b/Phaser/physics/advanced/shapes/Poly.ts @@ -14,7 +14,9 @@ module Phaser.Physics.Advanced.Shapes { export class Poly extends Phaser.Physics.Advanced.Shape implements IShape { - constructor(verts?:Phaser.Vec2[]) { + // Verts is an optional array of objects, the objects must have public x and y properties which will be used + // to seed this polygon (i.e. Vec2 objects, or just straight JS objects) and must wind COUNTER clockwise + constructor(verts?) { super(Manager.SHAPE_TYPE_POLY); @@ -28,7 +30,7 @@ module Phaser.Physics.Advanced.Shapes { { for (var i = 0; i < verts.length; i++) { - this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); + this.verts[i] = new Phaser.Vec2(verts[i].x, verts[i].y); this.tverts[i] = this.verts[i]; this.tplanes[i] = {}; @@ -91,6 +93,7 @@ module Phaser.Physics.Advanced.Shapes { this.convexity = false; } } + } public duplicate() { @@ -138,7 +141,7 @@ module Phaser.Physics.Advanced.Shapes { var numVerts = this.verts.length; - //console.log('shapePoly cacheData', numVerts); + console.log('Poly cacheData', numVerts, this.body.name); if (numVerts == 0) { diff --git a/Phaser/physics/advanced/shapes/Triangle.ts b/Phaser/physics/advanced/shapes/Triangle.ts index 90147ca8..7b88e013 100644 --- a/Phaser/physics/advanced/shapes/Triangle.ts +++ b/Phaser/physics/advanced/shapes/Triangle.ts @@ -14,9 +14,16 @@ module Phaser.Physics.Advanced.Shapes { export class Triangle extends Phaser.Physics.Advanced.Shapes.Poly { - constructor(p1, p2, p3) { + constructor(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) { - super( [ new Phaser.Vec2(p1.x, p1.y), new Phaser.Vec2(p2.x, p2.y), new Phaser.Vec2(p3.x, p3.y) ] ); + x1 = Manager.pixelsToMeters(x1); + y1 = Manager.pixelsToMeters(y1); + x2 = Manager.pixelsToMeters(x2); + y2 = Manager.pixelsToMeters(y2); + x3 = Manager.pixelsToMeters(x3); + y3 = Manager.pixelsToMeters(y3); + + super([{ x: x1, y: y1 }, { x: x2, y: y2 }, { x: x3, y: y3 }]); } diff --git a/Tests/assets/sprites/cokecan.png b/Tests/assets/sprites/cokecan.png new file mode 100644 index 0000000000000000000000000000000000000000..64ab2678c72f34c612bf991531daf64f48563505 GIT binary patch literal 1381 zcmV-r1)BPaP)wDuWols^`7FXrlg&2VA2YLRdtaJ<4FZ+0)Ce1CQBg!8Xf5(2(v+}g z6NW_fr^39%g)oQ5^X^YZ2}_BvS0#%hO$kedF!C~MMbfEjnG&`YVgDOhzFMY)Y4WWX zEGhe}+NFf0Mc7(Zfx+^8n-gFvvY~g15*8~C^VO~qNylT_SD(I^+Z+?d&$wx3e1-DBW=&CohOl7OJ4+SWz#j!M`<8`Z=9iNDAzK5}HMRZrjz z89>62Jch8=ld!X(VN%^s3HvBve=hFd4UBWltNk=7{oCaFz1eU24;8?a5|)aW9K~N$ zDJ|yXEh4Kko%4?N;co|hT)T*@4Xk|a2LJ1v_4PAlHp_+M%;(PcWvey)RR|ldY{AsD zg_=B(b?;$2OF#h`g7kf|lk?hKyOql+=r~07TXwqxeq293WN920Ez&1~Y%`lnQP1U0 zD^Rmyn?!UM8r!pXR)cY=QNp6+l7m~aj8~f0Ana+LcVT)a1&Z~gdh_eb=iX7KMy*h;zNtkLtw z3lmZQyng1iMk0&qXAMIzksYd$)ZWGIcP>tzH&ml>*i#5Q589H?YK^Z}4b={(8GD9c zi7d%`4}|U9QwFRk(=w2yaoE;`jR^`Ht4ejNnyIkvJ;~*AQ%<|6{xymUFtdTUQwe(( zVfmmf^Q09_j>i#3#%WooR*Hmy>3qq}b4f7l_wPep>eiDb5+psLhV4>4);B}AFl2|0 zN>akMCXBOpB#I-8(au)TKI`d?cW%GS{k-(uJ4q9U5x#HKHH_o()H11Fio~IpjyxMc z35%{_Uwyv_bQA-wT zmba$4<5r+qCFHILB`k5mFv8EJp8x3EIz5SOxd#I0^NY}Q-s-y%`MmgbV7bDvq; nEhLU3d81$TQ^F$IZvh4X4NiHgoGZRt00000NkvXXu0mjf`d62e literal 0 HcmV?d00001 diff --git a/Tests/phaser.js b/Tests/phaser.js index 0f9b2dab..70ddf6c5 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -16057,6 +16057,10 @@ var Phaser; this.camera = this._game.camera; this.activePointer = this.mousePointer; this.currentPointers = 0; + this.hitCanvas = document.createElement('canvas'); + this.hitCanvas.width = 1; + this.hitCanvas.height = 1; + this.hitContext = this.hitCanvas.getContext('2d'); } Input.MOUSE_OVERRIDES_TOUCH = 0; Input.TOUCH_OVERRIDES_MOUSE = 1; @@ -16495,7 +16499,16 @@ var Phaser; return Input; })(); Phaser.Input = Input; -})(Phaser || (Phaser = {})); + /* + public pixelPerfectCheck(sprite: Phaser.Sprite, pointer: Phaser.Pointer, alpha: number = 255): bool { + + this.hitContext.clearRect(0, 0, 1, 1); + + return true; + + } + */ + })(Phaser || (Phaser = {})); /// /// /// @@ -19387,6 +19400,34 @@ var Phaser; this.maxs.setTo(-999999, -999999); return this; }; + Object.defineProperty(Bounds.prototype, "x", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.x); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "y", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.y); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "width", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.x - this.mins.x); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "height", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.y - this.mins.y); + }, + enumerable: true, + configurable: true + }); Bounds.prototype.isEmpty = function () { return (this.mins.x > this.maxs.x || this.mins.y > this.maxs.y); }; @@ -19845,6 +19886,9 @@ var Phaser; if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } _super.call(this, Advanced.Manager.SHAPE_TYPE_CIRCLE); + x = Advanced.Manager.pixelsToMeters(x); + y = Advanced.Manager.pixelsToMeters(y); + radius = Advanced.Manager.pixelsToMeters(radius); this.center = new Phaser.Vec2(x, y); this.radius = radius; this.tc = new Phaser.Vec2(); @@ -19923,6 +19967,8 @@ var Phaser; (function (Shapes) { var Poly = (function (_super) { __extends(Poly, _super); + // Verts is an optional array of objects, the objects must have public x and y properties which will be used + // to seed this polygon (i.e. Vec2 objects, or just straight JS objects) and must wind COUNTER clockwise function Poly(verts) { _super.call(this, Advanced.Manager.SHAPE_TYPE_POLY); this.verts = []; @@ -19931,7 +19977,7 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); + this.verts[i] = new Phaser.Vec2(verts[i].x, verts[i].y); this.tverts[i] = this.verts[i]; this.tplanes[i] = { }; @@ -20004,7 +20050,7 @@ var Phaser; Poly.prototype.cacheData = function (xf) { this.bounds.clear(); var numVerts = this.verts.length; - //console.log('shapePoly cacheData', numVerts); + console.log('Poly cacheData', numVerts, this.body.name); if(numVerts == 0) { return; } @@ -21156,6 +21202,109 @@ var Phaser; var Physics = Phaser.Physics; })(Phaser || (Phaser = {})); var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shapes - Triangle + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Triangle = (function (_super) { + __extends(Triangle, _super); + function Triangle(x1, y1, x2, y2, x3, y3) { + x1 = Advanced.Manager.pixelsToMeters(x1); + y1 = Advanced.Manager.pixelsToMeters(y1); + x2 = Advanced.Manager.pixelsToMeters(x2); + y2 = Advanced.Manager.pixelsToMeters(y2); + x3 = Advanced.Manager.pixelsToMeters(x3); + y3 = Advanced.Manager.pixelsToMeters(y3); + _super.call(this, [ + { + x: x1, + y: y1 + }, + { + x: x2, + y: y2 + }, + { + x: x3, + y: y3 + } + ]); + } + return Triangle; + })(Phaser.Physics.Advanced.Shapes.Poly); + Shapes.Triangle = Triangle; + })(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 - 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, [ + { + x: -hw + x, + y: +hh + y + }, + { + x: -hw + x, + y: -hh + y + }, + { + x: +hw + x, + y: -hh + y + }, + { + x: +hw + x, + y: +hh + y + } + ]); + } + return Box; + })(Phaser.Physics.Advanced.Shapes.Poly); + 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) { /// @@ -21168,6 +21317,11 @@ var Phaser; /// /// /// + /// + /// + /// + /// + /// /** * Phaser - Advanced Physics - Body * @@ -21178,6 +21332,7 @@ var Phaser; function Body(sprite, type, x, y) { if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } + this._tempVec2 = new Phaser.Vec2(); // Shapes this.shapes = []; // Joints @@ -21188,17 +21343,16 @@ var Phaser; this.categoryBits = 0x0001; this.maskBits = 0xFFFF; this.stepCount = 0; - this._tempVec2 = new Phaser.Vec2(); this.id = Phaser.Physics.Advanced.Manager.bodyCounter++; this.name = 'body' + this.id; this.type = type; if(sprite) { this.sprite = sprite; this.game = sprite.game; - this.position = new Phaser.Vec2(sprite.x, sprite.y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.y)); this.angle = sprite.rotation; } else { - this.position = new Phaser.Vec2(x, y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(x), Phaser.Physics.Advanced.Manager.pixelsToMeters(y)); this.angle = 0; } this.transform = new Phaser.Transform(this.position, this.angle); @@ -21270,6 +21424,56 @@ var Phaser; this.type = type; this.awake(true); }; + Body.prototype.addPoly = function (verts, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var poly = new Phaser.Physics.Advanced.Shapes.Poly(verts); + poly.elasticity = elasticity; + poly.friction = friction; + poly.density = density; + this.addShape(poly); + this.resetMassData(); + return poly; + }; + Body.prototype.addTriangle = function (x1, y1, x2, y2, x3, y3, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var tri = new Phaser.Physics.Advanced.Shapes.Triangle(x1, y1, x2, y2, x3, y3); + tri.elasticity = elasticity; + tri.friction = friction; + tri.density = density; + this.addShape(tri); + this.resetMassData(); + return tri; + }; + Body.prototype.addBox = function (x, y, width, height, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var box = new Phaser.Physics.Advanced.Shapes.Box(x, y, width, height); + box.elasticity = elasticity; + box.friction = friction; + box.density = density; + this.addShape(box); + this.resetMassData(); + return box; + }; + Body.prototype.addCircle = function (radius, x, y, elasticity, friction, density) { + if (typeof x === "undefined") { x = 0; } + if (typeof y === "undefined") { y = 0; } + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var circle = new Phaser.Physics.Advanced.Shapes.Circle(radius, x, y); + circle.elasticity = elasticity; + circle.friction = friction; + circle.density = density; + this.addShape(circle); + this.resetMassData(); + return circle; + }; Body.prototype.addShape = function (shape) { // Check not already part of this body shape.body = this; @@ -21524,82 +21728,6 @@ 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.Shapes.Poly); - 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 - Shapes - Triangle - * - * Based on the work Ju Hyung Lee started in JS PhyRus. - */ - (function (Shapes) { - var Triangle = (function (_super) { - __extends(Triangle, _super); - function Triangle(p1, p2, p3) { - _super.call(this, [ - new Phaser.Vec2(p1.x, p1.y), - new Phaser.Vec2(p2.x, p2.y), - new Phaser.Vec2(p3.x, p3.y) - ]); - } - return Triangle; - })(Phaser.Physics.Advanced.Shapes.Poly); - Shapes.Triangle = Triangle; - })(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 = {})); /// /// /// diff --git a/Tests/physics/body1.js b/Tests/physics/body1.js index f8f99706..1af4c4ea 100644 --- a/Tests/physics/body1.js +++ b/Tests/physics/body1.js @@ -15,32 +15,63 @@ var physics; var circle; var walls; - var ground; + var t; function create() { atari = game.add.sprite(200, 100, 'atari'); + // need to get the physics bounds around the sprite center, regardless of origin atari.transform.origin.setTo(0.5, 0.5); //card = game.add.sprite(500, 300, 'card'); physics = new Phaser.Physics.Advanced.Manager(game); - walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); + walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_KINETIC); walls.game = game; + walls.addBox(500, 500, 500, 20); + walls.addBox(100, 250, 250, 20); + walls.transform.setRotation(game.math.degreesToRadians(3)); + //walls.fixedRotation = true; // position is in relation to the containing body! don't forget this - ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 20)); + //ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 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(); + //walls.resetMassData(); physics.space.addBody(walls); // Add a circle - circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, physics.pixelsToMeters(300), physics.pixelsToMeters(200)); + circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, 100, 200); circle.game = game; - var shape = new Phaser.Physics.Advanced.Shapes.Circle(0.4, 0, 0); - shape.elasticity = 0.8; - shape.friction = 1; - shape.density = 1; - circle.addShape(shape); - circle.resetMassData(); + circle.addCircle(32, 0, 0, 0.8); physics.space.addBody(circle); + t = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_KINETIC, 500, 400); + t.game = game; + //t.addCircle(32, 0, 0, 0.8); + //t.addTriangle(0, 0, 1, 1, 2, 2); + t.addPoly([ + { + x: -0.8, + y: 0.48 + }, + { + x: -0.8, + y: 0 + }, + { + x: 0.8, + y: 0 + }, + { + x: 0.8, + y: 0.32 + }, + { + x: 0, + y: 0.84 + }, + { + x: -0.56, + y: 0.84 + } + ], 0.5, 1, 6); + physics.space.addBody(t); } function update() { physics.update(); @@ -71,6 +102,10 @@ //console.log(circle.velocity.x, circle.velocity.y); //console.log('p', circle.position.x, circle.position.y); } + function renderBounds(body) { + game.stage.context.fillStyle = 'rgba(0,255,200,0.2)'; + game.stage.context.fillRect(body.bounds.x, body.bounds.y, body.bounds.width, body.bounds.height); + } 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); @@ -99,6 +134,11 @@ 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]); + renderBounds(circle); drawPolygon(game.stage.context, walls.shapes[0], 1, 'rgb(0,255,255)'); + drawPolygon(game.stage.context, walls.shapes[1], 1, 'rgb(0,255,255)'); + //renderCircle(t.shapes[0]); + drawPolygon(game.stage.context, t.shapes[0], 1, 'rgb(255,255,255)'); + renderBounds(t); } })(); diff --git a/Tests/physics/body1.ts b/Tests/physics/body1.ts index 2ab49637..6f43f4db 100644 --- a/Tests/physics/body1.ts +++ b/Tests/physics/body1.ts @@ -21,45 +21,50 @@ var physics: Phaser.Physics.Advanced.Manager; var circle: Phaser.Physics.Advanced.Body; var walls: Phaser.Physics.Advanced.Body; - - var ground: Phaser.Physics.Advanced.Shapes.Box; + var t: Phaser.Physics.Advanced.Body; function create() { atari = game.add.sprite(200, 100, 'atari'); + + // need to get the physics bounds around the sprite center, regardless of origin atari.transform.origin.setTo(0.5, 0.5); //card = game.add.sprite(500, 300, 'card'); physics = new Phaser.Physics.Advanced.Manager(game); - walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC); + walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_KINETIC); walls.game = game; + walls.addBox(500, 500, 500, 20); + walls.addBox(100, 250, 250, 20); + walls.transform.setRotation(game.math.degreesToRadians(3)); + //walls.fixedRotation = true; + // position is in relation to the containing body! don't forget this - ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 20)); + //ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 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(); + //walls.resetMassData(); physics.space.addBody(walls); // Add a circle - - circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, physics.pixelsToMeters(300), physics.pixelsToMeters(200)); + circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, 100, 200); circle.game = game; - - var shape = new Phaser.Physics.Advanced.Shapes.Circle(0.4, 0, 0); - shape.elasticity = 0.8; - shape.friction = 1; - shape.density = 1; - circle.addShape(shape); - circle.resetMassData(); - + circle.addCircle(32, 0, 0, 0.8); physics.space.addBody(circle); + t = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_KINETIC, 500, 400); + t.game = game; + //t.addCircle(32, 0, 0, 0.8); + //t.addTriangle(0, 0, 1, 1, 2, 2); + t.addPoly([{ x: -0.8, y: 0.48 }, { x: -0.8, y: 0 }, { x: 0.8, y: 0 }, { x: 0.8, y: 0.32 }, { x: 0, y: 0.84 }, { x: -0.56, y: 0.84 }], 0.5, 1, 6); + physics.space.addBody(t); + } function update() { @@ -106,6 +111,14 @@ //console.log('p', circle.position.x, circle.position.y); } + function renderBounds(body: Phaser.Physics.Advanced.Body) { + + game.stage.context.fillStyle = 'rgba(0,255,200,0.2)'; + + game.stage.context.fillRect(body.bounds.x, body.bounds.y, body.bounds.width, body.bounds.height); + + } + function renderCircle(shape) { game.stage.context.beginPath(); @@ -152,8 +165,14 @@ game.stage.context.fillText('vx: ' + circle.velocity.x + ' vy: ' + circle.velocity.y, 32, 64); renderCircle(circle.shapes[0]); + renderBounds(circle); drawPolygon(game.stage.context, walls.shapes[0], 1, 'rgb(0,255,255)'); + drawPolygon(game.stage.context, walls.shapes[1], 1, 'rgb(0,255,255)'); + + //renderCircle(t.shapes[0]); + drawPolygon(game.stage.context, t.shapes[0], 1, 'rgb(255,255,255)'); + renderBounds(t); } diff --git a/build/phaser.d.ts b/build/phaser.d.ts index 7a30e48a..c83cbfd5 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -8473,6 +8473,16 @@ module Phaser { */ private _game; /** + * A 1x1 sized canvas used for pixel-perfect checks + * @type {HTMLCanvasElement} + */ + public hitCanvas: HTMLCanvasElement; + /** + * The context of the 1x1 pixel check canvas + * @type {CanvasRenderingContext2D} + */ + public hitContext: CanvasRenderingContext2D; + /** * A vector object representing the previous position of the Pointer. * @property vector * @type {Vec2} @@ -9668,6 +9678,10 @@ module Phaser.Physics.Advanced { public setTo(mins: Vec2, maxs: Vec2): void; public copy(b: Bounds): Bounds; public clear(): Bounds; + public x : number; + public y : number; + public width : number; + public height : number; public isEmpty(): bool; public getPerimeter(): number; public addPoint(p: Vec2): Bounds; @@ -9792,7 +9806,7 @@ module Phaser.Physics.Advanced.Shapes { */ module Phaser.Physics.Advanced.Shapes { class Poly extends Shape implements IShape { - constructor(verts?: Vec2[]); + constructor(verts?); public verts: Vec2[]; public planes; public tverts; @@ -9938,6 +9952,26 @@ module Phaser.Physics.Advanced { } } /** +* Phaser - Advanced Physics - Shapes - Triangle +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Triangle extends Poly { + constructor(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number); + } +} +/** +* Phaser - Advanced Physics - Shapes - Box +* +* Based on the work Ju Hyung Lee started in JS PhyRus. +*/ +module Phaser.Physics.Advanced.Shapes { + class Box extends Poly { + constructor(x, y, width, height); + } +} +/** * Phaser - Advanced Physics - Body * * Based on the work Ju Hyung Lee started in JS PhyRus. @@ -9945,6 +9979,7 @@ module Phaser.Physics.Advanced { module Phaser.Physics.Advanced { class Body { constructor(sprite: Sprite, type: number, x?: number, y?: number); + private _tempVec2; /** * Reference to Phaser.Game */ @@ -9986,6 +10021,10 @@ module Phaser.Physics.Advanced { public joints: IJoint[]; public jointHash: {}; public bounds: Bounds; + public mass: number; + public massInverted: number; + public inertia: number; + public inertiaInverted: number; public fixedRotation: bool; public categoryBits: number; public maskBits: number; @@ -9997,12 +10036,12 @@ module Phaser.Physics.Advanced { public isKinetic : bool; public isDynamic : bool; public setType(type: number): void; + public addPoly(verts, elasticity?: number, friction?: number, density?: number): Shapes.Poly; + public addTriangle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, elasticity?: number, friction?: number, density?: number): Shapes.Triangle; + public addBox(x: number, y: number, width: number, height: number, elasticity?: number, friction?: number, density?: number): Shapes.Box; + public addCircle(radius: number, x?: number, y?: number, elasticity?: number, friction?: number, density?: number): Shapes.Circle; public addShape(shape); public removeShape(shape): void; - public mass: number; - public massInverted: number; - public inertia: number; - public inertiaInverted: number; private setMass(mass); private setInertia(inertia); public setTransform(pos, angle): void; @@ -10015,7 +10054,6 @@ module Phaser.Physics.Advanced { public resetMassData(): void; public resetJointAnchors(): void; public cacheData(): void; - private _tempVec2; public updateVelocity(gravity, dt, damping): void; public updatePosition(dt): void; public resetForce(): void; @@ -10031,26 +10069,6 @@ 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 Poly { - constructor(x, y, width, height); - } -} -/** -* Phaser - Advanced Physics - Shapes - Triangle -* -* Based on the work Ju Hyung Lee started in JS PhyRus. -*/ -module Phaser.Physics.Advanced.Shapes { - class Triangle extends Poly { - constructor(p1, p2, p3); - } -} -/** * Phaser - PixelUtils * * A collection of methods useful for manipulating pixels. diff --git a/build/phaser.js b/build/phaser.js index 0f9b2dab..70ddf6c5 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -16057,6 +16057,10 @@ var Phaser; this.camera = this._game.camera; this.activePointer = this.mousePointer; this.currentPointers = 0; + this.hitCanvas = document.createElement('canvas'); + this.hitCanvas.width = 1; + this.hitCanvas.height = 1; + this.hitContext = this.hitCanvas.getContext('2d'); } Input.MOUSE_OVERRIDES_TOUCH = 0; Input.TOUCH_OVERRIDES_MOUSE = 1; @@ -16495,7 +16499,16 @@ var Phaser; return Input; })(); Phaser.Input = Input; -})(Phaser || (Phaser = {})); + /* + public pixelPerfectCheck(sprite: Phaser.Sprite, pointer: Phaser.Pointer, alpha: number = 255): bool { + + this.hitContext.clearRect(0, 0, 1, 1); + + return true; + + } + */ + })(Phaser || (Phaser = {})); /// /// /// @@ -19387,6 +19400,34 @@ var Phaser; this.maxs.setTo(-999999, -999999); return this; }; + Object.defineProperty(Bounds.prototype, "x", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.x); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "y", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.y); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "width", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.x - this.mins.x); + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Bounds.prototype, "height", { + get: function () { + return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.y - this.mins.y); + }, + enumerable: true, + configurable: true + }); Bounds.prototype.isEmpty = function () { return (this.mins.x > this.maxs.x || this.mins.y > this.maxs.y); }; @@ -19845,6 +19886,9 @@ var Phaser; if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } _super.call(this, Advanced.Manager.SHAPE_TYPE_CIRCLE); + x = Advanced.Manager.pixelsToMeters(x); + y = Advanced.Manager.pixelsToMeters(y); + radius = Advanced.Manager.pixelsToMeters(radius); this.center = new Phaser.Vec2(x, y); this.radius = radius; this.tc = new Phaser.Vec2(); @@ -19923,6 +19967,8 @@ var Phaser; (function (Shapes) { var Poly = (function (_super) { __extends(Poly, _super); + // Verts is an optional array of objects, the objects must have public x and y properties which will be used + // to seed this polygon (i.e. Vec2 objects, or just straight JS objects) and must wind COUNTER clockwise function Poly(verts) { _super.call(this, Advanced.Manager.SHAPE_TYPE_POLY); this.verts = []; @@ -19931,7 +19977,7 @@ var Phaser; this.tplanes = []; if(verts) { for(var i = 0; i < verts.length; i++) { - this.verts[i] = Phaser.Vec2Utils.clone(verts[i]); + this.verts[i] = new Phaser.Vec2(verts[i].x, verts[i].y); this.tverts[i] = this.verts[i]; this.tplanes[i] = { }; @@ -20004,7 +20050,7 @@ var Phaser; Poly.prototype.cacheData = function (xf) { this.bounds.clear(); var numVerts = this.verts.length; - //console.log('shapePoly cacheData', numVerts); + console.log('Poly cacheData', numVerts, this.body.name); if(numVerts == 0) { return; } @@ -21156,6 +21202,109 @@ var Phaser; var Physics = Phaser.Physics; })(Phaser || (Phaser = {})); var Phaser; +(function (Phaser) { + (function (Physics) { + (function (Advanced) { + /// + /// + /// + /// + /// + /** + * Phaser - Advanced Physics - Shapes - Triangle + * + * Based on the work Ju Hyung Lee started in JS PhyRus. + */ + (function (Shapes) { + var Triangle = (function (_super) { + __extends(Triangle, _super); + function Triangle(x1, y1, x2, y2, x3, y3) { + x1 = Advanced.Manager.pixelsToMeters(x1); + y1 = Advanced.Manager.pixelsToMeters(y1); + x2 = Advanced.Manager.pixelsToMeters(x2); + y2 = Advanced.Manager.pixelsToMeters(y2); + x3 = Advanced.Manager.pixelsToMeters(x3); + y3 = Advanced.Manager.pixelsToMeters(y3); + _super.call(this, [ + { + x: x1, + y: y1 + }, + { + x: x2, + y: y2 + }, + { + x: x3, + y: y3 + } + ]); + } + return Triangle; + })(Phaser.Physics.Advanced.Shapes.Poly); + Shapes.Triangle = Triangle; + })(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 - 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, [ + { + x: -hw + x, + y: +hh + y + }, + { + x: -hw + x, + y: -hh + y + }, + { + x: +hw + x, + y: -hh + y + }, + { + x: +hw + x, + y: +hh + y + } + ]); + } + return Box; + })(Phaser.Physics.Advanced.Shapes.Poly); + 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) { /// @@ -21168,6 +21317,11 @@ var Phaser; /// /// /// + /// + /// + /// + /// + /// /** * Phaser - Advanced Physics - Body * @@ -21178,6 +21332,7 @@ var Phaser; function Body(sprite, type, x, y) { if (typeof x === "undefined") { x = 0; } if (typeof y === "undefined") { y = 0; } + this._tempVec2 = new Phaser.Vec2(); // Shapes this.shapes = []; // Joints @@ -21188,17 +21343,16 @@ var Phaser; this.categoryBits = 0x0001; this.maskBits = 0xFFFF; this.stepCount = 0; - this._tempVec2 = new Phaser.Vec2(); this.id = Phaser.Physics.Advanced.Manager.bodyCounter++; this.name = 'body' + this.id; this.type = type; if(sprite) { this.sprite = sprite; this.game = sprite.game; - this.position = new Phaser.Vec2(sprite.x, sprite.y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.y)); this.angle = sprite.rotation; } else { - this.position = new Phaser.Vec2(x, y); + this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(x), Phaser.Physics.Advanced.Manager.pixelsToMeters(y)); this.angle = 0; } this.transform = new Phaser.Transform(this.position, this.angle); @@ -21270,6 +21424,56 @@ var Phaser; this.type = type; this.awake(true); }; + Body.prototype.addPoly = function (verts, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var poly = new Phaser.Physics.Advanced.Shapes.Poly(verts); + poly.elasticity = elasticity; + poly.friction = friction; + poly.density = density; + this.addShape(poly); + this.resetMassData(); + return poly; + }; + Body.prototype.addTriangle = function (x1, y1, x2, y2, x3, y3, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var tri = new Phaser.Physics.Advanced.Shapes.Triangle(x1, y1, x2, y2, x3, y3); + tri.elasticity = elasticity; + tri.friction = friction; + tri.density = density; + this.addShape(tri); + this.resetMassData(); + return tri; + }; + Body.prototype.addBox = function (x, y, width, height, elasticity, friction, density) { + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var box = new Phaser.Physics.Advanced.Shapes.Box(x, y, width, height); + box.elasticity = elasticity; + box.friction = friction; + box.density = density; + this.addShape(box); + this.resetMassData(); + return box; + }; + Body.prototype.addCircle = function (radius, x, y, elasticity, friction, density) { + if (typeof x === "undefined") { x = 0; } + if (typeof y === "undefined") { y = 0; } + if (typeof elasticity === "undefined") { elasticity = 1; } + if (typeof friction === "undefined") { friction = 1; } + if (typeof density === "undefined") { density = 1; } + var circle = new Phaser.Physics.Advanced.Shapes.Circle(radius, x, y); + circle.elasticity = elasticity; + circle.friction = friction; + circle.density = density; + this.addShape(circle); + this.resetMassData(); + return circle; + }; Body.prototype.addShape = function (shape) { // Check not already part of this body shape.body = this; @@ -21524,82 +21728,6 @@ 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.Shapes.Poly); - 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 - Shapes - Triangle - * - * Based on the work Ju Hyung Lee started in JS PhyRus. - */ - (function (Shapes) { - var Triangle = (function (_super) { - __extends(Triangle, _super); - function Triangle(p1, p2, p3) { - _super.call(this, [ - new Phaser.Vec2(p1.x, p1.y), - new Phaser.Vec2(p2.x, p2.y), - new Phaser.Vec2(p3.x, p3.y) - ]); - } - return Triangle; - })(Phaser.Physics.Advanced.Shapes.Poly); - Shapes.Triangle = Triangle; - })(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 = {})); /// /// ///