-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaths.cs
executable file
·323 lines (276 loc) · 9.96 KB
/
Maths.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System;
namespace UberTools
{
[System.Serializable]
// public class IntVector2
// {
// public int x;
// public int y;
// public IntVector2(int inX, int inY)
// {
// x = inX;
// y = inY;
// }
// public IntVector2(float inX, float inY)
// {
// x = (int) inX;
// y = (int) inY;
// }
// public IntVector2(double inX, double inY)
// {
// x = (int) inX;
// y = (int) inY;
// }
// public IntVector2(Vector3 vec)
// {
// x = (int) vec.x;
// y = (int) vec.z;
// }
// public Vector2 Vector2()
// {
// return new Vector2(x, y);
// }
// public IntVector2 Rotate90(int rotation)
// {
// if(rotation<0)
// {
// rotation = 360 - (rotation%360);
// }
// else
// {
// rotation = rotation%360;
// }
// int ninetySteps = rotation / 90;
// // Debug.Log(String.Format("{0} - {1}", rotation, ninetySteps));
// if(rotation != ninetySteps*90)
// {
// Debug.LogError(String.Format("Can't rotate by anything other than multiples of 90! {0} - {1}", rotation, ninetySteps));
// }
// switch(ninetySteps)
// {
// case 1: return new IntVector2(y, -x);
// case 2: return new IntVector2(-x, -y);
// case 3: return new IntVector2(-y, x);
// default: return new IntVector2(x, y);
// }
// }
// public static IntVector2 operator+(IntVector2 a, IntVector2 b)
// {
// return new IntVector2(a.x + b.x, a.y + b.y);
// }
// public override bool Equals(object o)
// {
// return o is IntVector2 && this == (IntVector2) o;
// }
// public override int GetHashCode()
// {
// return x.GetHashCode() ^ y.GetHashCode();
// }
// public static bool operator==(IntVector2 a, IntVector2 b)
// {
// return a.x==b.x && a.y==b.y;
// }
// public static bool operator!=(IntVector2 a, IntVector2 b)
// {
// return a.x!=b.x || a.y!=b.y;
// }
// override public string ToString()
// {
// return String.Format("({0}, {1})", x, y);
// }
// }
public static class Maths
{
/// Returns the bounding radius of all colliders in that hierarchy
public static float GetRadiusOfHierarchy(GameObject root)
{
var rootPos = root.transform.position;
float totalRadius = 0.0f;
var colliders = root.GetComponentsInChildren<Collider>();
foreach (var c in colliders)
{
float boundsRadius = c.bounds.extents.magnitude;
float distanceFromRoot = (c.transform.position - rootPos).magnitude;
float radiusFromRoot = distanceFromRoot + boundsRadius;
totalRadius = Mathf.Max(radiusFromRoot, boundsRadius);
}
return totalRadius;
}
public static float MaxComponent(this Vector3 vec)
{
float returnVal = vec.x;
float comparison = Mathf.Abs(vec.x);
float test = Mathf.Abs(vec.y);
if(test>comparison)
{
comparison = test;
returnVal = vec.y;
}
test = Mathf.Abs(vec.z);
if(test>comparison)
{
comparison = test;
returnVal = vec.z;
}
return returnVal;
}
/// Double precision clamp function since there isn't one in Mathf
public static double Clamp(double value, double min, double max)
{
return (value < min) ? min : (value > max) ? max : value;
}
/// Returns a random velocity between minSpeed and max_speed
static public Vector3 RandomDirection(float minLength, float maxLength)
{
var vec = RandomVector(-1, 1);
return vec.normalized * UnityEngine.Random.Range(minLength, maxLength);
}
/// Returns a random vector with components between min and max
static public Vector3 RandomVector(float min, float max)
{
return new Vector3(UnityEngine.Random.Range(min, max), UnityEngine.Random.Range(min, max), UnityEngine.Random.Range(min, max));
}
/// Lerp between the two colours
static public Color Lerp(Color c1, Color c2, float amount)
{
return new Color(Mathf.Lerp(c1.r, c2.r, amount),
Mathf.Lerp(c1.g, c2.g, amount),
Mathf.Lerp(c1.b, c2.b, amount),
Mathf.Lerp(c1.a, c2.a, amount));
}
/// Do a float range
[System.Serializable]
public class FloatRange
{
public float MinVal;
public float MaxVal;
public FloatRange(float min, float max)
{
MinVal = min;
MaxVal = max;
}
public float Random()
{
return UnityEngine.Random.Range(MinVal, MaxVal);
}
}
static public float GetAngleSignBetween(Vector3 dir1, Vector3 dir2)
{
return GetAngleSignBetween(dir1, dir2, Vector3.up);
}
static public float GetAngleSignBetween(Vector3 dir1, Vector3 dir2, Vector3 up)
{
Vector3 right = Vector3.Cross(up, dir1);
if(Vector3.Dot(right, dir2)<0.0f)
{
return -1.0f;
}
return 1.0f;
}
static public float SignedAngle(Vector3 dir1, Vector3 dir2)
{
return SignedAngle(dir1, dir2, Vector3.up);
}
static public float SignedAngle(Vector3 dir1, Vector3 dir2, Vector3 up)
{
return Vector3.Angle(dir1, dir2)*GetAngleSignBetween(dir1, dir2, up);
}
static public Vector3 Sign(Vector3 vec)
{
return new Vector3(Mathf.Sign(vec.x), Mathf.Sign(vec.y), Mathf.Sign(vec.z));
}
static public GameObject Nearest(GameObject go1, GameObject go2, Vector3 pos)
{
var dist1 = (go1.transform.position-pos).sqrMagnitude;
var dist2 = (go2.transform.position-pos).sqrMagnitude;
if(dist1>dist2)
{
return go2;
}
return go1;
}
static public T Nearest<T>(T go1, T go2, Vector3 pos) where T:MonoBehaviour
{
var dist1 = (go1.transform.position-pos).sqrMagnitude;
var dist2 = (go2.transform.position-pos).sqrMagnitude;
if(dist1>dist2)
{
return go2;
}
return go1;
}
public struct LineSegment
{
public Vector2 start;
public Vector2 end;
public LineSegment(Vector2 inStart, Vector2 inEnd)
{
start = inStart;
end = inEnd;
}
public LineSegment(float sx, float sy, float ex, float ey)
{
start = new Vector2(sx, sy);
end = new Vector2(ex, ey);
}
}
public static bool CalcLineSegmentIntersection(LineSegment seg1, LineSegment[] segs, out Vector2 result)
{
foreach(var seg in segs)
{
if(CalcLineSegmentIntersection(seg1, seg, out result))
{
return true;
}
}
result = Vector2.zero;
return false;
}
public static bool CalcLineSegmentIntersection(LineSegment seg1, LineSegment seg2, out Vector2 result)
{
result = Vector2.zero;
float denom = ((seg1.end.x - seg1.start.x) * (seg2.end.y - seg2.start.y)) - ((seg1.end.y - seg1.start.y) * (seg2.end.x - seg2.start.x));
// AB & CD are parallel
if (denom == 0)
{
return false;
}
float numer = ((seg1.start.y - seg2.start.y) * (seg2.end.x - seg2.start.x)) - ((seg1.start.x - seg2.start.x) * (seg2.end.y - seg2.start.y));
float r = numer / denom;
float numer2 = ((seg1.start.y - seg2.start.y) * (seg1.end.x - seg1.start.x)) - ((seg1.start.x - seg2.start.x) * (seg1.end.y - seg1.start.y));
float s = numer2 / denom;
if ((r < 0 || r > 1) || (s < 0 || s > 1))
{
return false;
}
// Find intersection point
result.x = seg1.start.x + (r * (seg1.end.x - seg1.start.x));
result.y = seg1.start.y + (r * (seg1.end.y - seg1.start.y));
return true;
}
static public float RoundToNearest(float val, float rounding)
{
if(val>=0)
{
return Mathf.Floor((val + rounding/2.0f)/rounding)*rounding;
}
else
{
return Mathf.Ceil((val - rounding/2.0f)/rounding)*rounding;
}
}
}
static public class Vector3Extensions
{
/// Scale a vector in-place by a single scalar value
static public void Scale(this Vector3 v, float scale)
{
v.x *= scale;
v.y *= scale;
v.x *= scale;
}
}
}