Skip to content

Commit a61207b

Browse files
committed
Math.ToXY is a new mini function that will take a given index and return a Vector2 containing the x and y coordinates of that index within a grid.
1 parent 8ecab7a commit a61207b

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* `TextureManager.addGLTexture` is a new method that allows you to add a WebGLTexture directly into the Texture Manager, saved under the given key.
1515
* `TextureSource.isGLTexture` is a new boolean property that reflects if the data backing the underlying Texture Source is a WebGLTexture or not.
1616
* `TextureTintPipeline.batchSprite` will now flip the UV if the TextureSource comes from a GLTexture.
17+
* `Math.ToXY` is a new mini function that will take a given index and return a Vector2 containing the x and y coordinates of that index within a grid.
1718

1819
### Updates
1920

src/math/ToXY.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Vector2 = require('./Vector2');
8+
9+
/**
10+
* Returns a Vec2 containing the x and y position of the given index in a `width` x `height` sized grid.
11+
*
12+
* For example, in a 6 x 4 grid, index 16 would equal x: 4 y: 2.
13+
*
14+
* If the given index is out of range an empty Vec2 is returned.
15+
*
16+
* @function Phaser.Math.ToXY
17+
* @since 3.19.0
18+
*
19+
* @param {integer} index - The position within the grid to get the x/y value for.
20+
* @param {integer} width - The width of the grid.
21+
* @param {integer} height - The height of the grid.
22+
* @param {Phaser.Math.Vector2} [out] - An optional Vector2 to store the result in. If not given, a new Vector2 instance will be created.
23+
*
24+
* @return {Phaser.Math.Vector2} A Vector2 where the x and y properties contain the given grid index.
25+
*/
26+
var ToXY = function (index, width, height, out)
27+
{
28+
if (out === undefined) { out = new Vector2(); }
29+
30+
var x = 0;
31+
var y = 0;
32+
var total = width * height;
33+
34+
if (index > 0 && index <= total)
35+
{
36+
if (index > width - 1)
37+
{
38+
y = Math.floor(index / width);
39+
x = index - (y * width);
40+
}
41+
else
42+
{
43+
x = index;
44+
}
45+
46+
out.set(x, y);
47+
}
48+
49+
return out;
50+
};
51+
52+
module.exports = ToXY;

src/math/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var PhaserMath = {
5757
SinCosTableGenerator: require('./SinCosTableGenerator'),
5858
SmootherStep: require('./SmootherStep'),
5959
SmoothStep: require('./SmoothStep'),
60+
ToXY: require('./ToXY'),
6061
TransformXY: require('./TransformXY'),
6162
Within: require('./Within'),
6263
Wrap: require('./Wrap'),

0 commit comments

Comments
 (0)