Skip to content

Commit 85e75ea

Browse files
committed
Fix for coord transform between world <-> tile + expose tile -> world methods
1 parent bde77f7 commit 85e75ea

9 files changed

Lines changed: 138 additions & 12 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,43 @@ var Tilemap = new Class({
11571157
/**
11581158
* See component documentation. If no layer specified, the map's current layer is used.
11591159
*
1160-
* @return {number|null} Returns this, or null if the layer given was invalid.
1160+
* @return {number|null} Returns a number, or null if the layer given was invalid.
1161+
*/
1162+
tileToWorldX: function (tileX, camera, layer)
1163+
{
1164+
layer = this.getLayer(layer);
1165+
if (layer === null) { return null; }
1166+
return TilemapComponents.TileToWorldX(tileX, camera, layer);
1167+
},
1168+
1169+
/**
1170+
* See component documentation. If no layer specified, the map's current layer is used.
1171+
*
1172+
* @return {number|null} Returns a number, or null if the layer given was invalid.
1173+
*/
1174+
tileToWorldY: function (tileX, camera, layer)
1175+
{
1176+
layer = this.getLayer(layer);
1177+
if (layer === null) { return null; }
1178+
return TilemapComponents.TileToWorldY(tileX, camera, layer);
1179+
},
1180+
1181+
/**
1182+
* See component documentation. If no layer specified, the map's current layer is used.
1183+
*
1184+
* @return {Vector2|null} Returns a point, or null if the layer given was invalid.
1185+
*/
1186+
tileToWorldXY: function (tileX, tileY, point, camera, layer)
1187+
{
1188+
layer = this.getLayer(layer);
1189+
if (layer === null) { return null; }
1190+
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, layer);
1191+
},
1192+
1193+
/**
1194+
* See component documentation. If no layer specified, the map's current layer is used.
1195+
*
1196+
* @return {number|null} Returns a number, or null if the layer given was invalid.
11611197
*/
11621198
worldToTileX: function (worldX, snapToFloor, camera, layer)
11631199
{
@@ -1169,7 +1205,7 @@ var Tilemap = new Class({
11691205
/**
11701206
* See component documentation. If no layer specified, the map's current layer is used.
11711207
*
1172-
* @return {number|null} Returns this, or null if the layer given was invalid.
1208+
* @return {number|null} Returns a number, or null if the layer given was invalid.
11731209
*/
11741210
worldToTileY: function (worldY, snapToFloor, camera, layer)
11751211
{
@@ -1181,7 +1217,7 @@ var Tilemap = new Class({
11811217
/**
11821218
* See component documentation. If no layer specified, the map's current layer is used.
11831219
*
1184-
* @return {Vector|null} Returns this, or null if the layer given was invalid.
1220+
* @return {Vector2|null} Returns a point, or null if the layer given was invalid.
11851221
*/
11861222
worldToTileXY: function (worldX, worldY, snapToFloor, point, camera, layer)
11871223
{

v3/src/gameobjects/tilemap/components/TileToWorldX.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Internally used method to convert from tile X coordinates to world X coordinates, factoring in
3-
* layer position, scale and scroll.
2+
* Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the
3+
* layer's position, scale and scroll.
44
*
55
* @param {integer} tileX - [description]
66
* @param {Camera} [camera=main camera] - [description]
@@ -17,7 +17,7 @@ var TileToWorldX = function (tileX, camera, layer)
1717
{
1818
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
1919

20-
layerWorldX = tilemapLayer.x - (camera.scrollX * tilemapLayer.scrollFactorX);
20+
layerWorldX = tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX);
2121

2222
tileWidth *= tilemapLayer.scaleX;
2323
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var TileToWorldX = require('./TileToWorldX');
2+
var TileToWorldY = require('./TileToWorldY');
3+
var Vector2 = require('../../../math/Vector2');
4+
5+
/**
6+
* Converts from tile XY coordinates (tile units) to world XY coordinates (pixels), factoring in the
7+
* layer's position, scale and scroll. This will return a new Vector2 object or update the given
8+
* `point` object.
9+
*
10+
* @param {integer} tileX - [description]
11+
* @param {integer} tileY - [description]
12+
* @param {Vector2} [point] - [description]
13+
* @param {Camera} [camera=main camera] - [description]
14+
* @param {LayerData} layer - [description]
15+
* @returns {Vector2} The XY location in world coordinates.
16+
*/
17+
var TileToWorldXY = function (tileX, tileY, point, camera, layer)
18+
{
19+
if (point === undefined) { point = new Vector2(0, 0); }
20+
21+
point.x = TileToWorldX(tileX, camera, layer);
22+
point.y = TileToWorldY(tileY, camera, layer);
23+
24+
return point;
25+
};
26+
27+
module.exports = TileToWorldXY;

v3/src/gameobjects/tilemap/components/TileToWorldY.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Internally used method to convert from tile Y coordinates to world Y coordinates, factoring in
3-
* layer position, scale and scroll.
2+
* Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
3+
* layer's position, scale and scroll.
44
*
55
* @param {integer} tileY - [description]
66
* @param {Camera} [camera=main camera] - [description]
@@ -17,7 +17,7 @@ var TileToWorldY = function (tileY, camera, layer)
1717
{
1818
if (camera === undefined) { camera = tilemapLayer.scene.cameras.main; }
1919

20-
layerWorldY = tilemapLayer.y - (camera.scrollY * tilemapLayer.scrollFactorY);
20+
layerWorldY = (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
2121

2222
tileHeight *= tilemapLayer.scaleY;
2323
}

v3/src/gameobjects/tilemap/components/WorldToTileX.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var WorldToTileX = function (worldX, snapToFloor, camera, layer)
2222

2323
// Find the world position relative to the static or dynamic layer's top left origin,
2424
// factoring in the camera's horizontal scroll
25-
worldX = worldX + (camera.scrollX * tilemapLayer.scrollFactorX) - tilemapLayer.x;
25+
worldX = worldX - (tilemapLayer.x + camera.scrollX * (1 - tilemapLayer.scrollFactorX));
2626

2727
tileWidth *= tilemapLayer.scaleX;
2828
}

v3/src/gameobjects/tilemap/components/WorldToTileY.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var WorldToTileY = function (worldY, snapToFloor, camera, layer)
2222

2323
// Find the world position relative to the static or dynamic layer's top left origin,
2424
// factoring in the camera's vertical scroll
25-
worldY = worldY + (camera.scrollY * tilemapLayer.scrollFactorY) - tilemapLayer.y;
25+
worldY = worldY - (tilemapLayer.y + camera.scrollY * (1 - tilemapLayer.scrollFactorY));
2626

2727
tileHeight *= tilemapLayer.scaleY;
2828
}

v3/src/gameobjects/tilemap/components/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module.exports = {
3131
SetTileLocationCallback: require('./SetTileLocationCallback'),
3232
Shuffle: require('./Shuffle'),
3333
SwapByIndex: require('./SwapByIndex'),
34+
TileToWorldX: require('./TileToWorldX'),
35+
TileToWorldXY: require('./TileToWorldXY'),
36+
TileToWorldY: require('./TileToWorldY'),
3437
WorldToTileX: require('./WorldToTileX'),
3538
WorldToTileXY: require('./WorldToTileXY'),
3639
WorldToTileY: require('./WorldToTileY')

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,36 @@ var DynamicTilemapLayer = new Class({
425425
return this;
426426
},
427427

428+
/**
429+
* See component documentation.
430+
*
431+
* @return {number}
432+
*/
433+
tileToWorldX: function (tileX, camera)
434+
{
435+
return TilemapComponents.TileToWorldX(tileX, camera, this.layer);
436+
},
437+
438+
/**
439+
* See component documentation.
440+
*
441+
* @return {number}
442+
*/
443+
tileToWorldY: function (tileY, camera)
444+
{
445+
return TilemapComponents.TileToWorldY(tileY, camera, this.layer);
446+
},
447+
448+
/**
449+
* See component documentation.
450+
*
451+
* @return {Vector2}
452+
*/
453+
tileToWorldXY: function (tileX, tileY, point, camera)
454+
{
455+
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer);
456+
},
457+
428458
/**
429459
* See component documentation.
430460
*

v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,36 @@ var StaticTilemapLayer = new Class({
469469
return this;
470470
},
471471

472+
/**
473+
* See component documentation.
474+
*
475+
* @return {number}
476+
*/
477+
tileToWorldX: function (tileX, camera)
478+
{
479+
return TilemapComponents.TileToWorldX(tileX, camera, this.layer);
480+
},
481+
482+
/**
483+
* See component documentation.
484+
*
485+
* @return {number}
486+
*/
487+
tileToWorldY: function (tileY, camera)
488+
{
489+
return TilemapComponents.TileToWorldY(tileY, camera, this.layer);
490+
},
491+
492+
/**
493+
* See component documentation.
494+
*
495+
* @return {Vector2}
496+
*/
497+
tileToWorldXY: function (tileX, tileY, point, camera)
498+
{
499+
return TilemapComponents.TileToWorldXY(tileX, tileY, point, camera, this.layer);
500+
},
501+
472502
/**
473503
* See component documentation.
474504
*
@@ -492,7 +522,7 @@ var StaticTilemapLayer = new Class({
492522
/**
493523
* See component documentation.
494524
*
495-
* @return {Vector}
525+
* @return {Vector2}
496526
*/
497527
worldToTileXY: function (worldX, worldY, snapToFloor, point, camera)
498528
{

0 commit comments

Comments
 (0)