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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
using System;
using System.Runtime.InteropServices;
// file: core/math/vector3.h
// commit: bd282ff43f23fe845f29a3e25c8efc01bd65ffb0
// file: core/math/vector3.cpp
// commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
// file: core/variant_call.cpp
// commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
namespace Godot
{
[StructLayout(LayoutKind.Sequential)]
public struct Vector3 : IEquatable<Vector3>
{
public enum Axis
{
X = 0,
Y,
Z
}
public float x;
public float y;
public float z;
public float this[int index]
{
get
{
switch (index)
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw new IndexOutOfRangeException();
}
}
set
{
switch (index)
{
case 0:
x = value;
return;
case 1:
y = value;
return;
case 2:
z = value;
return;
default:
throw new IndexOutOfRangeException();
}
}
}
internal void normalize()
{
float length = this.length();
if (length == 0f)
{
x = y = z = 0f;
}
else
{
x /= length;
y /= length;
z /= length;
}
}
public Vector3 abs()
{
return new Vector3(Mathf.abs(x), Mathf.abs(y), Mathf.abs(z));
}
public float angle_to(Vector3 to)
{
return Mathf.atan2(cross(to).length(), dot(to));
}
public Vector3 bounce(Vector3 n)
{
return -reflect(n);
}
public Vector3 ceil()
{
return new Vector3(Mathf.ceil(x), Mathf.ceil(y), Mathf.ceil(z));
}
public Vector3 cross(Vector3 b)
{
return new Vector3
(
(y * b.z) - (z * b.y),
(z * b.x) - (x * b.z),
(x * b.y) - (y * b.x)
);
}
public Vector3 cubic_interpolate(Vector3 b, Vector3 preA, Vector3 postB, float t)
{
Vector3 p0 = preA;
Vector3 p1 = this;
Vector3 p2 = b;
Vector3 p3 = postB;
float t2 = t * t;
float t3 = t2 * t;
return 0.5f * (
(p1 * 2.0f) + (-p0 + p2) * t +
(2.0f * p0 - 5.0f * p1 + 4f * p2 - p3) * t2 +
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3
);
}
public float distance_squared_to(Vector3 b)
{
return (b - this).length_squared();
}
public float distance_to(Vector3 b)
{
return (b - this).length();
}
public float dot(Vector3 b)
{
return x * b.x + y * b.y + z * b.z;
}
public Vector3 floor()
{
return new Vector3(Mathf.floor(x), Mathf.floor(y), Mathf.floor(z));
}
public Vector3 inverse()
{
return new Vector3(1.0f / x, 1.0f / y, 1.0f / z);
}
public bool is_normalized()
{
return Mathf.abs(length_squared() - 1.0f) < Mathf.Epsilon;
}
public float length()
{
float x2 = x * x;
float y2 = y * y;
float z2 = z * z;
return Mathf.sqrt(x2 + y2 + z2);
}
public float length_squared()
{
float x2 = x * x;
float y2 = y * y;
float z2 = z * z;
return x2 + y2 + z2;
}
public Vector3 linear_interpolate(Vector3 b, float t)
{
return new Vector3
(
x + (t * (b.x - x)),
y + (t * (b.y - y)),
z + (t * (b.z - z))
);
}
public Axis max_axis()
{
return x < y ? (y < z ? Axis.Z : Axis.Y) : (x < z ? Axis.Z : Axis.X);
}
public Axis min_axis()
{
return x < y ? (x < z ? Axis.X : Axis.Z) : (y < z ? Axis.Y : Axis.Z);
}
public Vector3 normalized()
{
Vector3 v = this;
v.normalize();
return v;
}
public Basis outer(Vector3 b)
{
return new Basis(
new Vector3(x * b.x, x * b.y, x * b.z),
new Vector3(y * b.x, y * b.y, y * b.z),
new Vector3(z * b.x, z * b.y, z * b.z)
);
}
public Vector3 reflect(Vector3 n)
{
#if DEBUG
if (!n.is_normalized())
throw new ArgumentException(String.Format("{0} is not normalized", n), nameof(n));
#endif
return 2.0f * n * dot(n) - this;
}
public Vector3 rotated(Vector3 axis, float phi)
{
return new Basis(axis, phi).xform(this);
}
public Vector3 slide(Vector3 n)
{
return this - n * dot(n);
}
public Vector3 snapped(Vector3 by)
{
return new Vector3
(
Mathf.stepify(x, by.x),
Mathf.stepify(y, by.y),
Mathf.stepify(z, by.z)
);
}
public Basis to_diagonal_matrix()
{
return new Basis(
x, 0f, 0f,
0f, y, 0f,
0f, 0f, z
);
}
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3 operator +(Vector3 left, Vector3 right)
{
left.x += right.x;
left.y += right.y;
left.z += right.z;
return left;
}
public static Vector3 operator -(Vector3 left, Vector3 right)
{
left.x -= right.x;
left.y -= right.y;
left.z -= right.z;
return left;
}
public static Vector3 operator -(Vector3 vec)
{
vec.x = -vec.x;
vec.y = -vec.y;
vec.z = -vec.z;
return vec;
}
public static Vector3 operator *(Vector3 vec, float scale)
{
vec.x *= scale;
vec.y *= scale;
vec.z *= scale;
return vec;
}
public static Vector3 operator *(float scale, Vector3 vec)
{
vec.x *= scale;
vec.y *= scale;
vec.z *= scale;
return vec;
}
public static Vector3 operator *(Vector3 left, Vector3 right)
{
left.x *= right.x;
left.y *= right.y;
left.z *= right.z;
return left;
}
public static Vector3 operator /(Vector3 vec, float scale)
{
vec.x /= scale;
vec.y /= scale;
vec.z /= scale;
return vec;
}
public static Vector3 operator /(Vector3 left, Vector3 right)
{
left.x /= right.x;
left.y /= right.y;
left.z /= right.z;
return left;
}
public static bool operator ==(Vector3 left, Vector3 right)
{
return left.Equals(right);
}
public static bool operator !=(Vector3 left, Vector3 right)
{
return !left.Equals(right);
}
public static bool operator <(Vector3 left, Vector3 right)
{
if (left.x == right.x)
{
if (left.y == right.y)
return left.z < right.z;
else
return left.y < right.y;
}
return left.x < right.x;
}
public static bool operator >(Vector3 left, Vector3 right)
{
if (left.x == right.x)
{
if (left.y == right.y)
return left.z > right.z;
else
return left.y > right.y;
}
return left.x > right.x;
}
public static bool operator <=(Vector3 left, Vector3 right)
{
if (left.x == right.x)
{
if (left.y == right.y)
return left.z <= right.z;
else
return left.y < right.y;
}
return left.x < right.x;
}
public static bool operator >=(Vector3 left, Vector3 right)
{
if (left.x == right.x)
{
if (left.y == right.y)
return left.z >= right.z;
else
return left.y > right.y;
}
return left.x > right.x;
}
public override bool Equals(object obj)
{
if (obj is Vector3)
{
return Equals((Vector3)obj);
}
return false;
}
public bool Equals(Vector3 other)
{
return x == other.x && y == other.y && z == other.z;
}
public override int GetHashCode()
{
return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode();
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", new object[]
{
this.x.ToString(),
this.y.ToString(),
this.z.ToString()
});
}
public string ToString(string format)
{
return String.Format("({0}, {1}, {2})", new object[]
{
this.x.ToString(format),
this.y.ToString(format),
this.z.ToString(format)
});
}
}
}
|