From fbe508ab1b489452d812d758adeaefac107acc46 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Mon, 27 Jan 2014 17:08:12 +0000 Subject: [PATCH] Direct assignment of Body values, allows for sloped ground walking. --- README.md | 2 + examples/physics/mass velocity test.js | 3 +- examples/wip/land.js | 58 ++++----- examples/wip/tween-limit.js | 55 +++++++++ examples/wip/world.js | 38 ++++++ src/particles/arcade/Emitter.js | 2 +- src/physics/arcade/Body.js | 162 ++++++++++++------------- src/utils/Debug.js | 1 + 8 files changed, 199 insertions(+), 122 deletions(-) create mode 100644 examples/wip/tween-limit.js create mode 100644 examples/wip/world.js diff --git a/README.md b/README.md index c4460265..1aa768fb 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Significant API changes: * Button.forceOut default value has changed from true to false, so Buttons will revert to an Up state (if set) when pressed and released. * The way the collision process callback works has changed significantly and now works as originally intended. * The World level quadtree is no longer created, they are now built and ripped down each time you collide a Group, this helps collision accuracy. +* A SAT system has been integrated for Body collision and separation. * Bodies are no longer added to a world quadtree, so have had all of their quadtree properties removed such as skipQuadtree, quadTreeIndex, etc. * Body.drag has been removed. Please use the new Body.friction value instead (which is a number value, not a Point object) * Body.embedded and Body.wasTouching have been removed as they are no longer required. @@ -195,6 +196,7 @@ Bug Fixes: * InputHandler.checkBoundsRect and checkBoundsSprite now take into account if the Sprite is fixedToCamera or not. * Removed the frame property from TileSprites as it cannot use them, it tiles the whole image only, not just a section of it. * Fixed WebGLRenderer updateGraphics bug (thanks theadam) +* Removed duplicate Timer.create line (thanks hstolte) You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md diff --git a/examples/physics/mass velocity test.js b/examples/physics/mass velocity test.js index 69d0405f..be3de3a1 100644 --- a/examples/physics/mass velocity test.js +++ b/examples/physics/mass velocity test.js @@ -62,6 +62,7 @@ function update() { function render() { - game.debug.renderBodyInfo(car, 16, 24); + // game.debug.renderBodyInfo(car, 16, 24); + game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24); } diff --git a/examples/wip/land.js b/examples/wip/land.js index fb191947..87cfe25c 100644 --- a/examples/wip/land.js +++ b/examples/wip/land.js @@ -10,6 +10,7 @@ function preload() { var sprite; var sprite2; var land; +var cursors; function create() { @@ -39,10 +40,13 @@ function create() { new SAT.Vector(780,100), new SAT.Vector(0,100), ]); - console.log(land); - sprite.body.velocity.x = 100; - game.input.onDown.add(launch, this); + console.log(land); + + // sprite.body.velocity.x = 150; + + cursors = game.input.keyboard.createCursorKeys(); + // game.input.onDown.add(launch, this); } @@ -55,42 +59,26 @@ function launch() { } -/* - Tweening body scale test! - - sprite = game.add.sprite(300, 300, 'gameboy', 0); - sprite.name = 'red'; - sprite.body.collideWorldBounds = true; - // sprite.body.checkCollision.right = false; - sprite.body.bounce.setTo(1, 1); - sprite.body.friction = 0; - // sprite.scale.setTo(2, 2); - - sprite2 = game.add.sprite(500, 300, 'gameboy', 2); - sprite2.name = 'green'; - sprite2.body.collideWorldBounds = true; - sprite2.body.bounce.setTo(1, 1); - sprite2.body.friction = 0; - - game.add.tween(sprite.scale).to({x: 3, y: 3}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true); - // to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) { - - -*/ - function update() { - // game.physics.collide(sprite, land); + game.physics.collide(sprite, land); - if (sprite.body.overlap(land.body)) - { - console.log('o', sprite.body.response); - sprite.body.separate(land.body); - } + sprite.body.velocity.x = 0; + // sprite.body.velocity.y = 0; + if (cursors.left.isDown) + { + sprite.body.velocity.x = -100; + } + else if (cursors.right.isDown) + { + sprite.body.velocity.x = 100; + } - // game.physics.collide(sprite, sprite2); - // game.physics.collide(sprite2, land); + if (cursors.up.isDown) + { + sprite.body.velocity.y = -200; + } } @@ -113,7 +101,7 @@ function render() { game.debug.renderPolygon(sprite2.body.polygons); } - game.debug.renderRectangle(land.body); + // game.debug.renderRectangle(land.body); } diff --git a/examples/wip/tween-limit.js b/examples/wip/tween-limit.js new file mode 100644 index 00000000..5a46976f --- /dev/null +++ b/examples/wip/tween-limit.js @@ -0,0 +1,55 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.image('phaser', 'assets/sprites/phaser1.png'); + game.load.spritesheet('arrows', 'assets/sprites/arrows.png', 23, 31); + +} + +var arrowStart; +var arrowEnd; +var sprite; + +function create() { + + game.stage.backgroundColor = '#2384e7'; + + arrowStart = game.add.sprite(100, 100, 'arrows', 0); + + arrowEnd = game.add.sprite(400, 100, 'arrows', 1); + + sprite = game.add.sprite(100, 164, 'phaser'); + sprite.inputEnabled = true; + + sprite.events.onInputDown.add(move, this); + +} + +function move() { + + var tween = game.add.tween(sprite).to( { x: '+500' }, 3000, Phaser.Easing.Linear.None, true); + + tween.onComplete.add(over, this); + +} + +function over() { + console.log('done'); + sprite.alpha = 0.5; +} + +function update() { + + if (sprite.x > 200) + { + sprite.x = 200; + } + +} + +function render() { + + +} diff --git a/examples/wip/world.js b/examples/wip/world.js new file mode 100644 index 00000000..0345f787 --- /dev/null +++ b/examples/wip/world.js @@ -0,0 +1,38 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60); + +} + +var sprite; + +function create() { + + game.stage.backgroundColor = '#124184'; + + game.physics.gravity.y = 200; + + sprite = game.add.sprite(200, 250, 'gameboy', 4); + sprite.name = 'green'; + // sprite.anchor.setTo(0.5, 0.5); + + sprite.body.bounce.setTo(0.9, 0.9); + sprite.body.velocity.x = 150; + +} + +function update() { + + // sprite.worldTransform[2] += 1; + +} + +function render() { + + game.debug.renderBodyInfo(sprite, 32, 32); + game.debug.renderPolygon(sprite.body.polygons); + +} diff --git a/src/particles/arcade/Emitter.js b/src/particles/arcade/Emitter.js index 167edb71..dec83fe8 100644 --- a/src/particles/arcade/Emitter.js +++ b/src/particles/arcade/Emitter.js @@ -263,7 +263,7 @@ Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames var particle; var i = 0; var rndKey = keys; - var rndFrame = 0; + var rndFrame = frames; while (i < quantity) { diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index 58b56c0c..9ee95c67 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -341,6 +341,9 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this._distances = [0, 0, 0, 0]; + this._x = 0; + this._y = 0; + this._debug = 0; }; @@ -401,8 +404,11 @@ Phaser.Physics.Arcade.Body.prototype = { this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; + this._x = this.sprite.x; + this._y = this.sprite.y; this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; + this.preRotation = this.sprite.angle; this.blocked.up = false; @@ -447,66 +453,55 @@ Phaser.Physics.Arcade.Body.prototype = { */ checkWorldBounds: function () { - this.blockedPoint.setTo(0, 0); + if (this.x <= this.game.world.bounds.x) + { + this.x += this.game.world.bounds.x - this.x; + this.blocked.left = true; + } + else if (this.right >= this.game.world.bounds.right) + { + this.x -= this.right - this.game.world.bounds.right; + this.blocked.right = true; + } + if (this.y <= this.game.world.bounds.y) + { + this.y += this.game.world.bounds.y - this.y; + this.blocked.up = true; + } + else if (this.bottom >= this.game.world.bounds.bottom) + { + this.y -= this.bottom - this.game.world.bounds.bottom; + this.blocked.down = true; + } + +/* if (this.x <= this.game.world.bounds.x) { this.blockedPoint.x = this.game.world.bounds.x - this.x; - this.blocked.left = true; this.touching.left = true; + // console.log('cw left', this.blockedPoint.x); } else if (this.right >= this.game.world.bounds.right) { this.blockedPoint.x = this.right - this.game.world.bounds.right; - this.blocked.right = true; this.touching.right = true; + // console.log('cw right', this.blockedPoint.x); } if (this.y <= this.game.world.bounds.y) { this.blockedPoint.y = this.game.world.bounds.y - this.y; - this.blocked.up = true; this.touching.up = true; + // console.log('cw up', this.blockedPoint.y); } else if (this.bottom >= this.game.world.bounds.bottom) { this.blockedPoint.y = this.bottom - this.game.world.bounds.bottom; - this.blocked.down = true; this.touching.down = true; + // console.log('cw down', this.blockedPoint.y); } - - }, - - /** - * Internal method used to check the Body against the World Bounds. - * - * @method Phaser.Physics.Arcade#adjustWorldBounds - * @protected - */ - adjustWorldBounds: function () { - - if (this.x < this.game.world.bounds.x) - { - this.x += this.game.world.bounds.x - this.x; - this.preX += this.game.world.bounds.x - this.x; - } - else if (this.right > this.game.world.bounds.right) - { - this.x -= this.right - this.game.world.bounds.right; - this.preX -= this.right - this.game.world.bounds.right; - } - - if (this.y < this.game.world.bounds.y) - { - this.y += this.game.world.bounds.y - this.y; - this.preY += this.game.world.bounds.y - this.y; - } - else if (this.bottom > this.game.world.bounds.bottom) - { - this.y -= this.bottom - this.game.world.bounds.bottom; - this.preY -= this.bottom - this.game.world.bounds.bottom; - } - +*/ }, /** @@ -532,46 +527,41 @@ Phaser.Physics.Arcade.Body.prototype = { this.velocity.y = Math.sin(this.angle) * this.speed; } - // overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO - if (this.blocked.left && this.blockedPoint.x > 0) + if (this.blocked.left) { - // Separate - this.x += this.blockedPoint.x; this.velocity.x *= -this.bounce.x; this.reboundCheck(true, false); this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - // if (this._dx > this.minBounceVelocity || this.getTotalGravityX() > 0) - // { + if (this._dx > this.minBounceVelocity || this.getTotalGravityX() > 0) + { this.x += this._dx; this.velocity.x += this.motionVelocity.x; - // } - // else - // { - // this.preX = this.x; - // this.velocity.x = 0; - // } + } + else + { + this.preX = this.x; + this.velocity.x = 0; + } } - else if (this.blocked.right && this.blockedPoint.x > 0) + else if (this.blocked.right) { - // Separate - this.x -= this.blockedPoint.x; this.velocity.x *= -this.bounce.x; this.reboundCheck(true, false); this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2); - // if (this._dx < -this.minBounceVelocity || this.getTotalGravityX() < 0) - // { + if (this._dx < -this.minBounceVelocity || this.getTotalGravityX() < 0) + { this.x += this._dx; this.velocity.x += this.motionVelocity.x; - // } - // else - // { - // this.preX = this.x; - // this.velocity.x = 0; - // } + } + else + { + this.preX = this.x; + this.velocity.x = 0; + } } else { @@ -580,45 +570,41 @@ Phaser.Physics.Arcade.Body.prototype = { } // overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO - if (this.blocked.up && this.blockedPoint.y > 0) + if (this.blocked.up) { - // Separate - this.y += this.blockedPoint.y; this.velocity.y *= -this.bounce.y; this.reboundCheck(false, true); this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - // if (this._dy > this.minBounceVelocity || this.getTotalGravityY() > 0) - // { + if (this._dy > this.minBounceVelocity || this.getTotalGravityY() > 0) + { this.y += this._dy; this.velocity.y += this.motionVelocity.y; - // } - // else - // { - // this.preY = this.y; - // this.velocity.y = 0; - // } + } + else + { + this.preY = this.y; + this.velocity.y = 0; + } } - else if (this.blocked.down && this.blockedPoint.y > 0) + else if (this.blocked.down) { - // Separate - this.y -= this.blockedPoint.y; this.velocity.y *= -this.bounce.y; this.reboundCheck(false, true); this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2); - // if (this._dy < -this.minBounceVelocity || this.getTotalGravityY() < 0) - // { + if (this._dy < -this.minBounceVelocity || this.getTotalGravityY() < 0) + { this.y += this._dy; this.velocity.y += this.motionVelocity.y; - // } - // else - // { - // this.preY = this.y; - // this.velocity.y = 0; - // } + } + else + { + this.preY = this.y; + this.velocity.y = 0; + } } else { @@ -1205,7 +1191,9 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.moves) { - this.adjustWorldBounds(); + this.checkWorldBounds(); + + this.syncPosition(); if (this.deltaX() < 0) { @@ -1225,6 +1213,10 @@ Phaser.Physics.Arcade.Body.prototype = { this.facing = Phaser.DOWN; } + this.sprite.x = this.sprite.worldTransform[2] = this.x - (this.preX - this._x); + this.sprite.y = this.sprite.worldTransform[5] = this.y - (this.preY - this._y); + +/* if ((this.deltaX() < 0 && !this.blocked.left) || (this.deltaX() > 0 && !this.blocked.right)) { this.sprite.x += this.deltaX(); @@ -1236,7 +1228,7 @@ Phaser.Physics.Arcade.Body.prototype = { this.sprite.y += this.deltaY(); this.sprite.worldTransform[5] += this.deltaY(); } - +*/ this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight); if (this.allowRotation) diff --git a/src/utils/Debug.js b/src/utils/Debug.js index 4cb4ca37..e063485f 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -455,6 +455,7 @@ Phaser.Utils.Debug.prototype = { this.start(x, y, color, 220); this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2), 'width: ' + sprite.width, 'height: ' + sprite.height); + this.splitline('_x: ' + sprite.body._x.toFixed(2), '_y: ' + sprite.body._y.toFixed(2), 'wx: ' + sprite.worldTransform[2].toFixed(2), 'wy: ' + sprite.worldTransform[5].toFixed(2)); this.splitline('speed: ' + sprite.body.speed.toFixed(2), 'angle: ' + sprite.body.angle.toFixed(2), 'friction: ' + sprite.body.friction); this.splitline('blocked left: ' + sprite.body.blocked.left, 'right: ' + sprite.body.blocked.right, 'up: ' + sprite.body.blocked.up, 'down: ' + sprite.body.blocked.down); this.splitline('touching left: ' + sprite.body.touching.left, 'right: ' + sprite.body.touching.right, 'up: ' + sprite.body.touching.up, 'down: ' + sprite.body.touching.down);