11using System.Collections.Generic;
23 private Vector3? _cachedCenter;
25 private Matrix4x4? _cachedMatrix;
26 private Matrix4x4? _cachedTransformMatrix;
36 if (!checkIsCacheValid()) { rebuildCache(); }
37 return _cachedMatrix.Value;
40 private Pose? _cachedPose;
46 if (!checkIsCacheValid()) { rebuildCache(); }
47 return _cachedPose.Value;
51 private bool checkIsCacheValid() {
52 return _cachedPose.HasValue &&
53 _cachedCenter.HasValue &&
54 _cachedNormal.HasValue &&
55 center == _cachedCenter.Value &&
57 ((
transform ==
null && !_cachedTransformMatrix.HasValue) ||
58 (_cachedTransformMatrix.HasValue &&
59 _cachedTransformMatrix.Value ==
transform.localToWorldMatrix));
61 private void rebuildCache() {
62 _cachedMatrix = Matrix4x4.TRS(
64 Quaternion.LookRotation(
normal),
72 _cachedTransformMatrix =
null;
75 _cachedMatrix =
transform.localToWorldMatrix * _cachedMatrix;
76 _cachedPose =
transform.localToWorldMatrix.GetPose() * _cachedPose;
77 _cachedTransformMatrix =
transform.localToWorldMatrix;
87 _cachedTransformMatrix =
null;
90 this.transform =
null;
97 _cachedTransformMatrix =
null;
103 private static List<Collider> s_backingUnityBodyCollidersCache;
104 private static List<Collider> s_unityBodyCollidersCache {
106 if (s_backingUnityBodyCollidersCache ==
null) {
107 s_backingUnityBodyCollidersCache =
new List<Collider>();
109 return s_backingUnityBodyCollidersCache;
113 bool includeBehindPlane =
false) {
114 s_unityBodyCollidersCache.Clear();
117 s_unityBodyCollidersCache,
118 includeInactiveObjects: false
120 foreach (var collider
in s_unityBodyCollidersCache) {
132 bool includeBehindPlane =
false) {
133 var planePose = this.
pose;
136 var colliderCenter = getUnityColliderWorldCenter(unityCollider);
137 var colliderCenter_plane =
138 (planePoseInverse * colliderCenter).position;
139 var colliderCenterOnPlane_plane = colliderCenter_plane.WithZ(0f);
140 var colliderCenterOnPlane =
141 (planePose * colliderCenterOnPlane_plane).position;
143 var closestPoint = unityCollider.ClosestPoint(colliderCenterOnPlane);
144 var closestPoint_plane = (planePoseInverse * closestPoint).position;
149 var planeToClosestPoint_plane =
150 closestPoint_plane - colliderCenterOnPlane_plane;
152 if (includeBehindPlane) {
154 return planeToClosestPoint_plane.z <= 0f;
157 return planeToClosestPoint_plane.z == 0f;
161 private Vector3 getUnityColliderWorldCenter(
163 SphereCollider sphere = unityCollider as SphereCollider;
164 if (sphere !=
null) {
165 return unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
170 CapsuleCollider capsule = unityCollider as CapsuleCollider;
171 if (capsule !=
null) {
173 capsule.GetCapsulePoints(out a, out b);
174 return unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
179 BoxCollider box = unityCollider as BoxCollider;
181 return unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
186 MeshCollider mesh = unityCollider as MeshCollider;
189 return unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
194 throw new System.Exception(
"(Plane Collision) Collider type not supported: " +
195 unityCollider.GetType().ToString());
A struct very similar to Vector3, but that prevents itself from ever being converted to Vector3....
static bool PointsInSameDirection(Direction3 A, Direction3 B)
Returns whether two Direction3s point in the same direction without normalizing either of the underly...
Pose pose
The local to world pose for this plane. See Plane.matrix.
Matrix4x4 matrix
The local to world matrix for this plane. The plane is always locally an XY plane,...
bool CollidesWith(UnityEngine.Rigidbody unityBody, bool includeBehindPlane=false)
Plane(Vector3 center, Direction3 normal, Transform transform)
bool CollidesWith(UnityEngine.Collider unityCollider, bool includeBehindPlane=false)
Only guaranteed to be correct for convex colliders.
Plane(Vector3 center, Direction3 normal)
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...