Class Vector3Extensions
Extension methods for Vector3, providing randomization and geometric utilities for spheres and toroids.
public static class Vector3Extensions
- Inheritance
-
Vector3Extensions
- Inherited Members
Methods
GetNearestPointOnFlatToroid(Vector3, Vector3, float, float)
Gets the nearest point on a flat toroid (2D ring) from pointA, clamped to the toroid's bounds.
public static Vector3 GetNearestPointOnFlatToroid(Vector3 pointA, Vector3 pointB, float R, float radius)
Parameters
pointAVector3The point to project from.
pointBVector3The center of the toroid.
RfloatMajor radius.
radiusfloatTube radius.
Returns
- Vector3
The nearest point on the flat toroid.
GetNearestPositionOnSphere(Vector3, Vector3, float)
Gets the nearest position on the surface of a sphere centered at pointB, from pointA.
public static Vector3 GetNearestPositionOnSphere(Vector3 pointA, Vector3 pointB, float radius)
Parameters
pointAVector3The point to project from.
pointBVector3The center of the sphere.
radiusfloatThe radius of the sphere.
Returns
- Vector3
The nearest position on the sphere surface.
GetRandomConicalDirection(Vector3, float, DeterministicRNG)
A rotation looking along a direction drawn uniformly from inside a cone opening along
forward.
public static Quaternion GetRandomConicalDirection(this Vector3 forward, float coneAngleDegrees, DeterministicRNG random)
Parameters
forwardVector3Axis the cone opens along. A degenerate vector yields the identity rotation.
coneAngleDegreesfloatTOTAL spread of the cone. Zero or less means no deviation; 360 or more is the whole sphere.
randomDeterministicRNGDeterministic generator. Required — a shared or per-process stream would desynchronise peers.
Returns
- Quaternion
A rotation whose forward axis is the sampled direction.
Remarks
This used to read Quaternion.Euler(startPosition - ((distance * forward) + (RandomOnUnitSphere(random) * coneRadius))),
which fed a WORLD POSITION into Euler(float, float, float) as if it were Euler angles in
degrees. The result was a function of the object's absolute map coordinates rather than of
its heading, the arc was applied as a sphere radius, and the same ability produced unrelated
rotations at different places in the world. The start position and distance are gone because
a direction does not depend on either.
Sampling is uniform over the spherical cap, not over the polar angle: drawing
cos(theta) flat keeps the density even across the cap, where drawing theta flat
would bunch results towards the axis. Every draw comes from
random so two peers running the same ability with the same generator
state produce the same direction.
GetRandomPointInFlatToroid(float, float)
Returns a random point inside a flat toroid (2D ring) with major radius R and tube radius.
public static Vector3 GetRandomPointInFlatToroid(float R, float radius)
Parameters
Returns
- Vector3
A random Vector3 inside the flat toroid (z = 0).
GetRandomPointInToroid(float, float)
Returns a random point inside a 3D toroid (donut shape) with major radius R and tube radius.
public static Vector3 GetRandomPointInToroid(float R, float radius)
Parameters
Returns
- Vector3
A random Vector3 inside the toroid.
RandomInBoundingBox(Vector3)
Returns a random position inside a bounding box centered at the origin.
public static Vector3 RandomInBoundingBox(Vector3 boundingBox)
Parameters
boundingBoxVector3The size of the bounding box.
Returns
- Vector3
A random Vector3 inside the bounding box.
RandomOnUnitSphere(DeterministicRNG)
Returns a random point on the surface of a unit sphere using a provided or new DeterministicRNG.
public static Vector3 RandomOnUnitSphere(DeterministicRNG random = null)
Parameters
randomDeterministicRNGOptional random number generator.
Returns
- Vector3
A random Vector3 on the unit sphere.
RandomPositionWithinRadius(Vector3, float)
Returns a random position within a circle of given radius around a center point (on the XZ plane).
public static Vector3 RandomPositionWithinRadius(Vector3 center, float radius)
Parameters
centerVector3The center position.
radiusfloatThe radius of the circle.
Returns
- Vector3
A random Vector3 position within the radius.