-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringPanelCollider.cs
More file actions
220 lines (190 loc) · 7.85 KB
/
Copy pathSpringPanelCollider.cs
File metadata and controls
220 lines (190 loc) · 7.85 KB
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
using UnityEngine;
namespace FUnit
{
public class SpringPanelCollider : MonoBehaviour
{
public float width = 0.25f;
public float height = 0.25f;
// If linkedRenderer is not null, the collider will be enabled
// based on whether the renderer is
public Renderer linkedRenderer;
public Vector3 GetPlaneNormal()
{
return transform.forward;
}
public SpringBone.CollisionStatus CheckForCollisionAndReact
(
Vector3 headPosition,
float length,
ref Vector3 tailPosition,
float tailRadius
)
{
if (linkedRenderer != null
&& !linkedRenderer.enabled)
{
return SpringBone.CollisionStatus.NoCollision;
}
var localHeadPosition = transform.InverseTransformPoint(headPosition);
var localLength = transform.InverseTransformDirection(length, 0f, 0f).magnitude;
var localTailPosition = transform.InverseTransformPoint(tailPosition);
var localTailRadius = transform.InverseTransformDirection(tailRadius, 0f, 0f).magnitude;
var adjustedWidth = 0.5f * width + localTailRadius;
var adjustedHeight = 0.5f * height + localTailRadius;
var tailOutOfBounds = (localTailPosition.y <= -adjustedHeight)
|| (localTailPosition.y >= adjustedHeight)
|| (localTailPosition.x <= -adjustedWidth)
|| (localTailPosition.x >= adjustedWidth);
var headOutOfBounds = (localHeadPosition.y <= -adjustedHeight)
|| (localHeadPosition.y >= adjustedHeight)
|| (localHeadPosition.x <= -adjustedWidth)
|| (localHeadPosition.x >= adjustedWidth);
if (tailOutOfBounds && headOutOfBounds)
{
return SpringBone.CollisionStatus.NoCollision;
}
var collisionStatus = CheckForCollisionWithAlignedPlaneAndReact(
localHeadPosition, localLength, ref localTailPosition, localTailRadius, Axis.Z);
if (collisionStatus != SpringBone.CollisionStatus.NoCollision)
{
#if UNITY_EDITOR
RecordCollision(tailPosition, tailRadius, collisionStatus);
#endif
tailPosition = transform.TransformPoint(localTailPosition);
}
return collisionStatus;
}
public enum Axis
{
X = 0,
Y,
Z,
AxisCount
}
public static SpringBone.CollisionStatus CheckForCollisionWithAlignedPlaneAndReact
(
Vector3 localHeadPosition,
float localLength,
ref Vector3 localTailPosition,
float localTailRadius,
Axis upAxis
)
{
var zIndex = (int)upAxis;
if (localTailPosition[zIndex] >= localTailRadius)
{
return SpringBone.CollisionStatus.NoCollision;
}
var collisionStatus = SpringBone.CollisionStatus.TailCollision;
var newLocalTailPosition = localHeadPosition;
if (localHeadPosition[zIndex] + localLength <= localTailRadius)
{
// Bone is completely embedded
newLocalTailPosition[zIndex] += localLength;
collisionStatus = SpringBone.CollisionStatus.HeadIsEmbedded;
}
else
{
var xIndex = (zIndex + 1) % (int)Axis.AxisCount;
var yIndex = (zIndex + 2) % (int)Axis.AxisCount;
var heightAboveRadius = localHeadPosition[zIndex] - localTailRadius;
var projectionLength = Mathf.Sqrt(localLength * localLength - heightAboveRadius * heightAboveRadius);
var localBoneVector = localTailPosition - localHeadPosition;
var projectionVector = new Vector2(localBoneVector[xIndex], localBoneVector[yIndex]);
var projectionVectorLength = projectionVector.magnitude;
if (projectionVectorLength > 0.001f)
{
var projection = (projectionLength / projectionVectorLength) * projectionVector;
newLocalTailPosition[xIndex] += projection.x;
newLocalTailPosition[yIndex] += projection.y;
newLocalTailPosition[zIndex] = localTailRadius;
}
}
localTailPosition = newLocalTailPosition;
return collisionStatus;
}
#if UNITY_EDITOR
public void DrawGizmos(Color color)
{
const int PointCount = 4;
if (localGizmoPoints == null
|| worldGizmoPoints == null
|| localGizmoPoints.Length < PointCount
|| worldGizmoPoints.Length < PointCount)
{
localGizmoPoints = new Vector3[PointCount];
worldGizmoPoints = new Vector3[PointCount];
}
var halfWidth = 0.5f * width;
var halfHeight = 0.5f * height;
localGizmoPoints[0] = new Vector3(-halfWidth, -halfHeight, 0f);
localGizmoPoints[1] = new Vector3( halfWidth, -halfHeight, 0f);
localGizmoPoints[2] = new Vector3( halfWidth, halfHeight, 0f);
localGizmoPoints[3] = new Vector3(-halfWidth, halfHeight, 0f);
for (int pointIndex = 0; pointIndex < PointCount; pointIndex++)
{
worldGizmoPoints[pointIndex] = transform.TransformPoint(localGizmoPoints[pointIndex]);
}
UnityEditor.Handles.color = color;
for (int pointIndex = 0; pointIndex < PointCount; pointIndex++)
{
var endPointIndex = (pointIndex + 1) % PointCount;
UnityEditor.Handles.DrawLine(worldGizmoPoints[pointIndex], worldGizmoPoints[endPointIndex]);
UnityEditor.Handles.DrawLine(worldGizmoPoints[pointIndex], worldGizmoPoints[pointIndex] - 0.15f * transform.forward);
}
HandlesUtil.DrawArrow(transform.position, transform.position + transform.forward * 0.1f, color, 0.1f);
if (colliderDebug != null)
{
colliderDebug.DrawGizmos();
}
}
private SpringManager manager;
private Vector3[] localGizmoPoints;
private Vector3[] worldGizmoPoints;
private SpringColliderDebug colliderDebug;
private void OnDrawGizmos()
{
if (!enabled)
{
return;
}
if (manager == null)
{
manager = GetComponentInParent<SpringManager>();
}
if (SpringManager.showColliders
&& !SpringBone.SelectionContainsSpringBones())
{
DrawGizmos((manager != null) ? manager.colliderColor : Color.gray);
}
}
private void OnDrawGizmosSelected()
{
DrawGizmos(enabled ? Color.white : Color.gray);
}
private void RecordCollision
(
Vector3 tailPosition,
float tailRadius,
SpringBone.CollisionStatus collisionStatus
)
{
var planeNormal = GetPlaneNormal();
var planeOrigin = transform.position;
var planeToCollision = tailPosition - planeOrigin;
var normalProjection = Vector3.Dot(planeToCollision, planeNormal) * planeNormal;
var projectedCollisionPoint = tailPosition - normalProjection;
colliderDebug.RecordCollision(
projectedCollisionPoint, planeNormal, tailRadius, collisionStatus);
}
private void Awake()
{
colliderDebug = new SpringColliderDebug();
}
private void Update()
{
colliderDebug.ClearCollisions();
}
#endif
}
}