mirror of
https://github.com/photonstorm/phaser
synced 2025-03-02 14:27:10 +00:00
Clarified the documentation for Point.rotate and fixed all the examples.
This commit is contained in:
parent
9e78cd1d7a
commit
5f9bff0e8b
1 changed files with 16 additions and 8 deletions
|
@ -785,36 +785,44 @@ Phaser.Point.normalize = function (a, out) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotates a Point around the x/y coordinates given to the desired angle.
|
* Rotates a Point object, or any object with exposed x/y properties, around the given coordinates by
|
||||||
|
* the angle specified. If the angle between the point and coordinates was 45 deg and the angle argument
|
||||||
|
* is 45 deg then the resulting angle will be 90 deg, as the angle argument is added to the current angle.
|
||||||
|
*
|
||||||
|
* The distance allows you to specify a distance constraint for the rotation between the point and the
|
||||||
|
* coordinates. If none is given the distance between the two is calculated and used.
|
||||||
*
|
*
|
||||||
* @method Phaser.Point.rotate
|
* @method Phaser.Point.rotate
|
||||||
* @param {Phaser.Point} a - The Point object to rotate.
|
* @param {Phaser.Point} a - The Point object to rotate.
|
||||||
* @param {number} x - The x coordinate of the anchor point
|
* @param {number} x - The x coordinate of the anchor point
|
||||||
* @param {number} y - The y coordinate of the anchor point
|
* @param {number} y - The y coordinate of the anchor point
|
||||||
* @param {number} angle - The angle in radians (unless asDegrees is true) to rotate the Point to.
|
* @param {number} angle - The angle in radians (unless asDegrees is true) to rotate the Point by.
|
||||||
* @param {boolean} [asDegrees=false] - Is the given rotation in radians (false) or degrees (true)?
|
* @param {boolean} [asDegrees=false] - Is the given angle to rotate by in radians (false) or degrees (true)?
|
||||||
* @param {number} [distance] - An optional distance constraint between the Point and the anchor.
|
* @param {number} [distance] - An optional distance constraint between the Point and the anchor.
|
||||||
* @return {Phaser.Point} The modified point object.
|
* @return {Phaser.Point} The modified point object.
|
||||||
*/
|
*/
|
||||||
Phaser.Point.rotate = function (a, x, y, angle, asDegrees, distance) {
|
Phaser.Point.rotate = function (a, x, y, angle, asDegrees, distance) {
|
||||||
|
|
||||||
asDegrees = asDegrees || false;
|
if (typeof asDegrees === 'undefined') { asDegrees = false; }
|
||||||
distance = distance || null;
|
if (typeof distance === 'undefined') { distance = null; }
|
||||||
|
|
||||||
if (asDegrees)
|
if (asDegrees)
|
||||||
{
|
{
|
||||||
angle = Phaser.Math.degToRad(angle);
|
angle = Phaser.Math.degToRad(angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get distance from origin (cx/cy) to this point
|
|
||||||
if (distance === null)
|
if (distance === null)
|
||||||
{
|
{
|
||||||
|
// Get distance from origin (cx/cy) to this point
|
||||||
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
|
distance = Math.sqrt(((x - a.x) * (x - a.x)) + ((y - a.y) * (y - a.y)));
|
||||||
}
|
}
|
||||||
|
|
||||||
var requiredAngle = angle + Math.atan2(a.y - y, a.x - x);
|
var t = angle + Math.atan2(a.y - y, a.x - x);
|
||||||
|
|
||||||
return a.setTo(x + distance * Math.cos(requiredAngle), y + distance * Math.sin(requiredAngle));
|
a.x = x + distance * Math.cos(t);
|
||||||
|
a.y = y + distance * Math.sin(t);
|
||||||
|
|
||||||
|
return a;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue