Struct Capsule3D
A 3D capsule represented by two end-points PointA and PointB and a Radius. The capsule volume is the set of all points within Radius of the line segment connecting the two end-points, equivalent to a cylinder capped with two hemispherical ends.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Geom
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct Capsule3D : IEquatable<Capsule3D>
Remarks
This two-endpoint form matches the parameters of CapsuleCast(Vector3, Vector3, float, Vector3, float, int, QueryTriggerInteraction) exactly, supports arbitrary orientation without a separate quaternion field, and degenerates cleanly to a sphere when PointA equals PointB.
Radius is expected to be non-negative. Behaviour with a negative radius is undefined.
Equality comparisons use Approximately(float, float, float) for each component of PointA, PointB, and Radius, so two capsules that differ only by floating-point rounding are considered equal.
To obtain a conservative axis-aligned bounding box, call ToAABB3D(). For a standalone sphere without a central segment, see Sphere3D.
Constructors
Capsule3D(Vector3, Vector3, float)
Initializes a new Capsule3D with the supplied end-points and radius.
Declaration
public Capsule3D(Vector3 pointA, Vector3 pointB, float radius)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | pointA | One end-point of the capsule's central segment. |
| Vector3 | pointB | The other end-point of the capsule's central segment. |
| float | radius | The radius of the capsule in world units. Should be non-negative. |
Fields
PointA
One end-point of the capsule's central line segment. Together with PointB, this defines the axis of the capsule's cylindrical body.
Declaration
[SerializeField]
public Vector3 PointA
Field Value
| Type | Description |
|---|---|
| Vector3 |
PointB
The other end-point of the capsule's central line segment. Together with PointA, this defines the axis of the capsule's cylindrical body.
Declaration
[SerializeField]
public Vector3 PointB
Field Value
| Type | Description |
|---|---|
| Vector3 |
Radius
Radius of the capsule in world units. This is the radius of both the cylindrical body and the two hemispherical end-caps. Expected to be non-negative.
Declaration
[SerializeField]
public float Radius
Field Value
| Type | Description |
|---|---|
| float |
Properties
Axis
The normalized direction vector of the central segment, pointing from PointA toward PointB. Returns UnityEngine.Vector3.up when the two end-points are identical (degenerate capsule).
Declaration
public readonly Vector3 Axis { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Center
The midpoint of the capsule's central segment, equidistant from both end-points.
Declaration
public readonly Vector3 Center { get; }
Property Value
| Type | Description |
|---|---|
| Vector3 |
Height
The total height of the capsule, including both hemispherical end-caps. Equals the
length of the central segment (|PointB - PointA|) plus Radius * 2.
When PointA equals PointB (degenerate sphere) this is
simply the diameter.
Declaration
public readonly float Height { get; }
Property Value
| Type | Description |
|---|---|
| float |
Volume
The total volume enclosed by the capsule, in cubic world units. This is the sum of
the cylindrical body volume (pi * r^2 * segmentLength) and the combined
hemispherical caps, which together form a full sphere of volume
(4/3) * pi * r^3.
Declaration
public readonly float Volume { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
ClosestPoint(Vector3)
Returns the closest point on or inside the capsule to point. If
point is already inside the capsule, it is returned unchanged.
Declaration
public readonly Vector3 ClosestPoint(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The query point in world space. |
Returns
| Type | Description |
|---|---|
| Vector3 | The nearest point within the capsule volume to |
Contains(Vector3)
Returns true if point is inside or on the surface of
this capsule, i.e. if the distance from point to the central
segment is less than or equal to Radius.
Declaration
public readonly bool Contains(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The world-space point to test. |
Returns
| Type | Description |
|---|---|
| bool |
|
Distance(Vector3)
Returns the Euclidean distance from point to the capsule's
surface. Returns zero if point is inside or on the boundary.
Declaration
public readonly float Distance(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The query point in world space. |
Returns
| Type | Description |
|---|---|
| float | A non-negative distance in world units. Zero indicates the point is inside or touching the capsule. |
Equals(Capsule3D)
Returns true if this capsule and other are approximately
equal, comparing each component of PointA, PointB, and
Radius using
Approximately(float, float, float).
Declaration
public readonly bool Equals(Capsule3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| Capsule3D | other | The capsule to compare against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj |
Returns
| Type | Description |
|---|---|
| bool |
Overrides
FromAxisAligned(Vector3, Vector3, float, float)
Builds a capsule centered at center and oriented along
axis, with the given total height (including
both end-caps) and radius. The supplied axis is normalized
before use; a zero-length axis falls back to UnityEngine.Vector3.up.
Declaration
public static Capsule3D FromAxisAligned(Vector3 center, Vector3 axis, float height, float radius)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | center | The center of the capsule (midpoint of the central segment). |
| Vector3 | axis | The desired orientation of the capsule's long axis. Normalized internally; does not need to be unit length. A zero vector falls back to UnityEngine.Vector3.up. |
| float | height | The total height of the capsule including both hemispherical caps. Values smaller
than |
| float | radius | The radius of the capsule in world units. Should be non-negative. |
Returns
| Type | Description |
|---|---|
| Capsule3D | A new Capsule3D with end-points placed symmetrically around
|
GetHashCode()
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int |
Overrides
Intersects(in Capsule3D)
Returns true if this capsule overlaps other. The test
compares the closest distance between the two central segments against the sum of
both radii.
Declaration
public readonly bool Intersects(in Capsule3D other)
Parameters
| Type | Name | Description |
|---|---|---|
| Capsule3D | other | The capsule to test against. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Uses the segment-segment distance algorithm from Christer Ericson's "Real-Time Collision Detection" section 5.1.9. Degenerate cases (point-like segments) are handled without division by zero.
Intersects(in Sphere3D)
Returns true if this capsule overlaps sphere. The test
compares the distance from the sphere's center to the capsule's central segment
against the sum of both radii.
Declaration
public readonly bool Intersects(in Sphere3D sphere)
Parameters
| Type | Name | Description |
|---|---|---|
| Sphere3D | sphere | The sphere to test against. |
Returns
| Type | Description |
|---|---|
| bool |
|
ToAABB3D()
Returns the axis-aligned bounding box that encloses this capsule. The box is computed by taking the component-wise minimum and maximum of the two end-points and expanding by Radius on all sides.
Declaration
public readonly AABB3D ToAABB3D()
Returns
| Type | Description |
|---|---|
| AABB3D | An AABB3D that tightly encloses the capsule's full volume, including both hemispherical end-caps. |
ToString()
Returns a human-readable string representation of this capsule in the format
Capsule3D[A=(x,y,z), B=(x,y,z), Radius=r].
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A formatted string describing the capsule's end-points and radius. |
Overrides
Operators
operator ==(Capsule3D, Capsule3D)
Approximate equality operator. Delegates to Equals(Capsule3D); two capsules that differ only by floating-point rounding are considered equal.
Declaration
public static bool operator ==(Capsule3D left, Capsule3D right)
Parameters
| Type | Name | Description |
|---|---|---|
| Capsule3D | left | The left operand. |
| Capsule3D | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(Capsule3D, Capsule3D)
Approximate inequality operator. Returns the negation of operator ==(Capsule3D, Capsule3D).
Declaration
public static bool operator !=(Capsule3D left, Capsule3D right)
Parameters
| Type | Name | Description |
|---|---|---|
| Capsule3D | left | The left operand. |
| Capsule3D | right | The right operand. |
Returns
| Type | Description |
|---|---|
| bool |
|