Skip to content

Commit ba76c22

Browse files
committed
Added new Angle functions.
1 parent 0fce66c commit ba76c22

5 files changed

Lines changed: 102 additions & 2 deletions

File tree

v3/src/math/Within.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Checks if two values are within the given tolerance of each other.
3+
*
4+
* @method Phaser.Math#within
5+
* @param {number} a - The first number to check
6+
* @param {number} b - The second number to check
7+
* @param {number} tolerance - The tolerance. Anything equal to or less than this is considered within the range.
8+
* @return {boolean} True if a is <= tolerance of b.
9+
* @see {@link Phaser.Math.fuzzyEqual}
10+
*/
11+
var Within = function (a, b, tolerance)
12+
{
13+
return (Math.abs(a - b) <= tolerance);
14+
};
15+
16+
module.exports = Within;

v3/src/math/angle/RotateTo.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Rotates currentAngle towards targetAngle, taking the shortest rotation distance.
3+
* The lerp argument is the amount to rotate by in this call.
4+
*
5+
* @method Phaser.Math#rotateToAngle
6+
* @param {number} currentAngle - The current angle, in radians.
7+
* @param {number} targetAngle - The target angle to rotate to, in radians.
8+
* @param {number} [lerp=0.05] - The lerp value to add to the current angle.
9+
* @return {number} The adjusted angle.
10+
*/
11+
var RotateTo = function (currentAngle, targetAngle, lerp)
12+
{
13+
if (lerp === undefined) { lerp = 0.05; }
14+
15+
if (currentAngle === targetAngle)
16+
{
17+
return currentAngle;
18+
}
19+
20+
if (Math.abs(targetAngle - currentAngle) <= lerp || Math.abs(targetAngle - currentAngle) >= (Phaser.Math.PI2 - lerp))
21+
{
22+
currentAngle = targetAngle;
23+
}
24+
else
25+
{
26+
if (Math.abs(targetAngle - currentAngle) > Math.PI)
27+
{
28+
if (targetAngle < currentAngle)
29+
{
30+
targetAngle += Phaser.Math.PI2;
31+
}
32+
else
33+
{
34+
targetAngle -= Phaser.Math.PI2;
35+
}
36+
}
37+
38+
if (targetAngle > currentAngle)
39+
{
40+
currentAngle += lerp;
41+
}
42+
else if (targetAngle < currentAngle)
43+
{
44+
currentAngle -= lerp;
45+
}
46+
}
47+
48+
return currentAngle;
49+
};
50+
51+
module.exports = RotateTo;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Gets the shortest angle between `angle1` and `angle2`.
3+
* Both angles must be in the range -180 to 180, which is the same clamped
4+
* range that `sprite.angle` uses, so you can pass in two sprite angles to
5+
* this method, and get the shortest angle back between the two of them.
6+
*
7+
* The angle returned will be in the same range. If the returned angle is
8+
* greater than 0 then it's a counter-clockwise rotation, if < 0 then it's
9+
* a clockwise rotation.
10+
*
11+
* @method Phaser.Math#getShortestAngle
12+
* @param {number} angle1 - The first angle. In the range -180 to 180.
13+
* @param {number} angle2 - The second angle. In the range -180 to 180.
14+
* @return {number} The shortest angle, in degrees. If greater than zero it's a counter-clockwise rotation.
15+
*/
16+
var ShortestBetween = function (angle1, angle2)
17+
{
18+
var difference = angle2 - angle1;
19+
20+
if (difference === 0)
21+
{
22+
return 0;
23+
}
24+
25+
var times = Math.floor((difference - (-180)) / 360);
26+
27+
return difference - (times * 360);
28+
29+
};
30+
31+
module.exports = ShortestBetween;

v3/src/math/angle/Wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var MathWrap from '../Wrap';
1+
var MathWrap = require('../Wrap');
22

3-
var Wrap (angle)
3+
var Wrap = function (angle)
44
{
55
return MathWrap(angle, -Math.PI, Math.PI);
66
};

v3/src/math/angle/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = {
55
BetweenPoints: require('./BetweenPoints'),
66
BetweenPointsY: require('./BetweenPointsY'),
77
Reverse: require('./Reverse'),
8+
RotateTo: require('./RotateTo'),
9+
ShortestBetween: require('./ShortestBetween'),
810
Normalize: require('./Normalize'),
911
Wrap: require('./Wrap'),
1012
WrapDegrees: require('./WrapDegrees')

0 commit comments

Comments
 (0)