mirror of
https://github.com/wassname/phaser.git
synced 2026-06-28 16:20:37 +08:00
Much more stable collision, just need to refactor the Tilemap handling - see if I can optimise it a bit too.
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 134 KiB |
@@ -46,6 +46,11 @@ Version 1.0.6 (in progress)
|
||||
* New: When loading a Sprite Sheet you can now pass negative values for the frame sizes which specifies the number of rows/columns to load instead (thanks TheJare)
|
||||
* New: BitmapText now supports anchor and has fixed box dimensions (thanks TheJare)
|
||||
* Fixed bug where if a State contains an empty Preloader the Update will not be called (thanks TheJare)
|
||||
* Added World.postUpdate - all sprite position changes, as a result of physics, happen here before the render.
|
||||
* Complete overhaul of Physics.Arcade.Body - now significantly more stable and faster too.
|
||||
* Updated ArcadePhysics.separateX/Y to use new body system - much better results now.
|
||||
* QuadTree bug found in 1.0.5 now fixed. The QuadTree is updated properly now using worldTransform values.
|
||||
* Several new examples added (cameras, tweens, etc)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
$title = "Camera Cull";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
game.load.image('disk', 'assets/sprites/ra_dont_crack_under_pressure.png');
|
||||
}
|
||||
|
||||
var s;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#182d3b';
|
||||
|
||||
s = game.add.sprite(game.world.centerX, game.world.centerY, 'disk');
|
||||
s.anchor.setTo(0.5, 0.5);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
s.rotation += 0.01;
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
||||
{
|
||||
s.x -= 4;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
||||
{
|
||||
s.x += 4;
|
||||
}
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
|
||||
{
|
||||
s.y -= 4;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
|
||||
{
|
||||
s.y += 4;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderSpriteCorners(s, true, true);
|
||||
game.debug.renderSpriteInfo(s, 20, 32);
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
$title = "Moving the Camera";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
// Make our game world 2000x2000 pixels in size (the default is to match the game size)
|
||||
game.world.setSize(2000, 2000);
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
|
||||
s.scrollFactor.setTo(0.5, 0.5);
|
||||
}
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
|
||||
}
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
|
||||
s.scrollFactor.setTo(2, 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
||||
{
|
||||
game.camera.x -= 4;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
||||
{
|
||||
game.camera.x += 4;
|
||||
}
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
|
||||
{
|
||||
game.camera.y -= 4;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
|
||||
{
|
||||
game.camera.y += 4;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
$title = "Vertical bounding box";
|
||||
require('../head.php');
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('atari', 'assets/sprites/atari130xe.png');
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite1;
|
||||
var sprite2;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
sprite1 = game.add.sprite(300, 50, 'atari');
|
||||
sprite1.name = 'atari';
|
||||
sprite1.body.velocity.y = 100;
|
||||
|
||||
// This adjusts the collision body size.
|
||||
// 100x100 is the new width/height.
|
||||
// See the offset bounding box for another example.
|
||||
sprite1.body.setSize(220, 50, 0, 0);
|
||||
|
||||
sprite2 = game.add.sprite(400, 500, 'mushroom');
|
||||
sprite2.name = 'mushroom';
|
||||
sprite2.body.immovable = true;
|
||||
// sprite2.body.velocity.x = -100;
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
// object1, object2, collideCallback, processCallback, callbackContext
|
||||
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (obj1, obj2) {
|
||||
|
||||
game.stage.backgroundColor = '#992d2d';
|
||||
|
||||
console.log(obj1.name + ' collided with ' + obj2.name);
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderSpriteInfo(sprite1, 32, 32);
|
||||
game.debug.renderSpriteCollision(sprite1, 32, 400);
|
||||
|
||||
game.debug.renderSpriteBody(sprite1);
|
||||
game.debug.renderSpriteBody(sprite2);
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require('../foot.php');
|
||||
?>
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
(function () {
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
|
||||
|
||||
var p;
|
||||
|
||||
@@ -19,23 +19,17 @@
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = 0x337799;
|
||||
game.stage.backgroundColor = 0x2d2d2d;
|
||||
|
||||
p = game.add.sprite(0, 0, 'diamond');
|
||||
p = game.add.sprite(100, 100, 'diamond');
|
||||
|
||||
game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true)
|
||||
game.add.tween(p).to({ x: 600 }, 2000, Phaser.Easing.Linear.None, true)
|
||||
.to({ y: 300 }, 1000, Phaser.Easing.Linear.None)
|
||||
.to({ x: 0 }, 1000, Phaser.Easing.Linear.None)
|
||||
.to({ y: 0 }, 1000, Phaser.Easing.Linear.None)
|
||||
.to({ x: 100 }, 2000, Phaser.Easing.Linear.None)
|
||||
.to({ y: 100 }, 1000, Phaser.Easing.Linear.None)
|
||||
.loop();
|
||||
}
|
||||
|
||||
function update() {
|
||||
}
|
||||
|
||||
function render() {
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
|
||||
@@ -210,9 +210,6 @@ Phaser.Sprite.prototype.preUpdate = function() {
|
||||
this._cache.dirty = true;
|
||||
}
|
||||
|
||||
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
|
||||
if (this.visible)
|
||||
{
|
||||
this.renderOrderID = this.game.world.currentRenderOrderID++;
|
||||
@@ -299,10 +296,14 @@ Phaser.Sprite.prototype.postUpdate = function() {
|
||||
// The sprite is positioned in this call, after taking into consideration motion updates and collision
|
||||
this.body.postUpdate();
|
||||
|
||||
this.position.x -= (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this.position.y -= (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this.x -= (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this.y -= (this.game.world.camera.y * this.scrollFactor.y);
|
||||
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
|
||||
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
{
|
||||
this.position.x = this._cache.x;
|
||||
this.position.y = this._cache.y;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -648,7 +648,7 @@ Phaser.Rectangle.equals = function (a, b) {
|
||||
*/
|
||||
Phaser.Rectangle.intersection = function (a, b, out) {
|
||||
|
||||
out = out || new Phaser.Rectangle;
|
||||
out = out || new Phaser.Rectangle;
|
||||
|
||||
if (Phaser.Rectangle.intersects(a, b))
|
||||
{
|
||||
|
||||
+249
-280
@@ -380,12 +380,6 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
|
||||
|
||||
// if (this._result)
|
||||
// {
|
||||
// body1.postUpdate();
|
||||
// body2.postUpdate();
|
||||
// }
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -396,106 +390,103 @@ Phaser.Physics.Arcade.prototype = {
|
||||
*/
|
||||
separateX: function (body1, body2) {
|
||||
|
||||
// Can't separate two immovable or non-existing bodys
|
||||
// Can't separate two immovable bodies
|
||||
if (body1.immovable && body2.immovable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// First, get the two body deltas
|
||||
this._overlap = 0;
|
||||
|
||||
if (body1.deltaX() != body2.deltaX())
|
||||
// Check if the hulls actually overlap
|
||||
if (Phaser.Rectangle.intersects(body1, body2))
|
||||
{
|
||||
// Check if the X hulls actually overlap
|
||||
this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS;
|
||||
|
||||
this._bounds1.setTo(body1.x - ((body1.deltaX() > 0) ? body1.deltaX() : 0), body1.lastY, body1.width + ((body1.deltaX() > 0) ? body1.deltaX() : -body1.deltaX()), body1.height);
|
||||
this._bounds2.setTo(body2.x - ((body2.deltaX() > 0) ? body2.deltaX() : 0), body2.lastY, body2.width + ((body2.deltaX() > 0) ? body2.deltaX() : -body2.deltaX()), body2.height);
|
||||
|
||||
if ((this._bounds1.right > this._bounds2.x) && (this._bounds1.x < this._bounds2.right) && (this._bounds1.bottom > this._bounds2.y) && (this._bounds1.y < this._bounds2.bottom))
|
||||
if (body1.deltaX() == 0 && body2.deltaX() == 0)
|
||||
{
|
||||
this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS;
|
||||
// They overlap but neither of them are moving
|
||||
body1.embedded = true;
|
||||
body2.embedded = true;
|
||||
}
|
||||
else if (body1.deltaX() > body2.deltaX())
|
||||
{
|
||||
// Body1 is moving right and/or Body2 is moving left
|
||||
this._overlap = body1.x + body1.width - body2.x;
|
||||
|
||||
// If they did overlap (and can), figure out by how much and flip the corresponding flags
|
||||
if (body1.deltaX() > body2.deltaX())
|
||||
if ((this._overlap > this._maxOverlap) || body1.allowCollision.right == false || body2.allowCollision.left == false)
|
||||
{
|
||||
this._overlap = body1.x + body1.width - body2.x;
|
||||
|
||||
if ((this._overlap > this._maxOverlap) || body1.allowCollision.right == false || body2.allowCollision.left == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.right = true;
|
||||
body2.touching.left = true;
|
||||
}
|
||||
this._overlap = 0;
|
||||
}
|
||||
else if (body1.deltaX() < body2.deltaX())
|
||||
else
|
||||
{
|
||||
this._overlap = body1.x - body2.width - body2.x;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.left == false || body2.allowCollision.right == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.left = true;
|
||||
body2.touching.right = true;
|
||||
}
|
||||
body1.touching.right = true;
|
||||
body2.touching.left = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapX = this._overlap;
|
||||
body2.overlapX = this._overlap;
|
||||
|
||||
if (body1.customSeparateX || body2.customSeparateX)
|
||||
else if (body1.deltaX() < body2.deltaX())
|
||||
{
|
||||
// Body1 is moving left and/or Body2 is moving right
|
||||
this._overlap = body1.x - body2.width - body2.x;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.left == false || body2.allowCollision.right == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.left = true;
|
||||
body2.touching.right = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapX = this._overlap;
|
||||
body2.overlapX = this._overlap;
|
||||
|
||||
if (body1.customSeparateX || body2.customSeparateX)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.x;
|
||||
this._velocity2 = body2.velocity.x;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.x = body1.x - this._overlap;
|
||||
body2.x += this._overlap;
|
||||
|
||||
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
||||
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
||||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
|
||||
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
|
||||
}
|
||||
else if (!body1.immovable)
|
||||
{
|
||||
body1.x = body1.x - this._overlap;
|
||||
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
|
||||
}
|
||||
else if (!body2.immovable)
|
||||
{
|
||||
body2.x += this._overlap;
|
||||
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.x;
|
||||
this._velocity2 = body2.velocity.x;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.x = body1.x - this._overlap;
|
||||
body2.x += this._overlap;
|
||||
|
||||
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
||||
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
||||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
|
||||
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
|
||||
}
|
||||
else if (!body1.immovable)
|
||||
{
|
||||
body1.x = body1.x - this._overlap;
|
||||
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
|
||||
}
|
||||
else if (!body2.immovable)
|
||||
{
|
||||
body2.x += this._overlap;
|
||||
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -512,110 +503,110 @@ Phaser.Physics.Arcade.prototype = {
|
||||
return false;
|
||||
}
|
||||
|
||||
// First, get the two body deltas
|
||||
this._overlap = 0;
|
||||
|
||||
if (body1.deltaY() != body2.deltaY())
|
||||
// Check if the hulls actually overlap
|
||||
if (Phaser.Rectangle.intersects(body1, body2))
|
||||
{
|
||||
// Check if the Y hulls actually overlap
|
||||
this._bounds1.setTo(body1.x, body1.y - ((body1.deltaY() > 0) ? body1.deltaY() : 0), body1.width, body1.height + body1.deltaAbsY());
|
||||
this._bounds2.setTo(body2.x, body2.y - ((body2.deltaY() > 0) ? body2.deltaY() : 0), body2.width, body2.height + body2.deltaAbsY());
|
||||
this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
|
||||
|
||||
if ((this._bounds1.right > this._bounds2.x) && (this._bounds1.x < this._bounds2.right) && (this._bounds1.bottom > this._bounds2.y) && (this._bounds1.y < this._bounds2.bottom))
|
||||
if (body1.deltaY() == 0 && body2.deltaY() == 0)
|
||||
{
|
||||
this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
|
||||
// They overlap but neither of them are moving
|
||||
body1.embedded = true;
|
||||
body2.embedded = true;
|
||||
}
|
||||
else if (body1.deltaY() > body2.deltaY())
|
||||
{
|
||||
// Body1 is moving down and/or Body2 is moving up
|
||||
this._overlap = body1.y + body1.height - body2.y;
|
||||
|
||||
// If they did overlap (and can), figure out by how much and flip the corresponding flags
|
||||
if (body1.deltaY() > body2.deltaY())
|
||||
if ((this._overlap > this._maxOverlap) || body1.allowCollision.down == false || body2.allowCollision.up == false)
|
||||
{
|
||||
this._overlap = body1.y + body1.height - body2.y;
|
||||
|
||||
if ((this._overlap > this._maxOverlap) || body1.allowCollision.down == false || body2.allowCollision.up == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.down = true;
|
||||
body2.touching.up = true;
|
||||
}
|
||||
this._overlap = 0;
|
||||
}
|
||||
else if (body1.deltaY() < body2.deltaY())
|
||||
else
|
||||
{
|
||||
this._overlap = body1.y - body2.height - body2.y;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.up == false || body2.allowCollision.down == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.up = true;
|
||||
body2.touching.down = true;
|
||||
}
|
||||
body1.touching.down = true;
|
||||
body2.touching.up = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapY = this._overlap;
|
||||
body2.overlapY = this._overlap;
|
||||
|
||||
if (body1.customSeparateY || body2.customSeparateY)
|
||||
else if (body1.deltaY() < body2.deltaY())
|
||||
{
|
||||
// Body1 is moving up and/or Body2 is moving down
|
||||
this._overlap = body1.y - body2.height - body2.y;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.up == false || body2.allowCollision.down == false)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.touching.up = true;
|
||||
body2.touching.down = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
body1.overlapY = this._overlap;
|
||||
body2.overlapY = this._overlap;
|
||||
|
||||
if (body1.customSeparateY || body2.customSeparateY)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.y;
|
||||
this._velocity2 = body2.velocity.y;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.y = body1.y - this._overlap;
|
||||
body2.y += this._overlap;
|
||||
|
||||
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
||||
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
||||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
|
||||
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
|
||||
}
|
||||
else if (!body1.immovable)
|
||||
{
|
||||
body1.y = body1.y - this._overlap;
|
||||
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY()))
|
||||
{
|
||||
body1.x += body2.x - body2.lastX;
|
||||
}
|
||||
}
|
||||
else if (!body2.immovable)
|
||||
{
|
||||
body2.y += this._overlap;
|
||||
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY()))
|
||||
{
|
||||
body2.x += body1.x - body1.lastX;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
this._velocity1 = body1.velocity.y;
|
||||
this._velocity2 = body2.velocity.y;
|
||||
|
||||
if (!body1.immovable && !body2.immovable)
|
||||
{
|
||||
this._overlap *= 0.5;
|
||||
|
||||
body1.y = body1.y - this._overlap;
|
||||
body2.y += this._overlap;
|
||||
|
||||
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
||||
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
||||
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
||||
this._newVelocity1 -= this._average;
|
||||
this._newVelocity2 -= this._average;
|
||||
|
||||
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
|
||||
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
|
||||
}
|
||||
else if (!body1.immovable)
|
||||
{
|
||||
body1.y = body1.y - this._overlap;
|
||||
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body2.active && body2.moves && (body1.deltaY() > body2.deltaY()))
|
||||
{
|
||||
body1.x += body2.x - body2.lastX;
|
||||
}
|
||||
}
|
||||
else if (!body2.immovable)
|
||||
{
|
||||
body2.y += this._overlap;
|
||||
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontal moving platforms you can ride
|
||||
if (body1.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY()))
|
||||
{
|
||||
body2.x += body1.x - body1.lastX;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -626,12 +617,11 @@ Phaser.Physics.Arcade.prototype = {
|
||||
*/
|
||||
separateTile: function (object, x, y, width, height, mass, collideLeft, collideRight, collideUp, collideDown, separateX, separateY) {
|
||||
|
||||
var separatedY = this.separateTileY(object.body, x, y, width, height, mass, collideUp, collideDown, separateY);
|
||||
var separatedX = this.separateTileX(object.body, x, y, width, height, mass, collideLeft, collideRight, separateX);
|
||||
var separatedY = this.separateTileY(object.body, x, y, width, height, mass, collideUp, collideDown, separateY);
|
||||
|
||||
if (separatedX || separatedY)
|
||||
{
|
||||
object.body.postUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -653,77 +643,67 @@ Phaser.Physics.Arcade.prototype = {
|
||||
return false;
|
||||
}
|
||||
|
||||
// First, get the object delta
|
||||
this._overlap = 0;
|
||||
|
||||
// console.log('separatedX', x, y, object.deltaX());
|
||||
|
||||
if (object.deltaX() != 0)
|
||||
if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height))
|
||||
{
|
||||
this._bounds1.setTo(object.x, object.y, object.width, object.height);
|
||||
this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS;
|
||||
|
||||
if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height))
|
||||
if (object.deltaX() == 0)
|
||||
{
|
||||
// The hulls overlap, let's process it
|
||||
this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS;
|
||||
|
||||
// TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile
|
||||
// in which case we didn't ought to separate because it'll look like tunneling
|
||||
|
||||
if (object.deltaX() > 0)
|
||||
{
|
||||
// Going right ...
|
||||
this._overlap = object.x + object.width - x;
|
||||
|
||||
if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.touching.right = true;
|
||||
}
|
||||
}
|
||||
else if (object.deltaX() < 0)
|
||||
{
|
||||
// Going left ...
|
||||
this._overlap = object.x - width - x;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.touching.left = true;
|
||||
}
|
||||
}
|
||||
// Object is stuck inside a tile and not moving
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
if (separate)
|
||||
else if (object.deltaX() > 0)
|
||||
{
|
||||
object.x = object.x - this._overlap;
|
||||
// Going right ...
|
||||
this._overlap = object.x + object.width - x;
|
||||
|
||||
if (object.bounce.x == 0)
|
||||
if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft)
|
||||
{
|
||||
object.velocity.x = 0;
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.velocity.x = -object.velocity.x * object.bounce.x;
|
||||
object.touching.right = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
else if (object.deltaX() < 0)
|
||||
{
|
||||
// Going left ...
|
||||
this._overlap = object.x - width - x;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.touching.left = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
if (separate)
|
||||
{
|
||||
object.x = object.x - this._overlap;
|
||||
|
||||
if (object.bounce.x == 0)
|
||||
{
|
||||
object.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.velocity.x = -object.velocity.x * object.bounce.x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -740,77 +720,66 @@ Phaser.Physics.Arcade.prototype = {
|
||||
return false;
|
||||
}
|
||||
|
||||
// First, get the object delta
|
||||
this._overlap = 0;
|
||||
|
||||
if (object.deltaY() != 0)
|
||||
if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height))
|
||||
{
|
||||
this._bounds1.setTo(object.x, object.y, object.width, object.height);
|
||||
this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS;
|
||||
|
||||
if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height))
|
||||
if (object.deltaY() == 0)
|
||||
{
|
||||
// The hulls overlap, let's process it
|
||||
// Object is stuck inside a tile and not moving
|
||||
}
|
||||
else if (object.deltaY() > 0)
|
||||
{
|
||||
// Going down ...
|
||||
this._overlap = object.bottom - y;
|
||||
|
||||
// Not currently used, may need it so keep for now
|
||||
this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS;
|
||||
|
||||
// TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile
|
||||
// in which case we didn't ought to separate because it'll look like tunneling
|
||||
|
||||
if (object.deltaY() > 0)
|
||||
// if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap)
|
||||
if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown)
|
||||
{
|
||||
// Going down ...
|
||||
this._overlap = object.bottom - y;
|
||||
|
||||
// if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap)
|
||||
if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.touching.down = true;
|
||||
}
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Going up ...
|
||||
this._overlap = object.y - height - y;
|
||||
|
||||
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp)
|
||||
{
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.touching.up = true;
|
||||
}
|
||||
object.touching.down = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
if (separate)
|
||||
else if (object.deltaY() < 0)
|
||||
{
|
||||
object.y = object.y - this._overlap;
|
||||
// Going up ...
|
||||
this._overlap = object.y - height - y;
|
||||
|
||||
if (object.bounce.y == 0)
|
||||
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp)
|
||||
{
|
||||
object.velocity.y = 0;
|
||||
this._overlap = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.velocity.y = -object.velocity.y * object.bounce.y;
|
||||
object.touching.up = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
|
||||
if (this._overlap != 0)
|
||||
{
|
||||
if (separate)
|
||||
{
|
||||
object.y = object.y - this._overlap;
|
||||
|
||||
if (object.bounce.y == 0)
|
||||
{
|
||||
object.velocity.y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
object.velocity.y = -object.velocity.y * object.bounce.y;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
|
||||
+12
-47
@@ -9,8 +9,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
this.y = sprite.y;
|
||||
this.preX = sprite.x;
|
||||
this.preY = sprite.y;
|
||||
this.lastX = sprite.x;
|
||||
this.lastY = sprite.y;
|
||||
|
||||
// un-scaled original size
|
||||
this.sourceWidth = sprite.currentFrame.sourceSizeW;
|
||||
@@ -64,6 +62,9 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
this.overlapX = 0;
|
||||
this.overlapY = 0;
|
||||
|
||||
// If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true
|
||||
this.embedded = false;
|
||||
|
||||
this.collideWorldBounds = false;
|
||||
|
||||
};
|
||||
@@ -99,6 +100,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.touching.left = false;
|
||||
this.touching.right = false;
|
||||
|
||||
this.embedded = false;
|
||||
|
||||
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
this.rotation = this.sprite.angle;
|
||||
@@ -106,12 +109,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.x = this.preX;
|
||||
this.y = this.preY;
|
||||
|
||||
// There is a bug here in that the worldTransform values are what should be used, otherwise the quadTree gets the wrong rect given to it
|
||||
// this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
// this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
// this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
// this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
|
||||
if (this.moves)
|
||||
{
|
||||
this.game.physics.updateMotion(this);
|
||||
@@ -129,51 +126,24 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.game.physics.quadTree.insert(this);
|
||||
}
|
||||
|
||||
if (this.allowRotation)
|
||||
{
|
||||
this.sprite.angle = this.rotation;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
postUpdate: function () {
|
||||
|
||||
if (this.deltaX() != 0)
|
||||
{
|
||||
this.sprite.position.x += this.deltaX();
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
|
||||
if (this.deltaY() != 0)
|
||||
{
|
||||
this.sprite.position.y += this.deltaY();
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
|
||||
|
||||
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
|
||||
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
|
||||
|
||||
|
||||
// if (this.position.x != this._cache.x || this.position.y != this._cache.y)
|
||||
// {
|
||||
// this.position.x = this._cache.x;
|
||||
// this.position.y = this._cache.y;
|
||||
// this._cache.dirty = true;
|
||||
// }
|
||||
|
||||
|
||||
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
|
||||
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
|
||||
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
|
||||
|
||||
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
|
||||
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
|
||||
|
||||
// if (this.allowRotation)
|
||||
// {
|
||||
// this.sprite.angle = this.rotation;
|
||||
// }
|
||||
if (this.allowRotation)
|
||||
{
|
||||
this.sprite.angle = this.rotation;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
@@ -226,30 +196,25 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.angularVelocity = 0;
|
||||
this.angularAcceleration = 0;
|
||||
|
||||
// this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
// this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
// this.lastX = this.x;
|
||||
// this.lastY = this.y;
|
||||
this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
this.rotation = this.sprite.angle;
|
||||
|
||||
},
|
||||
|
||||
deltaAbsX: function () {
|
||||
// return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
|
||||
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
|
||||
},
|
||||
|
||||
deltaAbsY: function () {
|
||||
// return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
|
||||
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
|
||||
},
|
||||
|
||||
deltaX: function () {
|
||||
// return this.x - this.lastX;
|
||||
return this.x - this.preX;
|
||||
},
|
||||
|
||||
deltaY: function () {
|
||||
// return this.y - this.lastY;
|
||||
return this.y - this.preY;
|
||||
}
|
||||
|
||||
|
||||
+28
-13
@@ -121,18 +121,33 @@ Phaser.Tile.prototype = {
|
||||
this.collideUp = false;
|
||||
this.collideDown = false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method toString
|
||||
* @return {string} a string representation of the object.
|
||||
**/
|
||||
toString: function () {
|
||||
|
||||
// return "[{Tile (index=" + this.index + " collisions=" + this.allowCollisions + " width=" + this.width + " height=" + this.height + ")}]";
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
Object.defineProperty(Phaser.Tile.prototype, "bottom", {
|
||||
|
||||
/**
|
||||
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
||||
* @method bottom
|
||||
* @return {Number}
|
||||
**/
|
||||
get: function () {
|
||||
return this.y + this.height;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.Tile.prototype, "right", {
|
||||
|
||||
/**
|
||||
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
||||
* However it does affect the width property.
|
||||
* @method right
|
||||
* @return {Number}
|
||||
**/
|
||||
get: function () {
|
||||
return this.x + this.width;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -183,6 +183,8 @@ Phaser.Tilemap.prototype.parseTiledJSON = function (json, key) {
|
||||
*/
|
||||
Phaser.Tilemap.prototype.generateTiles = function (qty) {
|
||||
|
||||
console.log('generating', qty, 'tiles');
|
||||
|
||||
for (var i = 0; i < qty; i++)
|
||||
{
|
||||
this.tiles.push(new Phaser.Tile(this.game, this, i, this.currentLayer.tileWidth, this.currentLayer.tileHeight));
|
||||
|
||||
Reference in New Issue
Block a user