Skip to content

Commit bedf81a

Browse files
committed
Add angle distance functions PR #7092
1 parent 1fa02c0 commit bedf81a

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author samme
3+
* @copyright 2025 Phaser Studio Inc.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var NormalizeAngle = require('./Normalize');
8+
9+
/**
10+
* Gets the shortest nonnegative angular distance from angle1 to angle2.
11+
*
12+
* @function Phaser.Math.Angle.GetClockwiseDistance
13+
* @since 4.0.0
14+
*
15+
* @param {number} angle1 - The starting angle in radians.
16+
* @param {number} angle2 - The target angle in radians.
17+
*
18+
* @return {number} The distance in radians, in the range [0, 2pi).
19+
*/
20+
var GetClockwiseDistance = function (angle1, angle2)
21+
{
22+
return NormalizeAngle(angle2 - angle1);
23+
};
24+
25+
module.exports = GetClockwiseDistance;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author samme
3+
* @copyright 2025 Phaser Studio Inc.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var MATH_CONST = require('../const');
8+
var NormalizeAngle = require('./Normalize');
9+
10+
var TAU = MATH_CONST.TAU;
11+
12+
/**
13+
* Gets the shortest nonpositive angular distance from angle1 to angle2.
14+
*
15+
* @function Phaser.Math.Angle.GetCounterClockwiseDistance
16+
* @since 4.0.0
17+
*
18+
* @param {number} angle1 - The starting angle in radians.
19+
* @param {number} angle2 - The target angle in radians.
20+
*
21+
* @return {number} The distance in radians, in the range (-2pi, 0].
22+
*/
23+
var GetCounterClockwiseDistance = function (angle1, angle2)
24+
{
25+
var distance = NormalizeAngle(angle2 - angle1);
26+
27+
if (distance > 0)
28+
{
29+
distance -= TAU;
30+
}
31+
32+
return distance;
33+
};
34+
35+
module.exports = GetCounterClockwiseDistance;

src/math/angle/GetShortestDistance.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author samme
3+
* @copyright 2025 Phaser Studio Inc.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var WrapAngle = require('./Wrap');
8+
9+
/**
10+
* Gets the shortest signed angular distance from angle1 to angle2.
11+
* A positive distance is a clockwise rotation.
12+
* A negative distance is a counter-clockwise rotation.
13+
*
14+
* For calculation in degrees use {@link Phaser.Math.Angle.ShortestBetween} instead.
15+
*
16+
* @function Phaser.Math.Angle.GetShortestDistance
17+
* @since 4.0.0
18+
*
19+
* @param {number} angle1 - The first angle in radians.
20+
* @param {number} angle2 - The second angle in radians.
21+
*
22+
* @return {number} The distance in radians, in the range [-pi, pi).
23+
*/
24+
var GetShortestDistance = function (angle1, angle2)
25+
{
26+
return WrapAngle(angle2 - angle1);
27+
};
28+
29+
module.exports = GetShortestDistance;

src/math/angle/ShortestBetween.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* greater than 0 then it's a counter-clockwise rotation, if < 0 then it's
1616
* a clockwise rotation.
1717
*
18+
* For calculation in radians use {@link Phaser.Math.Angle.GetShortestDistance} instead.
19+
*
1820
* @function Phaser.Math.Angle.ShortestBetween
1921
* @since 3.0.0
2022
*

src/math/angle/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = {
1515
BetweenPointsY: require('./BetweenPointsY'),
1616
BetweenY: require('./BetweenY'),
1717
CounterClockwise: require('./CounterClockwise'),
18+
GetClockwiseDistance: require('./GetClockwiseDistance'),
19+
GetCounterClockwiseDistance: require('./GetCounterClockwiseDistance'),
20+
GetShortestDistance: require('./GetShortestDistance'),
1821
Normalize: require('./Normalize'),
1922
Random: require('./Random'),
2023
RandomDegrees: require('./RandomDegrees'),

0 commit comments

Comments
 (0)