Mostly working, just need to apply a max overlap to avoid tunneling, although a ray would be better.

This commit is contained in:
photonstorm
2013-12-06 02:34:28 +00:00
parent 1f513a333e
commit 8e289e6b7c
3 changed files with 171 additions and 25 deletions
+14 -10
View File
@@ -6,7 +6,9 @@ function preload() {
game.load.tilemap('map', 'assets/maps/newtest.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/ground_1x1.png', 32, 32);
// game.load.image('phaser', 'assets/sprites/phaser-ship.png');
game.load.image('phaser', 'assets/sprites/mushroom2.png');
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
// game.load.image('phaser', 'assets/sprites/wabbit.png');
game.load.image('phaser', 'assets/sprites/arrow.png');
}
@@ -36,9 +38,9 @@ function create() {
// this screws something up - not quite sure what, but needs investigating!
// layer.resizeWorld();
sprite = game.add.sprite(120, 450, 'phaser');
// sprite.anchor.setTo(0.5, 0.5);
// sprite.angle = 5;
sprite = game.add.sprite(200, 420, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
sprite.angle = 35;
// game.camera.follow(sprite);
@@ -84,23 +86,25 @@ function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.angle = sprite.angle + 1;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -150;
sprite.body.velocity.y = -900;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 150;
sprite.body.velocity.y = 900;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -150;
sprite.body.velocity.x = -900;
// sprite.scale.x = -1;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 150;
sprite.body.velocity.x = 900;
// sprite.scale.x = 1;
}
@@ -109,8 +113,8 @@ function update() {
function render() {
game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteBounds(sprite);
// game.debug.renderSpriteBody(sprite);
game.debug.renderSpriteBounds(sprite);
game.debug.renderText(sprite.x, 32, 32);
game.debug.renderText(sprite.y, 32, 48);
+2
View File
@@ -680,6 +680,8 @@ Phaser.Sprite.prototype.postUpdate = function() {
if (this.body)
{
this.body.postUpdate();
this.x = Math.round(this.x);
this.y = Math.round(this.y);
}
if (this.fixedToCamera)
+155 -15
View File
@@ -566,10 +566,14 @@ Phaser.Physics.Arcade.prototype = {
return;
}
console.log('collideSpriteVsTilemapLayer', this._mapData.length);
for (var i = 0; i < this._mapData.length; i++)
if (this._mapData.length > 1)
{
// console.log(' multi sep ---------------------------------------------------------------------------------------------');
this.separateTiles(sprite.body, this._mapData);
}
else
{
var i = 0;
if (this.separateTile(sprite.body, this._mapData[i]))
{
// They collided, is there a custom process callback?
@@ -1000,14 +1004,150 @@ Phaser.Physics.Arcade.prototype = {
},
separateTiles: function (body, tiles) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable)
{
return false;
}
body.overlapX = 0;
body.overlapY = 0;
var tile;
var localOverlapX = 0;
var localOverlapY = 0;
for (var i = 0; i < tiles.length; i++)
{
tile = tiles[i];
if (Phaser.Rectangle.intersects(body, tile))
{
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
{
// LEFT
localOverlapX = body.x - tile.right;
if (localOverlapX >= body.deltaX())
{
console.log('m left overlapX', localOverlapX, body.deltaX(), this.game.time.physicsElapsed);
// use touching instead of blocked?
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
}
}
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
{
// RIGHT
localOverlapX = body.right - tile.x;
// Distance check
if (localOverlapX <= body.deltaX())
{
console.log('m right overlapX', localOverlapX, body.deltaX(), this.game.time.physicsElapsed);
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
}
}
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
{
// UP
localOverlapY = body.y - tile.bottom;
// Distance check
if (localOverlapY >= body.deltaY())
{
console.log('m up overlapY', localOverlapY, body.deltaY(), this.game.time.physicsElapsed);
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
}
}
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
{
// DOWN
localOverlapY = body.bottom - tile.y;
if (localOverlapY <= body.deltaY())
{
console.log('m down overlapY', localOverlapY, body.deltaY(), this.game.time.physicsElapsed);
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
}
}
}
}
if (localOverlapX !== 0)
{
body.overlapX = localOverlapX;
}
if (localOverlapY !== 0)
{
body.overlapY = localOverlapY;
}
if (body.touching.none)
{
return false;
}
if (body.overlapX !== 0)
{
body.x -= body.overlapX;
body.preX -= body.overlapX;
if (body.bounce.x === 0)
{
body.velocity.x = 0;
}
else
{
body.velocity.x = -body.velocity.x * body.bounce.x;
}
}
if (body.overlapY !== 0)
{
body.y -= body.overlapY;
body.preY -= body.overlapY;
if (body.bounce.y === 0)
{
body.velocity.y = 0;
}
else
{
body.velocity.y = -body.velocity.y * body.bounce.y;
}
}
return true;
},
separateTile: function (body, tile) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || Phaser.Rectangle.intersects(body, tile) === false)
{
// console.log('no intersects');
// console.log('tx', tile.x, 'ty', tile.y, 'tw', tile.width, 'th', tile.height, 'tr', tile.right, 'tb', tile.bottom);
// console.log('bx', body.x, 'by', body.y, 'bw', body.width, 'bh', body.height, 'br', body.right, 'bb', body.bottom);
return false;
}
var maxOverlapX = Math.round(tile.width / 2);
var maxOverlapY = Math.round(tile.height / 2);
// use body var instead
body.overlapX = 0;
body.overlapY = 0;
@@ -1024,9 +1164,9 @@ Phaser.Physics.Arcade.prototype = {
// LEFT
body.overlapX = body.x - tile.right;
if (body.overlapX > body.deltaX())
if (body.overlapX >= body.deltaX())
{
console.log('left overlapX', body.overlapX);
console.log('left overlapX', body.overlapX, body.deltaX(), this.game.time.physicsElapsed);
// use touching instead of blocked?
body.blocked.left = true;
body.touching.left = true;
@@ -1039,9 +1179,9 @@ Phaser.Physics.Arcade.prototype = {
body.overlapX = body.right - tile.x;
// Distance check
if (body.overlapX < body.deltaX())
if (body.overlapX <= body.deltaX())
{
console.log('right overlapX', body.overlapX);
console.log('right overlapX', body.overlapX, body.deltaX(), this.game.time.physicsElapsed);
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
@@ -1054,9 +1194,9 @@ Phaser.Physics.Arcade.prototype = {
body.overlapY = body.y - tile.bottom;
// Distance check
if (body.overlapY > body.deltaY())
if (body.overlapY >= body.deltaY())
{
console.log('up overlapY', body.overlapY);
console.log('up overlapY', body.overlapY, body.deltaY(), this.game.time.physicsElapsed);
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
@@ -1067,9 +1207,9 @@ Phaser.Physics.Arcade.prototype = {
// DOWN
body.overlapY = body.bottom - tile.y;
if (body.overlapY < body.deltaY())
if (body.overlapY <= body.deltaY())
{
console.log('down overlapY', body.overlapY);
console.log('down overlapY', body.overlapY, body.deltaY(), this.game.time.physicsElapsed);
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
@@ -1090,11 +1230,11 @@ Phaser.Physics.Arcade.prototype = {
if (body.bounce.x === 0)
{
// body.velocity.x = 0;
body.velocity.x = 0;
}
else
{
// body.velocity.x = -body.velocity.x * body.bounce.x;
body.velocity.x = -body.velocity.x * body.bounce.x;
}
}
@@ -1105,11 +1245,11 @@ Phaser.Physics.Arcade.prototype = {
if (body.bounce.y === 0)
{
// body.velocity.y = 0;
body.velocity.y = 0;
}
else
{
// body.velocity.y = -body.velocity.y * body.bounce.y;
body.velocity.y = -body.velocity.y * body.bounce.y;
}
}