| | | 1 | | //======================================================================= |
| | | 2 | | // LSSphereCollider.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Queries; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using System; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Colliders; |
| | | 15 | | |
| | | 16 | | /// <summary>Represents a runtime 3D sphere collider.</summary> |
| | | 17 | | public sealed class LSSphereCollider : LSCollider |
| | | 18 | | { |
| | 2972 | 19 | | private Fixed64 _scaledRadius = Fixed64.Half; |
| | | 20 | | private Fixed64 _preparedRadius; |
| | | 21 | | private Fixed64 _preparedArea; |
| | | 22 | | |
| | | 23 | | /// <summary>Creates a sphere collider with default dimensions.</summary> |
| | 5636 | 24 | | public LSSphereCollider() { } |
| | | 25 | | |
| | | 26 | | /// <summary>Creates a runtime sphere from an authored shape definition.</summary> |
| | 154 | 27 | | public LSSphereCollider(ColliderShapeDefinition definition) |
| | | 28 | | { |
| | 154 | 29 | | definition.EnsureKind(ColliderShapeDefinitionKind.Sphere); |
| | 153 | 30 | | Material = definition.Material; |
| | 153 | 31 | | Radius = definition.Radius; |
| | 153 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | 240342 | 35 | | public override ColliderType Shape => ColliderType.Sphere; |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | 6113 | 38 | | public override int Priority => ColliderSettings.GetPriority(Shape); |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | | 41 | | protected override void OnRadiusChanged() |
| | | 42 | | { |
| | 380 | 43 | | Fixed64 diameter = _radius * 2; |
| | 380 | 44 | | _size = new Vector3d(diameter, diameter, diameter); |
| | 380 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc/> |
| | 95374 | 48 | | public override Fixed64 ScaledRadius => _scaledRadius; |
| | | 49 | | |
| | | 50 | | private protected override void PrepareShape(in ColliderShapeSnapshot snapshot) |
| | | 51 | | { |
| | 21871 | 52 | | Fixed64 radiusX = ColliderScalePolicy.ScalePositive( |
| | 21871 | 53 | | snapshot.Radius, |
| | 21871 | 54 | | snapshot.OwnerScale.X, |
| | 21871 | 55 | | snapshot.PartScale.X); |
| | 21869 | 56 | | Fixed64 radiusY = ColliderScalePolicy.ScalePositive( |
| | 21869 | 57 | | snapshot.Radius, |
| | 21869 | 58 | | snapshot.OwnerScale.Y, |
| | 21869 | 59 | | snapshot.PartScale.Y); |
| | 21869 | 60 | | Fixed64 radiusZ = ColliderScalePolicy.ScalePositive( |
| | 21869 | 61 | | snapshot.Radius, |
| | 21869 | 62 | | snapshot.OwnerScale.Z, |
| | 21869 | 63 | | snapshot.PartScale.Z); |
| | 21869 | 64 | | _preparedRadius = FixedMath.Max(radiusX, FixedMath.Max(radiusY, radiusZ)); |
| | 21869 | 65 | | _preparedArea = Fixed64.Pi * _preparedRadius * _preparedRadius; |
| | 21869 | 66 | | SetPreparedBounds(FixedBoundBox.FromCenterAndScopeClippedToDomain( |
| | 21869 | 67 | | snapshot.Center, |
| | 21869 | 68 | | Vector3d.One * _preparedRadius)); |
| | 21869 | 69 | | } |
| | | 70 | | |
| | | 71 | | private protected override void PublishShape() |
| | | 72 | | { |
| | 21759 | 73 | | _scaledRadius = _preparedRadius; |
| | 21759 | 74 | | Area = _preparedArea; |
| | 21759 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal override ExactMassWeight CalculateMassPropertyWeight() => |
| | 487 | 78 | | ExactMassWeight.FromProduct( |
| | 487 | 79 | | Fixed64.FromFraction(4, 3) * Fixed64.Pi, |
| | 487 | 80 | | ScaledRadius, |
| | 487 | 81 | | ScaledRadius, |
| | 487 | 82 | | ScaledRadius); |
| | | 83 | | |
| | | 84 | | internal override ExactMassWeight CalculatePreparedMassPropertyWeight() => |
| | 456 | 85 | | ExactMassWeight.FromProduct( |
| | 456 | 86 | | Fixed64.FromFraction(4, 3) * Fixed64.Pi, |
| | 456 | 87 | | _preparedRadius, |
| | 456 | 88 | | _preparedRadius, |
| | 456 | 89 | | _preparedRadius); |
| | | 90 | | |
| | | 91 | | internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass) |
| | | 92 | | { |
| | | 93 | | // For a solid sphere, the inertia tensor is (2/5)*m*r^2 for the diagonal elements |
| | 7179 | 94 | | Fixed64 diagonalElement = Fixed64.FromFraction(2, 5) * mass * ScaledRadiusSqr; |
| | | 95 | | |
| | 7179 | 96 | | Fixed3x3 tensor = new( |
| | 7179 | 97 | | diagonalElement, Fixed64.Zero, Fixed64.Zero, |
| | 7179 | 98 | | Fixed64.Zero, diagonalElement, Fixed64.Zero, |
| | 7179 | 99 | | Fixed64.Zero, Fixed64.Zero, diagonalElement |
| | 7179 | 100 | | ); |
| | 7179 | 101 | | return tensor; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc/> |
| | | 105 | | public override Vector3d ClosestPointOnSurface(Vector3d other) |
| | | 106 | | { |
| | 575 | 107 | | FixedPointAnchor anchor = |
| | 575 | 108 | | GetClosestSurfaceAnchor(other, out _); |
| | 575 | 109 | | if (anchor.TryGetPoint(out Vector3d point)) |
| | 574 | 110 | | return point; |
| | | 111 | | |
| | 1 | 112 | | throw new InvalidOperationException( |
| | 1 | 113 | | "The closest sphere surface point is outside the representable coordinate domain."); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <inheritdoc/> |
| | | 117 | | public override Vector3d GetNormalAtPoint(Vector3d point) |
| | | 118 | | { |
| | 1642 | 119 | | if (point == Center) |
| | 3 | 120 | | return Vector3d.Zero; |
| | | 121 | | |
| | 1639 | 122 | | _ = GetClosestSurfaceAnchor( |
| | 1639 | 123 | | point, |
| | 1639 | 124 | | out Vector3d normal); |
| | 1639 | 125 | | return normal; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 129 | | Vector3d point, |
| | | 130 | | out Vector3d normal) => |
| | 2725 | 131 | | WideFiniteAxisIntersection |
| | 2725 | 132 | | .GetClosestCenteredCapsuleSurfaceAnchor( |
| | 2725 | 133 | | point, |
| | 2725 | 134 | | Center, |
| | 2725 | 135 | | FixedQuaternion.Identity, |
| | 2725 | 136 | | Vector3d.Up, |
| | 2725 | 137 | | Fixed64.Zero, |
| | 2725 | 138 | | ScaledRadius, |
| | 2725 | 139 | | Vector3d.Right, |
| | 2725 | 140 | | out normal, |
| | 2725 | 141 | | out _); |
| | | 142 | | |
| | | 143 | | /// <inheritdoc/> |
| | | 144 | | public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | 1307 | 145 | | worker.CheckSphereOverlaps(this, ref outputIntersectionPoints); |
| | | 146 | | } |