From 1af86771bac8080a26d95ca07c98ebceeb53755a Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 5 Feb 2014 04:01:10 +0000 Subject: [PATCH] And we have level/ground jitter fixed :) Starstruck is running again properly now too. Time to commit and remove lots of debug data. --- examples/games/starstruck.js | 24 +- examples/wip/index.php | 1 + examples/wip/jitter.js | 334 ++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 31 --- src/physics/arcade/ArcadePhysics.js | 8 +- src/physics/arcade/Body.js | 67 +++--- src/utils/Debug.js | 2 +- 7 files changed, 401 insertions(+), 66 deletions(-) create mode 100644 examples/wip/jitter.js diff --git a/examples/games/starstruck.js b/examples/games/starstruck.js index d69d0a6b..47468fbd 100644 --- a/examples/games/starstruck.js +++ b/examples/games/starstruck.js @@ -25,6 +25,17 @@ var bg; function create() { + $('#step').click(function(){ + console.log('---- STEP', game.stepCount, '-------------------------------'); + game.step(); + }); + + $('#start').click(function(){ + console.log('---- START DEBUGGING -------------------------------'); + game.enableStep(); + player.debug = true; + }); + game.stage.backgroundColor = '#000000'; bg = game.add.tileSprite(0, 0, 800, 600, 'background'); @@ -43,12 +54,14 @@ function create() { layer.resizeWorld(); - game.physics.gravity.y = 260; + game.physics.gravity.y = 250; + game.physics.setBoundsToWorld(); player = game.add.sprite(32, 32, 'dude'); player.body.bounce.y = 0.2; + player.body.minVelocity.y = 5; player.body.collideWorldBounds = true; - player.body.setSize(16, 32, 8, 16); + player.body.setRectangle(16, 32, 8, 16); player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('turn', [4], 20, true); @@ -106,8 +119,10 @@ function update() { } } - if (jumpButton.isDown && player.body.touching.down && game.time.now > jumpTimer) + // if (jumpButton.isDown && player.body.touching.down && game.time.now > jumpTimer) + if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer) { + console.log('jump'); player.body.velocity.y = -250; jumpTimer = game.time.now + 750; } @@ -116,6 +131,7 @@ function update() { function render () { - // game.debug.renderSpriteBody(player); + game.debug.renderPhysicsBody(player.body); + game.debug.renderBodyInfo(player, 16, 24); } diff --git a/examples/wip/index.php b/examples/wip/index.php index ac871931..74826bf3 100644 --- a/examples/wip/index.php +++ b/examples/wip/index.php @@ -101,6 +101,7 @@
+

work in progress examples

diff --git a/examples/wip/jitter.js b/examples/wip/jitter.js new file mode 100644 index 00000000..4a8b9f3d --- /dev/null +++ b/examples/wip/jitter.js @@ -0,0 +1,334 @@ + +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); + game.load.image('atari', 'assets/sprites/atari130xe.png'); + +} + +var sprite; +var sprite2; +var sprite3; +var reverse = false; + +function onBeginContact(a, b) { + console.log('Begin Contact between', a.name, 'and', b.name); +} + +function onEndContact(a, b) { + console.log('End Contact between', a.name, 'and', b.name); +} + +function create() { + + game.stage.backgroundColor = '#124184'; + + test8(); + +} + +function test8() { + + $('#step').click(function(){ + console.log('---- STEP', game.stepCount, '-------------------------------'); + game.step(); + }); + + game.physics.gravity.y = 200; + + sprite = game.add.sprite(300, 300, 'atari'); + sprite.name = 'atari'; + sprite.body.collideWorldBounds = true; + sprite.body.bounce.setTo(0.2, 0.2); + sprite.body.minVelocity.setTo(5, 5); + + sprite2 = game.add.sprite(350, 150, 'gameboy', 2); + sprite2.name = 'gameboy'; + sprite2.body.collideWorldBounds = true; + sprite2.body.bounce.setTo(0.8, 0.8); + // sprite2.body.minVelocity.setTo(30, 30); + // sprite.debug = true; + + // game.enableStep(); + + sprite2.events.onBeginContact.add(onBeginContact, this); + sprite2.events.onEndContact.add(onEndContact, this); + + reverse = true; + + // game.input.onDown.add(launch8, this); + +} + +function launch8() { + + sprite2.body.velocity.x = -100; + sprite2.body.velocity.y = -200; + +} + +function test7() { + + // A down into B + + sprite = game.add.sprite(300, 400, 'atari'); + sprite.name = 'atari'; + sprite.body.collideWorldBounds = true; + sprite.body.bounce.setTo(1, 1); + sprite.body.checkCollision.up = false; + + sprite2 = game.add.sprite(350, 100, 'gameboy', 2); + sprite2.name = 'gameboy'; + sprite2.body.collideWorldBounds = true; + sprite2.body.bounce.setTo(1, 1); + + // reverse = true; + + game.input.onDown.add(launch7, this); + +} + +function launch7() { + + sprite2.body.velocity.y = 100; + +} + +function test6() { + + // Offset Down Collision false + + sprite = game.add.sprite(100, 300, 'atari'); + sprite.name = 'atari'; + sprite.body.collideWorldBounds = true; + sprite.body.bounce.setTo(1, 1); + sprite.body.checkCollision.left = false; + sprite.body.checkCollision.right = false; + + sprite2 = game.add.sprite(500, 330, 'gameboy', 2); + // sprite2 = game.add.sprite(500, 530, 'gameboy', 2); + sprite2.name = 'gameboy'; + sprite2.body.collideWorldBounds = true; + sprite2.body.bounce.setTo(1, 1); + + sprite3 = game.add.sprite(400, 100, 'gameboy', 0); + sprite3.name = 'gameboy2'; + sprite3.body.collideWorldBounds = true; + sprite3.body.bounce.setTo(1, 1); + + game.input.onDown.add(launch6, this); + +} + +function launch6() { + + sprite.body.velocity.x = 100; + sprite2.body.velocity.x = -100; + sprite3.body.velocity.y = 100; + +} + +function test5() { + + // Offset Down Collision false + + sprite = game.add.sprite(360, 400, 'gameboy', 0); + sprite.name = 'red'; + sprite.body.collideWorldBounds = true; + // sprite.body.bounce.setTo(0.9, 0.9); + sprite.body.checkCollision.up = false; + + sprite2 = game.add.sprite(400, 200, 'gameboy', 2); + sprite2.name = 'green'; + sprite2.body.collideWorldBounds = true; + // sprite2.body.checkCollision.down = false; + // sprite2.body.mass = 1; + sprite2.body.bounce.setTo(1, 1); + // sprite2.body.friction = 0; + + game.input.onDown.add(launch5, this); + +} + +function launch5() { + + // sprite.body.velocity.x = 200; + // sprite.body.velocity.y = -300; + // sprite2.body.velocity.x = -200; + sprite2.body.velocity.y = 200; + +} + +function test4() { + + // Down Collision false + + sprite = game.add.sprite(400, 400, 'gameboy', 0); + sprite.name = 'red'; + sprite.body.collideWorldBounds = true; + sprite.body.bounce.setTo(0.9, 0.9); + + sprite2 = game.add.sprite(400, 200, 'gameboy', 2); + sprite2.name = 'green'; + sprite2.body.collideWorldBounds = true; + sprite2.body.checkCollision.down = false; + // sprite2.body.mass = 1; + // sprite2.body.bounce.setTo(1, 1); + // sprite2.body.friction = 0; + + game.input.onDown.add(launch4, this); + +} + +function launch4() { + + // sprite.body.velocity.x = 200; + sprite.body.velocity.y = -300; + // sprite2.body.velocity.x = -200; + // sprite2.body.velocity.y = -200; + +} + +function test3() { + + // Top Collision false + + sprite = game.add.sprite(400, 400, 'gameboy', 0); + sprite.name = 'red'; + sprite.body.collideWorldBounds = true; + sprite.body.checkCollision.up = false; + + sprite2 = game.add.sprite(400, 200, 'gameboy', 2); + sprite2.name = 'green'; + sprite2.body.collideWorldBounds = true; + // sprite2.body.checkCollision.left = false; + sprite2.body.bounce.setTo(0.9, 0.9); + // sprite2.body.mass = 1; + // sprite2.body.bounce.setTo(1, 1); + // sprite2.body.friction = 0; + + game.input.onDown.add(launch3, this); + +} + +function launch3() { + + // sprite.body.velocity.x = 200; + // sprite.body.velocity.y = -300; + // sprite2.body.velocity.x = -200; + sprite2.body.velocity.y = 200; + +} + +function test2() { + + // Left Collision false + + sprite = game.add.sprite(200, 300, 'gameboy', 0); + sprite.name = 'red'; + sprite.body.collideWorldBounds = true; + // sprite.body.checkCollision.right = false; + sprite.body.bounce.setTo(0.9, 0.9); + // sprite.body.friction = 0; + // sprite.scale.setTo(2, 2); + // sprite.body.mass = 2; + + sprite2 = game.add.sprite(500, 300, 'gameboy', 2); + sprite2.name = 'green'; + sprite2.body.collideWorldBounds = true; + sprite2.body.checkCollision.left = false; + // sprite2.body.bounce.setTo(0.9, 0.9); + // sprite2.body.mass = 1; + // sprite2.body.bounce.setTo(1, 1); + // sprite2.body.friction = 0; + + game.input.onDown.add(launch2, this); + +} + +function launch2() { + + sprite.body.velocity.x = 200; + // sprite.body.velocity.y = -300; + // sprite2.body.velocity.x = -200; + // sprite2.body.velocity.y = -200; + +} + + +function test1() { + + // Right Collision false + + sprite = game.add.sprite(200, 300, 'gameboy', 0); + sprite.name = 'red'; + sprite.body.collideWorldBounds = true; + sprite.body.checkCollision.right = false; + // sprite.body.bounce.setTo(0.9, 0.9); + // sprite.body.bounce.setTo(1, 1); + // sprite.body.friction = 0; + // sprite.scale.setTo(2, 2); + // sprite.body.mass = 2; + + sprite2 = game.add.sprite(500, 300, 'gameboy', 2); + sprite2.name = 'green'; + sprite2.body.collideWorldBounds = true; + sprite2.body.bounce.setTo(0.9, 0.9); + // sprite2.body.mass = 1; + // sprite2.body.bounce.setTo(1, 1); + // sprite2.body.friction = 0; + + game.input.onDown.add(launch1, this); + +} + +function launch1() { + + sprite2.body.velocity.x = -200; + +} + +function update() { + + if (reverse) + { + game.physics.collide(sprite2, sprite); + } + else + { + game.physics.collide(sprite, sprite2); + } + + if (sprite3) + { + game.physics.collide(sprite, sprite3); + } + +} + +function render() { + + if (sprite) + { + game.debug.renderBodyInfo(sprite, 16, 24); + game.debug.renderBodyInfo(sprite2, 16, 240); + } + + if (sprite) + { + game.debug.renderPhysicsBody(sprite.body); + } + + if (sprite2) + { + game.debug.renderPhysicsBody(sprite2.body); + } + + if (sprite3) + { + game.debug.renderPhysicsBody(sprite3.body); + } + +} diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 5c38db97..ac98a29f 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -377,8 +377,6 @@ Phaser.Sprite = function (game, x, y, key, frame) { */ this.debug = false; - // this.events.onAddedToGroup.add(this.initGroup, this); - this.updateCache(); this.updateBounds(); @@ -430,12 +428,6 @@ Phaser.Sprite.prototype.preUpdate = function() { return; } - if (this.debug) - { - console.log('Sprite preUpdate', this.parent.worldTransform[2], this.parent.worldTransform[5], 'LT', this.parent.localTransform[2], this.parent.localTransform[5], 'xy', this.parent.position.x, this.parent.position.y); - console.log('Sprite preUpdate', this.x, this.y, 'world', this.world.x, this.world.y); - } - if (!this.exists || (this.group && !this.group.exists)) { this.renderOrderID = -1; @@ -494,11 +486,6 @@ Phaser.Sprite.prototype.updateCache = function() { this._cache.prevX = this.world.x; this._cache.prevY = this.world.y; - if (this.debug) - { - console.log('Sprite updateCache', this._cache.prevX, this._cache.prevY); - } - if (this.fixedToCamera) { this.x = this.game.camera.view.x + this.cameraOffset.x; @@ -724,16 +711,6 @@ Phaser.Sprite.prototype.postUpdate = function() { if (this.body) { this.body.postUpdate(); - - // console.log('Sprite postUpdate wt', this.worldTransform[2], this.worldTransform[5], 'xy', this.x, this.y); - - // this._cache.x = this.x; - // this._cache.y = this.y; - - // this.position.x = this._cache.x; - // this.position.y = this._cache.y; - - // this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]); } if (this.fixedToCamera) @@ -747,16 +724,8 @@ Phaser.Sprite.prototype.postUpdate = function() { this._cache.y = this.y; } - // this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]); - this.position.x = this._cache.x; this.position.y = this._cache.y; - - if (this.debug) - { - console.log('Sprite postUpdate delta', this.deltaX, this.deltaY, 'prev', this._cache.prevX, this._cache.prevY); - } - } }; diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index b69c8fb2..cd9a90df 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -183,7 +183,7 @@ Phaser.Physics.Arcade.prototype = { if (!body.collideWorldBounds || (!this.worldLeft && !this.worldRight && !this.worldTop && !this.worldBottom)) { - return; + return false; } this._response.clear(); @@ -234,13 +234,13 @@ Phaser.Physics.Arcade.prototype = { rebounded = true; } - return rebounded; - if (body.sprite.debug) { - console.log('checkBounds finished', body.blocked); + console.log('checkBounds finished', rebounded, body.blocked); } + return rebounded; + }, /** diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index c1c99247..21876056 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -395,15 +395,15 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.sprite.debug) { - // console.log('Body preUpdate x:', this.x, 'y:', this.y); + console.log('Body preUpdate x:', this.x, 'y:', this.y); // console.log('Body preUpdate Sprite x:', this.sprite.x, 'y:', this.sprite.y); // console.log('Body preUpdate Sprite world:', this.sprite.world.x, 'y:', this.sprite.world.y); // console.log('Body preUpdate Sprite position:', this.sprite.position.x, 'y:', this.sprite.position.y); // console.log('Body preUpdate Sprite localTransform:', this.sprite.localTransform[2], 'y:', this.sprite.localTransform[5]); // console.log('Body preUpdate Sprite worldTransform:', this.sprite.worldTransform[2], 'y:', this.sprite.worldTransform[5]); // console.log('Body preUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY); - // console.log('Body preUpdate blocked:', this.blocked, this.blockFlags); - // console.log('Body preUpdate velocity:', this.velocity.x, this.velocity.y); + console.log('Body preUpdate blocked:', this.blocked, this.blockFlags); + console.log('Body preUpdate velocity:', this.velocity.x, this.velocity.y); // console.log('Body preUpdate rotation:', this.rotation, this.preRotation); } @@ -555,14 +555,18 @@ if (this.sprite.debug) if (x) { - if (rebound && this.bounce.x !== 0 && (this.blocked.left || this.blocked.right)) + if (rebound && this.bounce.x !== 0 && (this.blocked.left || this.blocked.right || this.touching.left || this.touching.right)) { - this.velocity.x *= -this.bounce.x; - this.angle = Math.atan2(this.velocity.y, this.velocity.x); - - if (this.sprite.debug) + // Don't rebound if they've already rebounded in this frame + if (!(this._vx <= 0 && this.velocity.x > 0) && !(this._vx >= 0 && this.velocity.x < 0)) { - console.log('X rebound applied'); + this.velocity.x *= -this.bounce.x; + this.angle = Math.atan2(this.velocity.y, this.velocity.x); + + if (this.sprite.debug) + { + console.log('X rebound applied', this._vx, 'to', this.velocity.x); + } } } @@ -570,7 +574,7 @@ if (this.sprite.debug) { var gx = this.getUpwardForce(); - if ((this.blocked.left && (gx < 0 || this.velocity.x < 0)) || (this.blocked.right && (gx > 0 || this.velocity.x > 0))) + if (((this.blocked.left || this.touching.left) && (gx < 0 || this.velocity.x < 0)) || ((this.blocked.right || this.touching.right) && (gx > 0 || this.velocity.x > 0))) { this.velocity.x = 0; @@ -579,24 +583,30 @@ if (this.sprite.debug) console.log('reboundCheck X zeroed'); } } - } - if (this.sprite.debug) - { - console.log('reboundCheck X', this.velocity.x, 'gravity', gx); + if (this.sprite.debug) + { + console.log('reboundCheck X', this.velocity.x, 'gravity', gx); + } } } if (y) { - if (rebound && this.bounce.y !== 0 && (this.blocked.up || this.blocked.down)) + if (rebound && this.bounce.y !== 0 && (this.blocked.up || this.blocked.down || this.touching.up || this.touching.down)) { - this.velocity.y *= -this.bounce.y; - this.angle = Math.atan2(this.velocity.y, this.velocity.x); - - if (this.sprite.debug) + // Don't rebound if they've already rebounded in this frame + if (!(this._vy <= 0 && this.velocity.y > 0) && !(this._vy >= 0 && this.velocity.y < 0)) { - console.log('Y rebound applied'); + this.velocity.y *= -this.bounce.y; + this.angle = Math.atan2(this.velocity.y, this.velocity.x); + + if (this.sprite.debug) + { + console.log('Y rebound applied', this._vy, 'to', this.velocity.y); + console.log('Y rebound check 1', !(this._vy <= 0 && this.velocity.y > 0)); + console.log('Y rebound check 2', !(this._vy >= 0 && this.velocity.y < 0)); + } } } @@ -604,7 +614,7 @@ if (this.sprite.debug) { var gy = this.getDownwardForce(); - if ((this.blocked.up && (gy < 0 || this.velocity.y < 0)) || (this.blocked.down && (gy > 0 || this.velocity.y > 0))) + if (((this.blocked.up || this.touching.up) && (gy < 0 || this.velocity.y < 0)) || ((this.blocked.down || this.touching.down) && (gy > 0 || this.velocity.y > 0))) { this.velocity.y = 0; @@ -613,11 +623,11 @@ if (this.sprite.debug) console.log('reboundCheck Y zeroed'); } } - } - if (this.sprite.debug) - { - console.log('reboundCheck Y', this.velocity.y, 'gravity', gy); + if (this.sprite.debug) + { + console.log('reboundCheck Y', this.velocity.y, 'gravity', gy); + } } } @@ -989,12 +999,17 @@ if (this.sprite.debug) if (hasSeparated) { + console.log('Body hasSeparated'); this.game.physics.checkBounds(this); this.game.physics.checkBounds(body); } else { - this.addContact(body); + // They can only contact like this if at least one of their sides is open, otherwise it's a separation + if (!this.checkCollision.up || !this.checkCollision.down || !this.checkCollision.left || !this.checkCollision.right || !body.checkCollision.up || !body.checkCollision.down || !body.checkCollision.left || !body.checkCollision.right) + { + this.addContact(body); + } } return hasSeparated; diff --git a/src/utils/Debug.js b/src/utils/Debug.js index 0826208f..195e4c53 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -433,7 +433,7 @@ Phaser.Utils.Debug.prototype = { this.start(x, y, color, 210); this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2), 'width: ' + sprite.width, 'height: ' + sprite.height); - this.splitline('speed: ' + sprite.body.speed.toFixed(2), 'angle: ' + sprite.body.angle.toFixed(2), 'friction: ' + sprite.body.friction); + this.splitline('speed: ' + sprite.body.speed.toFixed(2), 'angle: ' + sprite.body.angle.toFixed(2), 'linear damping: ' + sprite.body.linearDamping); 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); this.splitline('gravity x: ' + sprite.body.gravity.x, 'y: ' + sprite.body.gravity.y, 'world gravity x: ' + this.game.physics.gravity.x, 'y: ' + this.game.physics.gravity.y);