Skip to content

Commit 257cbe3

Browse files
committed
Much more stable collision, just need to refactor the Tilemap handling - see if I can optimise it a bit too.
1 parent bc02a1a commit 257cbe3

12 files changed

Lines changed: 491 additions & 334 deletions

File tree

134 KB
Loading

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Version 1.0.6 (in progress)
4646
* 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)
4747
* New: BitmapText now supports anchor and has fixed box dimensions (thanks TheJare)
4848
* Fixed bug where if a State contains an empty Preloader the Update will not be called (thanks TheJare)
49+
* Added World.postUpdate - all sprite position changes, as a result of physics, happen here before the render.
50+
* Complete overhaul of Physics.Arcade.Body - now significantly more stable and faster too.
51+
* Updated ArcadePhysics.separateX/Y to use new body system - much better results now.
52+
* QuadTree bug found in 1.0.5 now fixed. The QuadTree is updated properly now using worldTransform values.
53+
* Several new examples added (cameras, tweens, etc)
4954

5055

5156

examples/camera/camera cull.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
$title = "Camera Cull";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
11+
12+
function preload() {
13+
game.load.image('disk', 'assets/sprites/ra_dont_crack_under_pressure.png');
14+
}
15+
16+
var s;
17+
18+
function create() {
19+
20+
game.stage.backgroundColor = '#182d3b';
21+
22+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'disk');
23+
s.anchor.setTo(0.5, 0.5);
24+
25+
}
26+
27+
function update() {
28+
29+
s.rotation += 0.01;
30+
31+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
32+
{
33+
s.x -= 4;
34+
}
35+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
36+
{
37+
s.x += 4;
38+
}
39+
40+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
41+
{
42+
s.y -= 4;
43+
}
44+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
45+
{
46+
s.y += 4;
47+
}
48+
49+
}
50+
51+
function render() {
52+
53+
game.debug.renderSpriteCorners(s, true, true);
54+
game.debug.renderSpriteInfo(s, 20, 32);
55+
56+
}
57+
58+
})();
59+
60+
</script>
61+
62+
<?php
63+
require('../foot.php');
64+
?>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
$title = "Moving the Camera";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
11+
12+
function preload() {
13+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
14+
}
15+
16+
function create() {
17+
18+
game.stage.backgroundColor = '#2d2d2d';
19+
20+
// Make our game world 2000x2000 pixels in size (the default is to match the game size)
21+
game.world.setSize(2000, 2000);
22+
23+
for (var i = 0; i < 50; i++)
24+
{
25+
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
26+
s.scrollFactor.setTo(0.5, 0.5);
27+
}
28+
29+
for (var i = 0; i < 50; i++)
30+
{
31+
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
32+
}
33+
34+
for (var i = 0; i < 50; i++)
35+
{
36+
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
37+
s.scrollFactor.setTo(2, 2);
38+
}
39+
40+
}
41+
42+
function update() {
43+
44+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
45+
{
46+
game.camera.x -= 4;
47+
}
48+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
49+
{
50+
game.camera.x += 4;
51+
}
52+
53+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
54+
{
55+
game.camera.y -= 4;
56+
}
57+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
58+
{
59+
game.camera.y += 4;
60+
}
61+
62+
}
63+
64+
})();
65+
66+
</script>
67+
68+
<?php
69+
require('../foot.php');
70+
?>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
$title = "Vertical bounding box";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
11+
12+
function preload() {
13+
14+
game.load.image('atari', 'assets/sprites/atari130xe.png');
15+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
16+
17+
}
18+
19+
var sprite1;
20+
var sprite2;
21+
22+
function create() {
23+
24+
game.stage.backgroundColor = '#2d2d2d';
25+
26+
sprite1 = game.add.sprite(300, 50, 'atari');
27+
sprite1.name = 'atari';
28+
sprite1.body.velocity.y = 100;
29+
30+
// This adjusts the collision body size.
31+
// 100x100 is the new width/height.
32+
// See the offset bounding box for another example.
33+
sprite1.body.setSize(220, 50, 0, 0);
34+
35+
sprite2 = game.add.sprite(400, 500, 'mushroom');
36+
sprite2.name = 'mushroom';
37+
sprite2.body.immovable = true;
38+
// sprite2.body.velocity.x = -100;
39+
40+
}
41+
42+
function update() {
43+
44+
// object1, object2, collideCallback, processCallback, callbackContext
45+
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
46+
47+
}
48+
49+
function collisionHandler (obj1, obj2) {
50+
51+
game.stage.backgroundColor = '#992d2d';
52+
53+
console.log(obj1.name + ' collided with ' + obj2.name);
54+
55+
}
56+
57+
function render() {
58+
59+
game.debug.renderSpriteInfo(sprite1, 32, 32);
60+
game.debug.renderSpriteCollision(sprite1, 32, 400);
61+
62+
game.debug.renderSpriteBody(sprite1);
63+
game.debug.renderSpriteBody(sprite2);
64+
65+
}
66+
67+
})();
68+
</script>
69+
70+
<?php
71+
require('../foot.php');
72+
?>

examples/tweens/chained tweens.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
(function () {
99

10-
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
1111

1212
var p;
1313

@@ -19,23 +19,17 @@ function preload() {
1919

2020
function create() {
2121

22-
game.stage.backgroundColor = 0x337799;
22+
game.stage.backgroundColor = 0x2d2d2d;
2323

24-
p = game.add.sprite(0, 0, 'diamond');
24+
p = game.add.sprite(100, 100, 'diamond');
2525

26-
game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true)
26+
game.add.tween(p).to({ x: 600 }, 2000, Phaser.Easing.Linear.None, true)
2727
.to({ y: 300 }, 1000, Phaser.Easing.Linear.None)
28-
.to({ x: 0 }, 1000, Phaser.Easing.Linear.None)
29-
.to({ y: 0 }, 1000, Phaser.Easing.Linear.None)
28+
.to({ x: 100 }, 2000, Phaser.Easing.Linear.None)
29+
.to({ y: 100 }, 1000, Phaser.Easing.Linear.None)
3030
.loop();
3131
}
3232

33-
function update() {
34-
}
35-
36-
function render() {
37-
}
38-
3933
})();
4034
</script>
4135

src/gameobjects/Sprite.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ Phaser.Sprite.prototype.preUpdate = function() {
210210
this._cache.dirty = true;
211211
}
212212

213-
// this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
214-
// this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
215-
216213
if (this.visible)
217214
{
218215
this.renderOrderID = this.game.world.currentRenderOrderID++;
@@ -299,10 +296,14 @@ Phaser.Sprite.prototype.postUpdate = function() {
299296
// The sprite is positioned in this call, after taking into consideration motion updates and collision
300297
this.body.postUpdate();
301298

302-
this.position.x -= (this.game.world.camera.x * this.scrollFactor.x);
303-
this.position.y -= (this.game.world.camera.y * this.scrollFactor.y);
304-
this.x -= (this.game.world.camera.x * this.scrollFactor.x);
305-
this.y -= (this.game.world.camera.y * this.scrollFactor.y);
299+
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
300+
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
301+
302+
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
303+
{
304+
this.position.x = this._cache.x;
305+
this.position.y = this._cache.y;
306+
}
306307
}
307308

308309
}

src/geom/Rectangle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ Phaser.Rectangle.equals = function (a, b) {
648648
*/
649649
Phaser.Rectangle.intersection = function (a, b, out) {
650650

651-
out = out || new Phaser.Rectangle;
651+
out = out || new Phaser.Rectangle;
652652

653653
if (Phaser.Rectangle.intersects(a, b))
654654
{

0 commit comments

Comments
 (0)