From 10d105d5ceefdad34cfa0f75d44e78880bb01ce7 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 9 Jan 2014 03:42:58 +0000 Subject: [PATCH] Acceleration tested and working fine. Proper accurate friction added and working really nicely, so much better than 'drag' used to. Considering removing drag, although will break the API history. --- examples/wip/acceleration.js | 32 +++++----- examples/wip/bunny.js | 6 +- src/physics/arcade/Body.js | 116 ++++++++--------------------------- src/system/StageScaleMode.js | 3 +- 4 files changed, 47 insertions(+), 110 deletions(-) diff --git a/examples/wip/acceleration.js b/examples/wip/acceleration.js index 55c8fce4..8d40cc5a 100644 --- a/examples/wip/acceleration.js +++ b/examples/wip/acceleration.js @@ -29,6 +29,7 @@ function create() { car.body.collideWorldBounds = true; car.body.bounce.setTo(0.8, 0.8); car.body.allowRotation = true; + car.body.friction = 10; // car.body.drag.x = 10; // car.body.drag.y = 10; @@ -50,36 +51,36 @@ function start() { function update() { - if (car.x >= 400 && car.body.velocity.x > 0) - { - car.body.velocity.x = 0; - car.body.acceleration.x = 0; - var total = game.time.now - s; - console.log(game.time.physicsElapsed); - console.log('total ms', total, 'px/sec', car.x/(total/1000)); - } + // if (car.x >= 400 && car.body.velocity.x > 0) + // { + // car.body.velocity.x = 0; + // car.body.acceleration.x = 0; + // var total = game.time.now - s; + // console.log(game.time.physicsElapsed); + // console.log('total ms', total, 'px/sec', car.x/(total/1000)); + // } // car.body.velocity.x = 0; // car.body.velocity.y = 0; - // car.body.angularVelocity = 0; + car.body.angularVelocity = 0; - // car.body.acceleration.x = 0; - // car.body.acceleration.y = 0; + car.body.acceleration.x = 0; + car.body.acceleration.y = 0; if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { car.body.angularVelocity = -200; - // car.body.acceleration.x = -10; + // car.body.acceleration.x = -100; } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { car.body.angularVelocity = 200; - // car.body.acceleration.x = 10; + // car.body.acceleration.x = 100; } if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { - // game.physics.accelerationFromRotation(car.rotation, 10, car.body.acceleration); + game.physics.accelerationFromRotation(car.rotation, 400, car.body.acceleration); // car.body.acceleration.x = 10; // car.body.acceleration.copyFrom(game.physics.velocityFromAngle(car.angle, 30)); @@ -96,6 +97,7 @@ function update() { function render() { - game.debug.renderSpriteInfo(car, 32, 32); + // game.debug.renderSpriteInfo(car, 32, 32); + game.debug.renderBodyInfo(car, 16, 24); } diff --git a/examples/wip/bunny.js b/examples/wip/bunny.js index a86a6a8e..518e52a7 100644 --- a/examples/wip/bunny.js +++ b/examples/wip/bunny.js @@ -40,9 +40,9 @@ this.bunny.body.velocity.x = -500; } - // var melon = this.melonGroup.getFirstExists(true); - // melon.x = this.bunny.x; - // melon.y = this.bunny.y - 40; + var melon = this.melonGroup.getFirstExists(true); + melon.x = this.bunny.x; + melon.y = this.bunny.y - 40; this.carrot.x = this.bunny.x; this.carrot.y = this.bunny.y - 20; diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index 7dfe206d..975a66ff 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -315,7 +315,7 @@ Phaser.Physics.Arcade.Body = function (sprite) { this._sleepTimer = 0; // ms this._drag = 0; this._debug = 0; - // this.friction = 0.99; + this.friction = 0.9; // this._debug = 0; /** @@ -387,7 +387,7 @@ Phaser.Physics.Arcade.Body.prototype = { this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; this.preRotation = this.sprite.angle; - if (this.canSleep && this.sleeping && this.velocity.equals(this.prevVelocity) === false) + if (this.canSleep && this.sleeping && (this.velocity.equals(this.prevVelocity) === false || this.acceleration.isZero() === false)) { this.sleeping = false; this._sleepTimer = 0; @@ -450,33 +450,19 @@ Phaser.Physics.Arcade.Body.prototype = { applyMotion: function () { - // Apply drag - this should be proportionally applied, not linearly like below - if (this.drag.x !== 0 && this.acceleration.x === 0) + if (this.friction > 0 && this.acceleration.isZero()) { - this._drag = this.drag.x * this.game.time.physicsElapsed; + if (this.speed > this.friction) + { + this.speed -= this.friction; + } + else + { + this.speed = 0; + } - if (this.velocity.x > 0) - { - this.velocity.x -= this._drag; - } - else if (this.velocity.x < 0) - { - this.velocity.x += this._drag; - } - } - - if (this.drag.y !== 0 && this.acceleration.y === 0) - { - this._drag = this.drag.y * this.game.time.physicsElapsed; - - if (this.velocity.y > 0) - { - this.velocity.y -= this._drag; - } - else if (this.velocity.y < 0) - { - this.velocity.y += this._drag; - } + this.velocity.x = Math.cos(this.angle) * this.speed; + this.velocity.y = Math.sin(this.angle) * this.speed; } this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); @@ -485,74 +471,22 @@ Phaser.Physics.Arcade.Body.prototype = { this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); this.velocity.y += this.motionVelocity.y; - - /* - if (this.drag.x !== 0 && this.acceleration.x === 0) - { - this._drag = this.drag.x * this.game.time.physicsElapsed; - - if (this.velocity.x - this._drag > 0) - { - this.velocity.x -= this._drag; - this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - this.velocity.x += this.motionVelocity.x; - } - else if (this.velocity.x + this.drag.x < 0) - { - this.velocity.x += this._drag; - this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - this.velocity.x += this.motionVelocity.x; - } - else - { - this.velocity.x = 0; - // this.preX = this.x; - // this.motionVelocity.x = 0; - } - } - else - { - this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - this.velocity.x += this.motionVelocity.x; - } - - if (this.drag.y !== 0 && this.acceleration.y === 0) - { - this._drag = this.drag.y * this.game.time.physicsElapsed; - - if (this.velocity.y - this._drag > 0) - { - this.velocity.y -= this._drag; - this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - this.velocity.y += this.motionVelocity.y; - } - else if (this.velocity.y + this.drag.y < 0) - { - this.velocity.y += this._drag; - this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - this.velocity.y += this.motionVelocity.y; - } - else - { - this.velocity.y = 0; - // this.preY = this.y; - // this.motionVelocity.y = 0; - } - } - else - { - this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - this.velocity.y += this.motionVelocity.y; - } - */ - if (this.velocity.x > this.maxVelocity.x) { - // this.velocity.x = this.maxVelocity.x; + this.velocity.x = this.maxVelocity.x; } else if (this.velocity.x < -this.maxVelocity.x) { - // this.velocity.x = -this.maxVelocity.x; + this.velocity.x = -this.maxVelocity.x; + } + + if (this.velocity.y > this.maxVelocity.y) + { + this.velocity.y = this.maxVelocity.y; + } + else if (this.velocity.y < -this.maxVelocity.y) + { + this.velocity.y = -this.maxVelocity.y; } if (this.collideWorldBounds) @@ -606,7 +540,7 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.collideWorldBounds) { - // this.checkWorldBounds(); + this.checkWorldBounds(); } this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight); diff --git a/src/system/StageScaleMode.js b/src/system/StageScaleMode.js index a875a792..52dce578 100644 --- a/src/system/StageScaleMode.js +++ b/src/system/StageScaleMode.js @@ -260,7 +260,8 @@ Phaser.StageScaleMode.prototype = { this._width = this.width; this._height = this.height; - // console.log('startFullScreen', this._width, this._height); + // This needs updating to match the final spec: + // http://generatedcontent.org/post/70347573294/is-your-fullscreen-api-code-up-to-date-find-out-how-to if (element['requestFullScreen']) {