Monday, May 10, 2010

XNA Random Point In Circle

The function creates a random point in a circle on the XZ plane.

Random _random = new Random();

private Vector3 RandomPointInCircle(Vector3 position, float radius)
{
float randomRadius = radius * (float)Math.Sqrt(_random.NextDouble());

double randomAngle = _random.NextDouble() * MathHelper.TwoPi;

float x = randomRadius * (float)Math.Cos(randomAngle);
float z = randomRadius * (float)Math.Sin(randomAngle);

return new Vector3(position.X + x, position.Y, position.Z + z);
}