Phaser.Physics.Body = function (sprite){ this.sprite = sprite; this.game = sprite.game; this.offset = new Phaser.Point(); this.data = new p2.Body({ position: [this.px2p(sprite.position.x), this.px2p(sprite.position.y)] , mass: 1} ); this.data.parent = this; this.velocity = new Phaser.Physics.PointProxy(this.data.velocity); this.force = new Phaser.Physics.PointProxy(this.data.force); this.setRectangleFromSprite(sprite); this.game.physics.addBody(this.data); } ; Phaser.Physics.Body.prototype = { adjustCenterOfMass: function (){ this.data.adjustCenterOfMass(); } , applyDamping: function (dt){ this.data.applyDamping(dt); } , applyForce: function (force, worldX, worldY){ this.data.applyForce(force, [this.px2p(worldX), this.px2p(worldY)] ); } , setZeroForce: function (){ this.data.setZeroForce(); } , setZeroRotation: function (){ this.data.angularVelocity = 0; } , setZeroVelocity: function (){ this.data.velocity[0] = 0; this.data.velocity[1] = 0; } , setZeroDamping: function (){ this.data.damping = 0; this.data.angularDamping = 0; } , toLocalFrame: function (out, worldPoint){ return this.data.toLocalFrame(out, worldPoint); } , toWorldFrame: function (out, localPoint){ return this.data.toWorldFrame(out, localPoint); } , rotateLeft: function (speed){ this.data.angularVelocity = this.px2p(speed); } , rotateRight: function (speed){ this.data.angularVelocity = this.px2p(- speed); } , thrust: function (speed){ var magnitude = this.px2p(- speed); var angle = this.data.angle + Math.PI / 2; this.data.force[0] += magnitude * Math.cos(angle); this.data.force[1] += magnitude * Math.sin(angle); } , moveLeft: function (speed){ this.data.velocity[0] = this.px2p(- speed); } , moveRight: function (speed){ this.data.velocity[0] = this.px2p(speed); } , moveUp: function (speed){ this.data.velocity[1] = this.px2p(- speed); } , moveDown: function (speed){ this.data.velocity[1] = this.px2p(speed); } , postUpdate: function (){ this.sprite.x = this.p2px(this.data.position[0]); this.sprite.y = this.p2px(this.data.position[1]); if (!this.fixedRotation) { this.sprite.rotation = this.data.angle; } } , reset: function (x, y, resetDamping, resetMass){ if (typeof resetDamping === 'undefined') { resetDamping = false ; } if (typeof resetMass === 'undefined') { resetMass = false ; } this.setZeroForce(); this.setZeroVelocity(); this.setZeroRotation(); if (resetDamping) { this.setZeroDamping(); } if (resetMass) { this.mass = 1; } this.x = x; this.y = y; } , addToWorld: function (){ if (this.data.world !== this.game.physics) { this.game.physics.addBody(this.data); } } , removeFromWorld: function (){ if (this.data.world === this.game.physics) { this.game.physics.removeBody(this.data); } } , destroy: function (){ this.removeFromWorld(); this.clearShapes(); this.sprite = null ; } , clearShapes: function (){ for (var i = _AN_Read_length('length', this.data.shapes) - 1; i >= 0; i-- ){ var shape = this.data.shapes[i]; this.data.removeShape(shape); } } , addShape: function (shape, offsetX, offsetY, rotation){ if (typeof offsetX === 'undefined') { offsetX = 0; } if (typeof offsetY === 'undefined') { offsetY = 0; } if (typeof rotation === 'undefined') { rotation = 0; } this.data.addShape(shape, [this.px2p(offsetX), this.px2p(offsetY)] , rotation); return shape; } , addCircle: function (radius, offsetX, offsetY, rotation){ var shape = new p2.Circle(this.px2p(radius)); return this.addShape(shape, offsetX, offsetY, rotation); } , addRectangle: function (width, height, offsetX, offsetY, rotation){ var shape = new p2.Rectangle(this.px2p(width), this.px2p(height)); return this.addShape(shape, offsetX, offsetY, rotation); } , addPlane: function (width, height, offsetX, offsetY, rotation){ var shape = new p2.Plane(); return this.addShape(shape, offsetX, offsetY, rotation); } , addParticle: function (width, height, offsetX, offsetY, rotation){ var shape = new p2.Particle(); return this.addShape(shape, offsetX, offsetY, rotation); } , addLine: function (length, offsetX, offsetY, rotation){ var shape = new p2.Line(this.px2p(length)); return this.addShape(shape, offsetX, offsetY, rotation); } , addCapsule: function (length, radius, offsetX, offsetY, rotation){ var shape = new p2.Capsule(this.px2p(length), radius); return this.addShape(shape, offsetX, offsetY, rotation); } , addPolygon: function (options, points){ options = options || { } ; points = Array.prototype.slice.call(arguments, 1); var path; if (_AN_Read_length('length', points) === 1 && Array.isArray(points[0])) { path = points[0]; } else if (Array.isArray(points[0])) { path = points; } else if (typeof points[0] === 'number') { var temp = [] ; for (var i = 0, len = _AN_Read_length('length', points); i < len; i += 2){ temp.push([points[i], points[i + 1]] ); } path = temp; } for (var p = 0; p < _AN_Read_length('length', path); p++ ){ path[p][0] = this.px2p(path[p][0]); path[p][1] = this.px2p(path[p][1]); } return this.data.fromPolygon(path, options); } , removeShape: function (shape){ return this.data.removeShape(shape); } , setCircle: function (radius, offsetX, offsetY, rotation){ this.clearShapes(); this.addCircle(radius, offsetX, offsetY, rotation); } , setRectangle: function (width, height, offsetX, offsetY, rotation){ if (typeof width === 'undefined') { width = 16; } if (typeof height === 'undefined') { height = 16; } this.clearShapes(); return this.addRectangle(width, height, offsetX, offsetY, rotation); } , setRectangleFromSprite: function (sprite){ if (typeof sprite === 'undefined') { sprite = this.sprite; } this.clearShapes(); return this.addRectangle(sprite.width, sprite.height, 0, 0, sprite.rotation); } , loadPolygon: function (key, object, options){ var data = game.cache.getPhysicsData(key, object); if (data && data.shape) { var temp = [] ; for (var i = 0, len = _AN_Read_length('length', data.shape); i < len; i += 2){ temp.push([data.shape[i], data.shape[i + 1]] ); } return this.addPolygon(options, temp); } return false ; } , loadData: function (key, object, options){ var data = game.cache.getPhysicsData(key, object); if (data && data.shape) { this.mass = data.density; this.loadPolygon(key, object); } } , p2px: function (v){ return v *= -20; } , px2p: function (v){ return v * -0.05; } } ; Phaser.Physics.Body.prototype.constructor = Phaser.Physics.Body; Object.defineProperty(Phaser.Physics.Body.prototype, "static", { get: function (){ return (this.data.motionState === Phaser.STATIC); } , set: function (value){ if (value && this.data.motionState !== Phaser.STATIC) { this.data.motionState = Phaser.STATIC; } else if (!value && this.data.motionState === Phaser.STATIC) { this.data.motionState = Phaser.DYNAMIC; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "dynamic", { get: function (){ return (this.data.motionState === Phaser.DYNAMIC); } , set: function (value){ if (value && this.data.motionState !== Phaser.DYNAMIC) { this.data.motionState = Phaser.DYNAMIC; } else if (!value && this.data.motionState === Phaser.DYNAMIC) { this.data.motionState = Phaser.STATIC; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "kinematic", { get: function (){ return (this.data.motionState === Phaser.KINEMATIC); } , set: function (value){ if (value && this.data.motionState !== Phaser.KINEMATIC) { this.data.motionState = Phaser.KINEMATIC; } else if (!value && this.data.motionState === Phaser.KINEMATIC) { this.data.motionState = Phaser.STATIC; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "allowSleep", { get: function (){ return this.data.allowSleep; } , set: function (value){ if (value !== this.data.allowSleep) { this.data.allowSleep = value; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "angle", { get: function (){ return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.data.angle)); } , set: function (value){ this.data.angle = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value)); } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "angularDamping", { get: function (){ return this.data.angularDamping; } , set: function (value){ this.data.angularDamping = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "angularForce", { get: function (){ return this.data.angularForce; } , set: function (value){ this.data.angularForce = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "angularVelocity", { get: function (){ return this.data.angularVelocity; } , set: function (value){ this.data.angularVelocity = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "damping", { get: function (){ return this.data.damping; } , set: function (value){ this.data.damping = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "fixedRotation", { get: function (){ return this.data.fixedRotation; } , set: function (value){ if (value !== this.data.fixedRotation) { this.data.fixedRotation = value; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "inertia", { get: function (){ return this.data.inertia; } , set: function (value){ this.data.inertia = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "mass", { get: function (){ return this.data.mass; } , set: function (value){ if (value !== this.data.mass) { this.data.mass = value; this.data.updateMassProperties(); } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "motionState", { get: function (){ return this.data.motionState; } , set: function (value){ if (value !== this.data.motionState) { this.data.motionState = value; } } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "rotation", { get: function (){ return this.data.angle; } , set: function (value){ this.data.angle = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "sleepSpeedLimit", { get: function (){ return this.data.sleepSpeedLimit; } , set: function (value){ this.data.sleepSpeedLimit = value; } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "x", { get: function (){ return this.p2px(this.data.position[0]); } , set: function (value){ this.data.position[0] = this.px2p(value); } } ); Object.defineProperty(Phaser.Physics.Body.prototype, "y", { get: function (){ return this.p2px(this.data.position[1]); } , set: function (value){ this.data.position[1] = this.px2p(value); } } );