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
|
using UnityEngine;
[AddComponentMenu("Dynamic Bone/Dynamic Bone Collider")]
public class DynamicBoneCollider : DynamicBoneColliderBase
{
#if UNITY_5_3_OR_NEWER
[Tooltip("The radius of the sphere or capsule.")]
#endif
public float m_Radius = 0.5f;
#if UNITY_5_3_OR_NEWER
[Tooltip("The height of the capsule.")]
#endif
public float m_Height = 0;
void OnValidate()
{
m_Radius = Mathf.Max(m_Radius, 0);
m_Height = Mathf.Max(m_Height, 0);
}
public override bool Collide(ref Vector3 particlePosition, float particleRadius)
{
float radius = m_Radius * Mathf.Abs(transform.lossyScale.x);
float h = m_Height * 0.5f - m_Radius;
if (h <= 0)
{
if (m_Bound == Bound.Outside)
return OutsideSphere(ref particlePosition, particleRadius, transform.TransformPoint(m_Center), radius);
else
return InsideSphere(ref particlePosition, particleRadius, transform.TransformPoint(m_Center), radius);
}
else
{
Vector3 c0 = m_Center;
Vector3 c1 = m_Center;
switch (m_Direction)
{
case Direction.X:
c0.x -= h;
c1.x += h;
break;
case Direction.Y:
c0.y -= h;
c1.y += h;
break;
case Direction.Z:
c0.z -= h;
c1.z += h;
break;
}
if (m_Bound == Bound.Outside)
return OutsideCapsule(ref particlePosition, particleRadius, transform.TransformPoint(c0), transform.TransformPoint(c1), radius);
else
return InsideCapsule(ref particlePosition, particleRadius, transform.TransformPoint(c0), transform.TransformPoint(c1), radius);
}
}
static bool OutsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
{
float r = sphereRadius + particleRadius;
float r2 = r * r;
Vector3 d = particlePosition - sphereCenter;
float len2 = d.sqrMagnitude;
// if is inside sphere, project onto sphere surface
if (len2 > 0 && len2 < r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = sphereCenter + d * (r / len);
return true;
}
return false;
}
static bool InsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
{
float r = sphereRadius - particleRadius;
float r2 = r * r;
Vector3 d = particlePosition - sphereCenter;
float len2 = d.sqrMagnitude;
// if is outside sphere, project onto sphere surface
if (len2 > r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = sphereCenter + d * (r / len);
return true;
}
return false;
}
static bool OutsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
{
float r = capsuleRadius + particleRadius;
float r2 = r * r;
Vector3 dir = capsuleP1 - capsuleP0;
Vector3 d = particlePosition - capsuleP0;
float t = Vector3.Dot(d, dir);
if (t <= 0)
{
// check sphere1
float len2 = d.sqrMagnitude;
if (len2 > 0 && len2 < r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = capsuleP0 + d * (r / len);
return true;
}
}
else
{
float dl = dir.sqrMagnitude;
if (t >= dl)
{
// check sphere2
d = particlePosition - capsuleP1;
float len2 = d.sqrMagnitude;
if (len2 > 0 && len2 < r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = capsuleP1 + d * (r / len);
return true;
}
}
else if (dl > 0)
{
// check cylinder
t /= dl;
d -= dir * t;
float len2 = d.sqrMagnitude;
if (len2 > 0 && len2 < r2)
{
float len = Mathf.Sqrt(len2);
particlePosition += d * ((r - len) / len);
return true;
}
}
}
return false;
}
static bool InsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
{
float r = capsuleRadius - particleRadius;
float r2 = r * r;
Vector3 dir = capsuleP1 - capsuleP0;
Vector3 d = particlePosition - capsuleP0;
float t = Vector3.Dot(d, dir);
if (t <= 0)
{
// check sphere1
float len2 = d.sqrMagnitude;
if (len2 > r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = capsuleP0 + d * (r / len);
return true;
}
}
else
{
float dl = dir.sqrMagnitude;
if (t >= dl)
{
// check sphere2
d = particlePosition - capsuleP1;
float len2 = d.sqrMagnitude;
if (len2 > r2)
{
float len = Mathf.Sqrt(len2);
particlePosition = capsuleP1 + d * (r / len);
return true;
}
}
else if (dl > 0)
{
// check cylinder
t /= dl;
d -= dir * t;
float len2 = d.sqrMagnitude;
if (len2 > r2)
{
float len = Mathf.Sqrt(len2);
particlePosition += d * ((r - len) / len);
return true;
}
}
}
return false;
}
void OnDrawGizmosSelected()
{
if (!enabled)
return;
if (m_Bound == Bound.Outside)
Gizmos.color = Color.yellow;
else
Gizmos.color = Color.magenta;
float radius = m_Radius * Mathf.Abs(transform.lossyScale.x);
float h = m_Height * 0.5f - m_Radius;
if (h <= 0)
{
Gizmos.DrawWireSphere(transform.TransformPoint(m_Center), radius);
}
else
{
Vector3 c0 = m_Center;
Vector3 c1 = m_Center;
switch (m_Direction)
{
case Direction.X:
c0.x -= h;
c1.x += h;
break;
case Direction.Y:
c0.y -= h;
c1.y += h;
break;
case Direction.Z:
c0.z -= h;
c1.z += h;
break;
}
Gizmos.DrawWireSphere(transform.TransformPoint(c0), radius);
Gizmos.DrawWireSphere(transform.TransformPoint(c1), radius);
}
}
}
|