phaser/src/math/RandomXYZ.js

34 lines
903 B
JavaScript
Raw Normal View History

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
/**
* 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
*
* @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
*
* @return {Phaser.Math.Vector3} The given Vector.
2017-10-06 03:52:41 +00:00
*/
var RandomXYZ = function (vec3, radius)
{
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;
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-10-06 03:52:41 +00:00
return vec3;
};
module.exports = RandomXYZ;