Skip to content

Commit 886618c

Browse files
committed
Tilemap collision fixed, regardless of rotation, number of overlapping tiles and speed (to a point anyway). Maps also don't crash if they are smaller than the render area. Layers can be positioned successfully anywhere in camera but collision isn't yet offset for this.
1 parent ca3c71e commit 886618c

7 files changed

Lines changed: 174 additions & 297 deletions

File tree

examples/wip/tilemap.js

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ function preload() {
55

66
game.load.tilemap('map', 'assets/maps/newtest.json', null, Phaser.Tilemap.TILED_JSON);
77
game.load.tileset('tiles', 'assets/maps/ground_1x1.png', 32, 32);
8-
// game.load.image('phaser', 'assets/sprites/phaser-ship.png');
8+
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
99
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
1010
// game.load.image('phaser', 'assets/sprites/wabbit.png');
1111
// game.load.image('phaser', 'assets/sprites/arrow.png');
12-
game.load.image('phaser', 'assets/sprites/darkwing_crazy.png');
12+
// game.load.image('phaser', 'assets/sprites/darkwing_crazy.png');
1313

1414
}
1515

1616
var cursors;
1717
var map;
1818
var layer;
19+
var layer2;
1920
var sprite;
2021

2122
function create() {
@@ -28,48 +29,63 @@ function create() {
2829

2930
// Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset, tilemap, layer) {
3031

31-
layer = game.add.tilemapLayer(0, 0, 800, 600, null, map, 0);
32+
// layer = game.add.tilemapLayer(0, 0, 800, 600, null, map, 0);
33+
layer = game.add.tilemapLayer(0, 0, 400, 600, null, map, 0);
34+
35+
// layer2 = game.add.tilemapLayer(0, 0, 400, 600, null, map, 0);
36+
layer.cameraOffset.x = 400;
3237

3338
// tileset = game.add.tileset('tilesNes');
3439
// layer = game.add.tilemapLayer(0, 0, map.layers[0].width*tilesetNes.tileWidth, 600, tileset, map, 0);
35-
3640
// disable this and you can place anywhere, but will almost certainly screw collision
37-
// layer.fixedToCamera=false;
3841

39-
// this screws something up - not quite sure what, but needs investigating!
40-
// layer.resizeWorld();
42+
layer.resizeWorld();
4143

42-
sprite = game.add.sprite(200, 420, 'phaser');
44+
sprite = game.add.sprite(260, 100, 'phaser');
4345
sprite.anchor.setTo(0.5, 0.5);
44-
sprite.angle = 35;
4546

46-
// game.camera.follow(sprite);
47+
// We'll set a lower max angular velocity here to keep it from going totally nuts
48+
sprite.body.maxAngular = 500;
4749

48-
cursors = game.input.keyboard.createCursorKeys();
50+
// Apply a drag otherwise the sprite will just spin and never slow down
51+
sprite.body.angularDrag = 50;
52+
53+
// sprite.body.drag.x = 50;
54+
// sprite.body.drag.y = 20;
55+
56+
// sprite.body.velocity.x = 50;
4957

50-
game.input.onDown.add(dump, this);
58+
sprite.body.bounce.x = 0.8;
59+
sprite.body.bounce.y = 0.8;
5160

61+
// sprite.angle = 35;
62+
63+
game.camera.follow(sprite);
64+
65+
game.input.onDown.add(getIt, this);
66+
67+
cursors = game.input.keyboard.createCursorKeys();
5268

5369
}
5470

55-
function dump() {
71+
function getIt() {
5672

57-
console.log(sprite.bounds);
58-
console.log(sprite.body.hull);
59-
console.log('wy', sprite.world.y);
73+
console.log('cam', game.camera.bounds);
74+
console.log('w', game.world.bounds);
75+
// console.log(layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true, true));
6076

6177
}
6278

6379
function update() {
6480

65-
/*
81+
/*
6682
if (cursors.left.isDown)
6783
{
68-
layer.scrollX -= 4;
84+
game.camera.x -= 1;
6985
}
7086
else if (cursors.right.isDown)
7187
{
72-
layer.scrollX += 4;
88+
game.camera.x += 1;
7389
}
7490
7591
if (cursors.up.isDown)
@@ -80,10 +96,42 @@ function update() {
8096
{
8197
layer.scrollY += 4;
8298
}
83-
*/
84-
99+
*/
85100
game.physics.collide(sprite, layer);
86101

102+
sprite.body.velocity.x = 0;
103+
sprite.body.velocity.y = 0;
104+
sprite.body.angularVelocity = 0;
105+
106+
// sprite.body.acceleration.x = 0;
107+
// sprite.body.angularAcceleration = 0;
108+
109+
if (cursors.left.isDown)
110+
{
111+
// sprite.body.acceleration.x = -200;
112+
sprite.body.angularVelocity = -300;
113+
// sprite.body.angularAcceleration -= 200;
114+
}
115+
else if (cursors.right.isDown)
116+
{
117+
// sprite.body.acceleration.x = 200;
118+
sprite.body.angularVelocity = 300;
119+
// sprite.body.angularAcceleration += 200;
120+
}
121+
122+
if (cursors.up.isDown)
123+
{
124+
game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
125+
}
126+
else
127+
{
128+
// game.physics.velocityFromAngle(sprite.angle, sprite.body.velocity, sprite.body.velocity);
129+
}
130+
131+
132+
133+
134+
/*
87135
sprite.body.velocity.x = 0;
88136
sprite.body.velocity.y = 0;
89137
@@ -108,6 +156,7 @@ function update() {
108156
sprite.body.velocity.x = 900;
109157
// sprite.scale.x = 1;
110158
}
159+
*/
111160

112161

113162
}
@@ -117,7 +166,10 @@ function render() {
117166
// game.debug.renderSpriteBody(sprite);
118167
game.debug.renderSpriteBounds(sprite);
119168

120-
game.debug.renderText(sprite.x, 32, 32);
121-
game.debug.renderText(sprite.y, 32, 48);
169+
// game.debug.renderText(sprite.x, 32, 32);
170+
// game.debug.renderText(sprite.y, 32, 48);
171+
172+
game.debug.renderText(layer.scrollX, 32, 32);
173+
game.debug.renderText(layer.scrollY, 32, 48);
122174

123175
}

src/core/Camera.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,28 @@ Phaser.Camera.prototype = {
207207

208208
if (this.deadzone)
209209
{
210-
this._edge = this.target.x - this.deadzone.x;
210+
this._edge = this.target.bounds.x - this.deadzone.x;
211211

212212
if (this.view.x > this._edge)
213213
{
214214
this.view.x = this._edge;
215215
}
216216

217-
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
217+
this._edge = this.target.bounds.right - this.deadzone.x - this.deadzone.width;
218218

219219
if (this.view.x < this._edge)
220220
{
221221
this.view.x = this._edge;
222222
}
223223

224-
this._edge = this.target.y - this.deadzone.y;
224+
this._edge = this.target.bounds.y - this.deadzone.y;
225225

226226
if (this.view.y > this._edge)
227227
{
228228
this.view.y = this._edge;
229229
}
230230

231-
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
231+
this._edge = this.target.bounds.bottom - this.deadzone.y - this.deadzone.height;
232232

233233
if (this.view.y < this._edge)
234234
{

src/core/World.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Phaser.World.prototype.postUpdate = function () {
130130

131131
/**
132132
* Updates the size of this world. Note that this doesn't modify the world x/y coordinates, just the width and height.
133-
* If you need to adjust the bounds of the world
133+
*
134134
* @method Phaser.World#setBounds
135135
* @param {number} x - Top left most corner of the world.
136136
* @param {number} y - Top left most corner of the world.
@@ -139,10 +139,22 @@ Phaser.World.prototype.postUpdate = function () {
139139
*/
140140
Phaser.World.prototype.setBounds = function (x, y, width, height) {
141141

142+
if (width < this.game.width)
143+
{
144+
width = this.game.width;
145+
}
146+
147+
if (height < this.game.height)
148+
{
149+
height = this.game.height;
150+
}
151+
142152
this.bounds.setTo(x, y, width, height);
143153

144154
if (this.camera.bounds)
145155
{
156+
// The Camera can never be smaller than the game size
157+
146158
this.camera.bounds.setTo(x, y, width, height);
147159
}
148160

src/gameobjects/Sprite.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,6 @@ Phaser.Sprite.prototype.postUpdate = function() {
680680
if (this.body)
681681
{
682682
this.body.postUpdate();
683-
this.x = Math.round(this.x);
684-
this.y = Math.round(this.y);
685683
}
686684

687685
if (this.fixedToCamera)

0 commit comments

Comments
 (0)