2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-06 03:52:41 +00:00
|
|
|
/**
|
2018-05-23 09:46:16 +00:00
|
|
|
* Compute a random position vector in a spherical area, optionally defined by the given radius.
|
2017-10-06 03:52:41 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Math.RandomXYZ
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-05-23 09:46:16 +00:00
|
|
|
* @param {Phaser.Math.Vector3} vec3 - The Vector to compute random values for.
|
|
|
|
* @param {number} [radius=1] - The radius.
|
2017-10-06 03:52:41 +00:00
|
|
|
*
|
2018-05-23 09:46:16 +00:00
|
|
|
* @return {Phaser.Math.Vector3} The given Vector.
|
2017-10-06 03:52:41 +00:00
|
|
|
*/
|
|
|
|
var RandomXYZ = function (vec3, radius)
|
2017-09-18 20:48:26 +00:00
|
|
|
{
|
|
|
|
if (radius === undefined) { radius = 1; }
|
|
|
|
|
|
|
|
var r = Math.random() * 2 * Math.PI;
|
|
|
|
var z = (Math.random() * 2) - 1;
|
|
|
|
var zScale = Math.sqrt(1 - z * z) * radius;
|
2018-05-23 09:46:16 +00:00
|
|
|
|
2017-10-06 03:52:41 +00:00
|
|
|
vec3.x = Math.cos(r) * zScale;
|
|
|
|
vec3.y = Math.sin(r) * zScale;
|
|
|
|
vec3.z = z * radius;
|
2017-09-18 20:48:26 +00:00
|
|
|
|
2017-10-06 03:52:41 +00:00
|
|
|
return vec3;
|
2017-09-18 20:48:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RandomXYZ;
|