diff --git a/examples/assets/sprites/thrust_ship2.png b/examples/assets/sprites/thrust_ship2.png new file mode 100644 index 00000000..80074564 Binary files /dev/null and b/examples/assets/sprites/thrust_ship2.png differ diff --git a/examples/wip/p24.js b/examples/wip/p24.js index ba9c686b..fe1e8b08 100644 --- a/examples/wip/p24.js +++ b/examples/wip/p24.js @@ -3,6 +3,7 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: function preload() { + game.load.image('backdrop', 'assets/pics/remember-me.jpg'); game.load.image('box', 'assets/sprites/block.png'); } @@ -21,17 +22,23 @@ function px2p(v) { function create() { + game.world.setBounds(0, 0, 1920, 1200); + game.add.sprite(0, 0, 'backdrop'); + box = game.add.sprite(200, 200, 'box'); box.anchor.set(0.5); box.physicsEnabled = true; - box2 = game.add.sprite(400, 100, 'box'); - box2.anchor.set(0.5); + box2 = game.add.sprite(400, 200, 'box'); + box2.anchor.set(0.5); // if using physics you nearly always need to anchor from the center like this unless you don't need rotation + // box2.scale.set(2); // if you need to scale, do it BEFORE enabling physics. You can't do it at run-time. box2.physicsEnabled = true; box2.body.setZeroDamping(); box2.body.fixedRotation = true; + game.camera.follow(box2); + cursors = game.input.keyboard.createCursorKeys(); game.physics.defaultRestitution = 0.8; diff --git a/examples/wip/p25.js b/examples/wip/p25.js new file mode 100644 index 00000000..3b98e37a --- /dev/null +++ b/examples/wip/p25.js @@ -0,0 +1,69 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.image('backdrop', 'assets/pics/remember-me.jpg'); + game.load.image('ship', 'assets/sprites/thrust_ship2.png'); + +} + +var ship; +var cursors; + +function p2px(v) { + return v *= -20; +} + +function px2p(v) { + return v * -0.05; +} + +function create() { + + game.world.setBounds(0, 0, 1920, 1200); + + var bg = game.add.sprite(0, 0, 'backdrop'); + bg.alpha = 0.2; + + ship = game.add.sprite(200, 200, 'ship'); + ship.anchor.set(0.5); + ship.physicsEnabled = true; + + game.camera.follow(ship); + + cursors = game.input.keyboard.createCursorKeys(); + + game.physics.defaultRestitution = 0.8; + +} + +function update() { + + if (cursors.left.isDown) + { + ship.body.rotateLeft(100); + } + else if (cursors.right.isDown) + { + ship.body.rotateRight(100); + } + else + { + ship.body.setZeroRotation(); + } + + if (cursors.up.isDown) + { + ship.body.thrust(400); + } + +} + +function render() { + + // game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32); + // game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64); + +} + diff --git a/src/physics/Body.js b/src/physics/Body.js index df7cf350..01a9f39c 100644 --- a/src/physics/Body.js +++ b/src/physics/Body.js @@ -31,8 +31,6 @@ Phaser.Physics.Body = function (sprite) { */ this.offset = new Phaser.Point(); -// force - this.shape = null; this.data = new p2.Body({ position:[this.px2p(sprite.x), this.px2p(sprite.y)], mass: 1 }); @@ -43,16 +41,9 @@ Phaser.Physics.Body = function (sprite) { this.velocity = new Phaser.Physics.PointProxy(this.data.velocity); /** - * @property {number} _sx - Internal cache var. - * @private + * @property {Phaser.PointProxy} force - The force applied to the body. */ - this._sx = sprite.scale.x; - - /** - * @property {number} _sy - Internal cache var. - * @private - */ - this._sy = sprite.scale.y; + this.force = new Phaser.Physics.PointProxy(this.data.force); // Set-up the default shape this.setRectangle(sprite.width, sprite.height, 0, 0); @@ -124,6 +115,17 @@ Phaser.Physics.Body.prototype = { // toLocalFrame // toWorldFrame + /** + * If this Body is dynamic then this will zero its velocity on both axis. + * + * @method Phaser.Physics.Body#setZeroRotation + */ + setZeroRotation: function () { + + ship.body.angularVelocity = 0; + + }, + /** * If this Body is dynamic then this will zero its velocity on both axis. * @@ -148,6 +150,47 @@ Phaser.Physics.Body.prototype = { }, + /** + * This will rotate the Body by the given speed to the left (counter-clockwise). + * + * @method Phaser.Physics.Body#rotateLeft + * @param {number} speed - The speed at which it should rotate. + */ + rotateLeft: function (speed) { + + this.data.angularVelocity = this.px2p(speed); + + }, + + /** + * This will rotate the Body by the given speed to the left (clockwise). + * + * @method Phaser.Physics.Body#rotateRight + * @param {number} speed - The speed at which it should rotate. + */ + rotateRight: function (speed) { + + this.data.angularVelocity = this.px2p(-speed); + + }, + + /** + * Applies a force to the Body that causes it to 'thrust' forwards, based on its current angle and the given speed. + * The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms). + * + * @method Phaser.Physics.Body#thrust + * @param {number} speed - The speed at which it should thrust. + */ + 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); + + }, + /** * If this Body is dynamic then this will move it to the left by setting its x velocity to the given speed. * The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms). @@ -200,28 +243,6 @@ Phaser.Physics.Body.prototype = { }, - /** - * Internal method that updates the Body scale in relation to the parent Sprite. - * - * @method Phaser.Physics.Body#updateScale - * @protected - */ - updateScale: function () { - - // if (this.polygon) - // { - // this.polygon.scale(this.sprite.scale.x / this._sx, this.sprite.scale.y / this._sy); - // } - // else - // { - // this.shape.r *= Math.max(this.sprite.scale.x, this.sprite.scale.y); - // } - - this._sx = this.sprite.scale.x; - this._sy = this.sprite.scale.y; - - }, - /** * Internal method that updates the Body position in relation to the parent Sprite. * @@ -233,18 +254,6 @@ Phaser.Physics.Body.prototype = { // this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x; // this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y; - // This covers any motion that happens during this frame, not since the last frame - // this.preX = this.x; - // this.preY = this.y; - // this.preRotation = this.sprite.angle; - - // this.rotation = this.preRotation; - - if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy) - { - this.updateScale(); - } - }, /** @@ -255,55 +264,13 @@ Phaser.Physics.Body.prototype = { */ postUpdate: function () { - /* - if (this.moves) - { - this.game.physics.checkBounds(this); - - this.reboundCheck(true, true, true); - - this._dx = this.deltaX(); - this._dy = this.deltaY(); - - if (this._dx < 0) - { - this.facing = Phaser.LEFT; - } - else if (this._dx > 0) - { - this.facing = Phaser.RIGHT; - } - - if (this._dy < 0) - { - this.facing = Phaser.UP; - } - else if (this._dy > 0) - { - this.facing = Phaser.DOWN; - } - - if (this._dx !== 0 || this._dy !== 0) - { - this.sprite.x += this._dx; - this.sprite.y += this._dy; - } - - if (this.allowRotation && this.deltaZ() !== 0) - { - this.sprite.angle += this.deltaZ(); - } - - if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy) - { - this.updateScale(); - } - } - */ - this.sprite.x = this.p2px(this.data.position[0]); this.sprite.y = this.p2px(this.data.position[1]); - this.sprite.rotation = this.data.angle; + + if (!this.fixedRotation) + { + this.sprite.rotation = this.data.angle; + } }, @@ -465,7 +432,9 @@ Phaser.Physics.Body.prototype = { * @return {number} The scaled value. */ p2px: function (v) { + return v *= -20; + }, /** @@ -476,7 +445,9 @@ Phaser.Physics.Body.prototype = { * @return {number} The scaled value. */ px2p: function (v) { + return v * -0.05; + } };