Skip to content

Commit 5f9bff0

Browse files
committed
Clarified the documentation for Point.rotate and fixed all the examples.
1 parent 9e78cd1 commit 5f9bff0

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/geom/Point.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,36 +785,44 @@ Phaser.Point.normalize = function (a, out) {
785785
};
786786

787787
/**
788-
* Rotates a Point around the x/y coordinates given to the desired angle.
788+
* Rotates a Point object, or any object with exposed x/y properties, around the given coordinates by
789+
* the angle specified. If the angle between the point and coordinates was 45 deg and the angle argument
790+
* is 45 deg then the resulting angle will be 90 deg, as the angle argument is added to the current angle.
791+
*
792+
* The distance allows you to specify a distance constraint for the rotation between the point and the
793+
* coordinates. If none is given the distance between the two is calculated and used.
789794
*
790795
* @method Phaser.Point.rotate
791796
* @param {Phaser.Point} a - The Point object to rotate.
792797
* @param {number} x - The x coordinate of the anchor point
793798
* @param {number} y - The y coordinate of the anchor point
794-
* @param {number} angle - The angle in radians (unless asDegrees is true) to rotate the Point to.
795-
* @param {boolean} [asDegrees=false] - Is the given rotation in radians (false) or degrees (true)?
799+
* @param {number} angle - The angle in radians (unless asDegrees is true) to rotate the Point by.
800+
* @param {boolean} [asDegrees=false] - Is the given angle to rotate by in radians (false) or degrees (true)?
796801
* @param {number} [distance] - An optional distance constraint between the Point and the anchor.
797802
* @return {Phaser.Point} The modified point object.
798803
*/
799804
Phaser.Point.rotate = function (a, x, y, angle, asDegrees, distance) {
800805

801-
asDegrees = asDegrees || false;
802-
distance = distance || null;
806+
if (typeof asDegrees === 'undefined') { asDegrees = false; }
807+
if (typeof distance === 'undefined') { distance = null; }
803808

804809
if (asDegrees)
805810
{
806811
angle = Phaser.Math.degToRad(angle);
807812
}
808813

809-
// Get distance from origin (cx/cy) to this point
810814
if (distance === null)
811815
{
816+
// Get distance from origin (cx/cy) to this point
812817
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
813818
}
814819

815-
var requiredAngle = angle + Math.atan2(a.y - y, a.x - x);
820+
var t = angle + Math.atan2(a.y - y, a.x - x);
821+
822+
a.x = x + distance * Math.cos(t);
823+
a.y = y + distance * Math.sin(t);
816824

817-
return a.setTo(x + distance * Math.cos(requiredAngle), y + distance * Math.sin(requiredAngle));
825+
return a;
818826

819827
};
820828

0 commit comments

Comments
 (0)