-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPhysicsExtensions.cs
283 lines (245 loc) · 13.3 KB
/
PhysicsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// MIT License
//
// Copyright (c) 2017 Justin Larrabee <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public static class PhysicsExtensions
{
//
// Box
//
public static bool BoxCast(BoxCollider box, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.BoxCast(center, halfExtents, direction, orientation, maxDistance, layerMask, queryTriggerInteraction);
}
public static bool BoxCast(BoxCollider box, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.BoxCast(center, halfExtents, direction, out hitInfo, orientation, maxDistance, layerMask, queryTriggerInteraction);
}
public static RaycastHit[] BoxCastAll(BoxCollider box, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.BoxCastAll(center, halfExtents, direction, orientation, maxDistance, layerMask, queryTriggerInteraction);
}
public static int BoxCastNonAlloc(BoxCollider box, Vector3 direction, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.BoxCastNonAlloc(center, halfExtents, direction, results, orientation, maxDistance, layerMask, queryTriggerInteraction);
}
public static bool CheckBox(BoxCollider box, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.CheckBox(center, halfExtents, orientation, layerMask, queryTriggerInteraction);
}
public static Collider[] OverlapBox(BoxCollider box, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.OverlapBox(center, halfExtents, orientation, layerMask, queryTriggerInteraction);
}
public static int OverlapBoxNonAlloc(BoxCollider box, Collider[] results, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center, halfExtents;
Quaternion orientation;
box.ToWorldSpaceBox(out center, out halfExtents, out orientation);
return Physics.OverlapBoxNonAlloc(center, halfExtents, results, orientation, layerMask, queryTriggerInteraction);
}
public static void ToWorldSpaceBox(this BoxCollider box, out Vector3 center, out Vector3 halfExtents, out Quaternion orientation)
{
orientation = box.transform.rotation;
center = box.transform.TransformPoint(box.center);
var lossyScale = box.transform.lossyScale;
var scale = AbsVec3(lossyScale);
halfExtents = Vector3.Scale(scale, box.size) * 0.5f;
}
//
// Sphere
//
public static bool SphereCast(SphereCollider sphere, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.SphereCast(center, radius, direction, out hitInfo, maxDistance, layerMask, queryTriggerInteraction);
}
public static RaycastHit[] SphereCastAll(SphereCollider sphere, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.SphereCastAll(center, radius, direction, maxDistance, layerMask, queryTriggerInteraction);
}
public static int SphereCastNonAlloc(SphereCollider sphere, Vector3 direction, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.SphereCastNonAlloc(center, radius, direction, results, maxDistance, layerMask, queryTriggerInteraction);
}
public static bool CheckSphere(SphereCollider sphere, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.CheckSphere(center, radius, layerMask, queryTriggerInteraction);
}
public static Collider[] OverlapSphere(SphereCollider sphere, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.OverlapSphere(center, radius, layerMask, queryTriggerInteraction);
}
public static int OverlapSphereNonAlloc(SphereCollider sphere, Collider[] results, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 center;
float radius;
sphere.ToWorldSpaceSphere(out center, out radius);
return Physics.OverlapSphereNonAlloc(center, radius, results, layerMask, queryTriggerInteraction);
}
public static void ToWorldSpaceSphere(this SphereCollider sphere, out Vector3 center, out float radius)
{
center = sphere.transform.TransformPoint(sphere.center);
radius = sphere.radius * MaxVec3(AbsVec3(sphere.transform.lossyScale));
}
//
// Capsule
//
public static bool CapsuleCast(CapsuleCollider capsule, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.CapsuleCast(point0, point1, radius, direction, out hitInfo, maxDistance, layerMask, queryTriggerInteraction);
}
public static RaycastHit[] CapsuleCastAll(CapsuleCollider capsule, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.CapsuleCastAll(point0, point1, radius, direction, maxDistance, layerMask, queryTriggerInteraction);
}
public static int CapsuleCastNonAlloc(CapsuleCollider capsule, Vector3 direction, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.CapsuleCastNonAlloc(point0, point1, radius, direction, results, maxDistance, layerMask, queryTriggerInteraction);
}
public static bool CheckCapsule(CapsuleCollider capsule, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.CheckCapsule(point0, point1, radius, layerMask, queryTriggerInteraction);
}
public static Collider[] OverlapCapsule(CapsuleCollider capsule, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.OverlapCapsule(point0, point1, radius, layerMask, queryTriggerInteraction);
}
public static int OverlapCapsuleNonAlloc(CapsuleCollider capsule, Collider[] results, int layerMask = Physics.DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
{
Vector3 point0, point1;
float radius;
capsule.ToWorldSpaceCapsule(out point0, out point1, out radius);
return Physics.OverlapCapsuleNonAlloc(point0, point1, radius, results, layerMask, queryTriggerInteraction);
}
public static void ToWorldSpaceCapsule(this CapsuleCollider capsule, out Vector3 point0, out Vector3 point1, out float radius)
{
var center = capsule.transform.TransformPoint(capsule.center);
radius = 0f;
float height = 0f;
Vector3 lossyScale = AbsVec3(capsule.transform.lossyScale);
Vector3 dir = Vector3.zero;
switch (capsule.direction) {
case 0: // x
radius = Mathf.Max(lossyScale.y, lossyScale.z) * capsule.radius;
height = lossyScale.x * capsule.height;
dir = capsule.transform.TransformDirection(Vector3.right);
break;
case 1: // y
radius = Mathf.Max(lossyScale.x, lossyScale.z) * capsule.radius;
height = lossyScale.y * capsule.height;
dir = capsule.transform.TransformDirection(Vector3.up);
break;
case 2: // z
radius = Mathf.Max(lossyScale.x, lossyScale.y) * capsule.radius;
height = lossyScale.z * capsule.height;
dir = capsule.transform.TransformDirection(Vector3.forward);
break;
}
if (height < radius*2f) {
dir = Vector3.zero;
}
point0 = center + dir * (height * 0.5f - radius);
point1 = center - dir * (height * 0.5f - radius);
}
//
// Util
//
public static void SortClosestToFurthest(RaycastHit[] hits, int hitCount = -1)
{
if (hitCount == 0) {
return;
}
if (hitCount < 0) {
hitCount = hits.Length;
}
Array.Sort<RaycastHit>(hits, 0, hitCount, ascendDistance);
}
//
// Private
//
private class AscendingDistanceComparer : IComparer<RaycastHit>
{
public int Compare(RaycastHit h1, RaycastHit h2)
{
return h1.distance < h2.distance ? -1 : (h1.distance > h2.distance ? 1 : 0);
}
}
private static AscendingDistanceComparer ascendDistance = new AscendingDistanceComparer();
private static Vector3 AbsVec3(Vector3 v)
{
return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
}
private static float MaxVec3(Vector3 v)
{
return Mathf.Max(v.x, Mathf.Max(v.y, v.z));
}
}