phaser/src/math/RotateAround.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2022-02-28 14:29:51 +00:00
* @copyright 2022 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-10-06 03:52:41 +00:00
/**
* Rotate a `point` around `x` and `y` to the given `angle`, at the same distance.
*
* In polar notation, this maps a point from (r, t) to (r, angle), vs. the origin (x, y).
2017-10-06 03:52:41 +00:00
*
* @function Phaser.Math.RotateAround
* @since 3.0.0
*
2020-04-27 15:13:17 +00:00
* @generic {Phaser.Types.Math.Vector2Like} T - [point,$return]
2020-04-24 20:30:07 +00:00
*
* @param {(Phaser.Geom.Point|object)} point - The point to be rotated.
* @param {number} x - The horizontal coordinate to rotate around.
* @param {number} y - The vertical coordinate to rotate around.
* @param {number} angle - The angle of rotation in radians.
2017-10-06 03:52:41 +00:00
*
2020-04-27 15:13:17 +00:00
* @return {Phaser.Types.Math.Vector2Like} The given point.
2017-10-06 03:52:41 +00:00
*/
var RotateAround = function (point, x, y, angle)
{
var c = Math.cos(angle);
var s = Math.sin(angle);
var tx = point.x - x;
var ty = point.y - y;
point.x = tx * c - ty * s + x;
point.y = tx * s + ty * c + y;
return point;
};
module.exports = RotateAround;