-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCapture-Lib.nut
7690 lines (6649 loc) · 251 KB
/
PCapture-Lib.nut
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*+--------------------------------------------------------------------------------+
| PCapture Vscripts Library |
+---------------------------------------------------------------------------------+
| Author: |
| One-of-a-Kind - laVashik |
+---------------------------------------------------------------------------------+
| pcapture-lib.nut |
| The main file in the library. initializes required parts of the library |
| GitHud repo: https://github.com/IaVashik/PCapture-LIB |
+----------------------------------------------------------------------------------+ */
local version = "PCapture-Lib 3.3 Stable"
// `Self` must be in any case, even if the script is run directly by the interpreter
if (!("self" in this)) {
self <- Entities.First()
} else {
getroottable()["self"] <- self
}
if("_lib_version_" in getroottable() && version.find("Debug") == null) {
printl("\n")
dev.warning("PCapture-Lib already initialized.")
if(_lib_version_ != version) {
dev.error("Attempting to initialize different versions of the PCapture-Lib library!")
dev.fprint("Version \"{}\" != \"{}\"", _lib_version_, version)
}
return
}
/*+--------------------------------------------------------------------------------+
| PCapture Vscripts Library |
+----------------------------------------------------------------------------------+
| Author: |
| Visionary Mathematician - laVashik :O |
+----------------------------------------------------------------------------------+
| The core mathematical module, providing essential functions and objects for |
| performing calculations, transformations, and interpolations in VScripts. |
+----------------------------------------------------------------------------------+ */
::math <- {}
/*
* Finds the minimum value among the given arguments.
*
* @param {...number} vargv - The numbers to compare.
* @returns {number} - The minimum value.
*/
math["min"] <- function(...) {
local min = vargv[0]
for(local i = 0; i< vargc; i++) {
if(min > vargv[i])
min = vargv[i]
}
return min
}
/*
* Finds the maximum value among the given arguments.
*
* @param {...number} vargv - The numbers to compare.
* @returns {number} - The maximum value.
*/
math["max"] <- function(...) {
local max = vargv[0]
for(local i = 0; i< vargc; i++) {
if(vargv[i] > max)
max = vargv[i]
}
return max
}
/*
* Clamps a number within the specified range.
*
* @param {number} number - The number to clamp.
* @param {number} min - The minimum value.
* @param {number} max - The maximum value (optional).
* @returns {number} - The clamped value.
*/
math["clamp"] <- function(number, min, max = 99999) {
if ( number < min ) return min;
if ( number > max ) return max;
return number
}
/*
* Rounds a number to the specified precision.
*
* @param {number} value - The number to round.
* @param {int} precision - The precision (e.g., 1000 for rounding to three decimal places).
* @returns {number} - The rounded value.
*/
math["round"] <- function(value, precision = 1) {
return floor(value * precision + 0.5) / precision
}
/*
* Returns the sign of a number.
*
* @param {number} x - The number.
* @returns {number} - The sign of the number (-1, 0, or 1).
*/
math["Sign"] <- function(x) {
if (x > 0) {
return 1;
} else if (x < 0) {
return -1;
} else {
return 0;
}
}
/*
* Copies the sign of one value to another.
*
* @param {number} value - The value to copy the sign to.
* @param {number} sign - The sign to copy.
* @returns {number} - The value with the copied sign.
*/
math["copysign"] <- function(value, sign) {
if (sign < 0 || value < 0) {
return -value;
}
return value
}
/*
* Remaps a value from the range [A, B] to the range [C, D].
*
* If A is equal to B, the value will be clamped to C or D depending on its relationship to B.
*
* @param {number} value - The value to remap.
* @param {number} A - The start of the input range.
* @param {number} B - The end of the input range.
* @param {number} C - The start of the output range.
* @param {number} D - The end of the output range.
* @returns {number} - The remapped value.
*/
math["RemapVal"] <- function( value, A, B, C, D )
{
if ( A == B )
{
if ( value >= B )
return D;
return C;
};
return C + (D - C) * (value - A) / (B - A);
}
math["vector"] <- {}
local mVector = math["vector"]
/*
* Checks if two vectors are equal based on their rounded components.
*
* @param {Vector} vector - The first vector.
* @param {Vector} other - The second vector.
* @returns {boolean} - True if the vectors are exactly equal, false otherwise.
*/
mVector["isEqually"] <- function(vector, other) {
return ::abs(vector.x - other.x) == 0 &&
::abs(vector.y - other.y) == 0 &&
::abs(vector.z - other.z) == 0
}
/*
* Checks if two vectors are approximately equal, within a certain precision.
* This function rounds the components of both vectors before comparing them.
*
* @param {Vector} vector - The first vector.
* @param {Vector} other - The second vector.
* @param {int} precision - The precision factor (e.g., 1000 for rounding to three decimal places).
* @returns {boolean} - True if the vectors are approximately equal, false otherwise.
*/
mVector["isEqually2"] <- function(vector, other, precision = 1000) {
vector = round(vector, precision)
other = round(other, precision)
return vector.x == other.x &&
vector.y == other.y &&
vector.z == other.z
}
/*
* Compares two vectors based on their lengths.
*
* @param {Vector} vector - The first vector.
* @param {Vector} other - The second vector.
* @returns {int} - 1 if the first vector is longer, -1 if the second vector is longer, and 0 if they have equal lengths.
*/mVector["cmp"] <- function(vector, other) {
local l1 = vector.Length()
local l2 = other.Length()
if(l1 > l2) return 1
if(l1 < l2) return -1
return 0
}
/*
* Performs element-wise multiplication of two vectors.
*
* @param {Vector} vector - The first vector.
* @param {Vector} other - The second vector.
* @returns {Vector} - A new vector with the result of the element-wise multiplication.
*/
mVector["mul"] <- function(vector, other) {
return Vector(vector.x * other.x, vector.y * other.y, vector.z * other.z)
}
/*
* Rotates a vector by a given angle.
*
* @param {Vector} vector - The vector to rotate.
* @param {Vector} angle - The Euler angles in degrees (pitch, yaw, roll) representing the rotation.
* @returns {Vector} - The rotated vector.
*/
mVector["rotate"] <- function(vector, angle) {
return math.Quaternion.fromEuler(angle).rotateVector(vector)
}
/*
* Un-rotates a vector by a given angle.
*
* @param {Vector} vector - The vector to un-rotate.
* @param {Vector} angle - The Euler angles in degrees (pitch, yaw, roll) representing the rotation to reverse.
* @returns {Vector} - The un-rotated vector.
*/
mVector["unrotate"] <- function(vector, angle) {
return math.Quaternion.fromEuler(angle).unrotateVector(vector)
}
/*
* Generates a random vector within the specified range.
*
* If `min` and `max` are both vectors, each component of the resulting vector will be a random value between the corresponding components of `min` and `max`.
* If `min` and `max` are numbers, all components of the resulting vector will be random values between `min` and `max`.
*
* @param {Vector|number} min - The minimum values for each component or a single minimum value for all components.
* @param {Vector|number} max - The maximum values for each component or a single maximum value for all components.
* @returns {Vector} - The generated random vector.
*/
mVector["random"] <- function(min, max) {
if(typeof min == "Vector" && typeof max == "Vector")
return Vector(RandomFloat(min.x, max.x), RandomFloat(min.y, max.y), RandomFloat(min.z, max.z))
return Vector(RandomFloat(min, max), RandomFloat(min, max), RandomFloat(min, max))
}
/*
* Reflects a direction vector off a surface with a given normal.
*
* @param {Vector} dir - The direction vector to reflect.
* @param {Vector} normal - The normal vector of the surface.
* @returns {Vector} - The reflected direction vector.
*/
mVector["reflect"] <- function(dir, normal) {
return dir - normal * (dir.Dot(normal) * 2)
}
/*
* Clamps the components of a vector within the specified range.
*
* @param {Vector} vector - The vector to clamp.
* @param {number} min - The minimum value for each component.
* @param {number} max - The maximum value for each component.
* @returns {Vector} - The clamped vector.
*/
mVector["clamp"] <- function(vector, min = 0, max = 255) {
return Vector(math.clamp(vector.x, min, max), math.clamp(vector.y, min, max), math.clamp(vector.z, min, max))
}
/*
* Resizes a vector to a new length while maintaining its direction.
*
* @param {Vector} vector - The vector to resize.
* @param {number} newLength - The desired new length of the vector.
* @returns {Vector} - The resized vector with the specified length.
*/
mVector["resize"] <- function(vector, newLength) {
local currentLength = vector.Length()
return vector * (newLength / currentLength)
}
/*
* Rounds the elements of a vector to the specified precision.
*
* @param {Vector} vec - The vector to round.
* @param {int} precision - The precision (e.g., 1000 for rounding to three decimal places).
* @returns {Vector} - The rounded vector.
*/
mVector["round"] <- function(vec, precision = 1000) {
vec.x = floor(vec.x * precision + 0.5) / precision
vec.y = floor(vec.y * precision + 0.5) / precision
vec.z = floor(vec.z * precision + 0.5) / precision
return vec
}
/*
* Returns a vector with the signs (-1, 0, or 1) of each component of the input vector.
*
* @param {Vector} vec - The input vector.
* @returns {Vector} - A new vector with the signs of the input vector's components.
*/
mVector["sign"] <- function(vec) {
return Vector(math.Sign(vec.x), math.Sign(vec.y), math.Sign(vec.z))
}
/*
* Calculates the absolute value of each component in a vector.
*
* @param {Vector} vector - The vector to calculate the absolute values for.
* @returns {Vector} - A new vector with the absolute values of the original vector's components.
*/
mVector["abs"] <- function(vector) {
return Vector(::abs(vector.x), ::abs(vector.y), ::abs(vector.z))
}
math["lerp"] <- {}
local lerp = math["lerp"]
/*
* Performs linear interpolation (lerp) between start and end based on the parameter t.
*
* @param {number} start - The starting value.
* @param {number} end - The ending value.
* @param {number} t - The interpolation parameter.
* @returns {number} - The interpolated value.
*/
lerp["number"] <- function(start, end, t) {
return start * (1 - t) + end * t;
}
/*
* Performs linear interpolation (lerp) between two vectors.
*
* @param {Vector} start - The starting vector.
* @param {Vector} end - The ending vector.
* @param {number} t - The interpolation parameter.
* @returns {Vector} - The interpolated vector.
*/
lerp["vector"] <- function(start, end, t) {
return Vector(this.number(start.x, end.x, t), this.number(start.y, end.y, t), this.number(start.z, end.z, t));
}
/*
* Performs linear interpolation (lerp) between two colors.
*
* @param {Vector|string} start - The starting color vector or string representation (e.g., "255 0 0").
* @param {Vector|string} end - The ending color vector or string representation.
* @param {number} t - The interpolation parameter.
* @returns {string} - The interpolated color string representation (e.g., "128 64 0").
*/
lerp["color"] <- function(start, end, t) {
if (type(start) == "string") {
start = macros.StrToVec(start)
}
if (type(end) == "string") {
end = macros.StrToVec(end)
}
return floor(this.number(start.x, end.x, t)) + " " + floor(this.number(start.y, end.y, t)) + " " + floor(this.number(start.z, end.z, t))
}
// SLERP for vector
lerp["sVector"] <- function(start, end, t) {
local q1 = math.Quaternion.fromEuler(start)
local q2 = math.Quaternion.fromEuler(end)
return q1.slerp(q2, t).toVector()
}
/*
* Performs smooth interpolation between two values using a smoothstep function.
*
* @param {number} edge0 - The lower edge of the interpolation range.
* @param {number} edge1 - The upper edge of the interpolation range.
* @param {number} value - The interpolation parameter.
* @returns {number} - The interpolated value.
*/
lerp["SmoothStep"] <- function(edge0, edge1, value) {
local t = math.clamp((value - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t)
}
/*
* Performs linear interpolation between two values.
*
* @param {number} f1 - The start value.
* @param {number} f2 - The end value.
* @param {number} i1 - The start interpolation parameter.
* @param {number} i2 - The end interpolation parameter.
* @param {number} value - The interpolation parameter.
* @returns {number} - The interpolated value.
*/
lerp["FLerp"] <- function( f1, f2, i1, i2, value ) {
return f1 + (f2 - f1) * (value - i1) / (i2 - i1);
}
// More info here: https://gizma.com/easing/
math["ease"] <- {}
local ease = math["ease"]
ease["InSine"] <- function(t) {
return 1 - cos((t * PI) / 2);
}
ease["OutSine"] <- function(t) {
return sin((t * PI) / 2);
}
ease["InOutSine"] <- function(t) {
return -(cos(PI * t) - 1) / 2;
}
ease["InQuad"] <- function(t) {
return t * t;
}
ease["OutQuad"] <- function(t) {
return 1 - (1 - t) * (1 - t);
}
ease["InOutQuad"] <- function(t) {
return t < 0.5 ? 2 * t * t : 1 - pow(-2 * t + 2, 2) / 2;
}
ease["InCubic"] <- function(t) {
return t * t * t;
}
ease["OutCubic"] <- function(t) {
return 1 - pow(1 - t, 3);
}
ease["InOutCubic"] <- function(t) {
return t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2;
}
ease["InQuart"] <- function(t) {
return t * t * t * t;
}
ease["OutQuart"] <- function(t) {
return 1 - pow(1 - t, 4);
}
ease["InOutQuart"] <- function(t) {
return t < 0.5 ? 8 * t * t * t * t : 1 - pow(-2 * t + 2, 4) / 2;
}
ease["InQuint"] <- function(t) {
return t * t * t * t * t;
}
ease["OutQuint"] <- function(t) {
return 1 - pow(1 - t, 5);
}
ease["InOutQuint"] <- function(t) {
return t < 0.5 ? 16 * t * t * t * t * t : 1 - pow(-2 * t + 2, 5) / 2;
}
ease["InExpo"] <- function(t) {
return t == 0 ? 0 : pow(2, 10 * t - 10);
}
ease["OutExpo"] <- function(t) {
return t == 1 ? 1 : 1 - pow(2, -10 * t);
}
ease["InOutExpo"] <- function(t) {
return t == 0 ? 0 : t == 1 ? 1 : t < 0.5 ? pow(2, 20 * t - 10) / 2 : (2 - pow(2, -20 * t + 10)) / 2;
}
ease["InCirc"] <- function(t) {
return 1 - sqrt(1 - pow(t, 2));
}
ease["OutCirc"] <- function(t) {
return sqrt(1 - pow(t - 1, 2));
}
ease["InOutCirc"] <- function(t) {
return t < 0.5 ? (1 - sqrt(1 - pow(2 * t, 2))) / 2 : (sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2;
}
ease["InBack"] <- function(t) {
local c1 = 1.70158;
local c3 = c1 + 1;
return c3 * t * t * t - c1 * t * t;
}
ease["OutBack"] <- function(t) {
local c1 = 1.70158;
local c3 = c1 + 1;
return 1 + c3 * pow(t - 1, 3) + c1 * pow(t - 1, 2);
}
ease["InOutBack"] <- function(t) {
local c1 = 1.70158;
local c2 = c1 * 1.525;
return t < 0.5
? (pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2
: (pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
}
ease["InElastic"] <- function(t) {
local c4 = (2 * PI) / 3;
return t == 0
? 0
: t == 1
? 1
: -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * c4);
}
ease["OutElastic"] <- function(t) {
local c4 = (2 * PI) / 3;
return t == 0
? 0
: t == 1
? 1
: pow(2, -10 * t) * sin((t * 10 - 0.75) * c4) + 1;
}
ease["InOutElastic"] <- function(t) {
local c5 = (2 * PI) / 4.5;
return t == 0
? 0
: t == 1
? 1
: t < 0.5
? -(pow(2, 20 * t - 10) * sin((20 * t - 11.125) * c5)) / 2
: (pow(2, -20 * t + 10) * sin((20 * t - 11.125) * c5)) / 2 + 1;
}
ease["InBounce"] <- function(t) {
return 1 - math.ease.OutBounce(1 - t); // todo
}
ease["OutBounce"] <- function(t) {
local n1 = 7.5625;
local d1 = 2.75;
if (t < 1 / d1) {
return n1 * t * t;
} else if (t < 2 / d1) {
return n1 * (t -= 1.5 / d1) * t + 0.75;
} else if (t < 2.5 / d1) {
return n1 * (t -= 2.25 / d1) * t + 0.9375;
} else {
return n1 * (t -= 2.625 / d1) * t + 0.984375;
}
}
ease["InOutBounce"] <- function(t) {
return t < 0.5
? (1 - math.ease.OutBounce(1 - 2 * t)) / 2
: (1 + math.ease.OutBounce(2 * t - 1)) / 2;
}
math["Quaternion"] <- class {
x = null;
y = null;
z = null;
w = null;
/*
* Creates a new quaternion.
*
* @param {number} x - The x component.
* @param {number} y - The y component.
* @param {number} z - The z component.
* @param {number} w - The w component.
*/
constructor(x,y,z,w) {
this.x = x
this.y = y
this.z = z
this.w = w
}
/*
* Creates a new quaternion from Euler angles.
*
* @param {Vector} angles - The Euler angles in degrees (pitch, yaw, roll).
* @returns {Quaternion} - The new quaternion.
*/
function fromEuler(angles) {
// Convert angles to radians
local pitch = angles.z * 0.5 / 180 * PI
local yaw = angles.y * 0.5 / 180 * PI
local roll = angles.x * 0.5 / 180 * PI
// Calculate sine and cosine values
local sRoll = sin(roll);
local cRoll = cos(roll);
local sPitch = sin(pitch);
local cPitch = cos(pitch);
local sYaw = sin(yaw);
local cYaw = cos(yaw);
// Calculate quaternion components
return math.Quaternion(
cYaw * cRoll * sPitch - sYaw * sRoll * cPitch,
cYaw * sRoll * cPitch + sYaw * cRoll * sPitch,
sYaw * cRoll * cPitch - cYaw * sRoll * sPitch,
cYaw * cRoll * cPitch + sYaw * sRoll * sPitch
)
}
/*
* Creates a new quaternion from a vector.
*
* @param {Vector} vector - The vector.
* @returns {Quaternion} - The new quaternion with the vector's components as x, y, z, and w set to 0.
*/
function fromVector(vector) {
return math.Quaternion(vector.x, vector.y, vector.z, 0)
}
/*
* Rotates a vector by the quaternion.
*
* @param {Vector} vector - The vector to rotate.
* @returns {Vector} - The rotated vector.
*/
function rotateVector(vector) {
// Convert vector to quaternion
local vecQuaternion = this.fromVector(vector)
// Find the inverse quaternion
local inverse = math.Quaternion(
-this.x,
-this.y,
-this.z,
this.w
)
// Apply quaternion rotations to the vector
local rotatedQuaternion = this * vecQuaternion * inverse;
// Return the result as a rotated vector
return Vector(rotatedQuaternion.x, rotatedQuaternion.y, rotatedQuaternion.z);
}
/*
* Un-rotates a vector by the quaternion.
*
* @param {Vector} vector - The vector to un-rotate.
* @returns {Vector} - The un-rotated vector.
*/
function unrotateVector(vector) {
local vecQuaternion = this.fromVector(vector)
// Find the conjugate quaternion
local conjugateQuaternion = math.Quaternion(
-this.x,
-this.y,
-this.z,
this.w
);
// Apply quaternion rotations to the vector with inverse rotation angles
local unrotatedQuaternion = conjugateQuaternion * vecQuaternion * this;
// Return the result as an un-rotated vector
return Vector(unrotatedQuaternion.x, unrotatedQuaternion.y, unrotatedQuaternion.z);
}
/*
* Performs spherical linear interpolation (slerp) between two quaternions.
*
* @param {Quaternion} targetQuaternion - The target quaternion to interpolate towards.
* @param {Number} t - The interpolation parameter between 0 and 1.
* @returns {Quaternion} - The interpolated quaternion.
*/
function slerp(targetQuaternion, t) {
// Normalize quaternions
local quaternion1 = this.normalize()
local quaternion2 = targetQuaternion.normalize()
// Calculate angle between quaternions
local cosTheta = quaternion1.x * quaternion2.x + quaternion1.y * quaternion2.y + quaternion1.z * quaternion2.z + quaternion1.w * quaternion2.w;
// If angle is negative, invert the second quaternion
if (cosTheta < 0) {
quaternion2.x *= -1;
quaternion2.y *= -1;
quaternion2.z *= -1;
quaternion2.w *= -1;
cosTheta *= -1;
}
// Calculate interpolation values
local theta = acos(cosTheta);
local sinTheta = sin(theta);
local weight1 = sin((1 - t) * theta) / sinTheta;
local weight2 = sin(t * theta) / sinTheta;
// Interpolate quaternions
local resultQuaternion = math.Quaternion(
weight1 * quaternion1.x + weight2 * quaternion2.x,
weight1 * quaternion1.y + weight2 * quaternion2.y,
weight1 * quaternion1.z + weight2 * quaternion2.z,
weight1 * quaternion1.w + weight2 * quaternion2.w
);
return resultQuaternion.normalize()
}
/*
* Normalizes the quaternion.
*
* @returns {Quaternion} - The normalized quaternion.
*/
function normalize() {
local magnitude = this.length()
return math.Quaternion(
this.x / magnitude,
this.y / magnitude,
this.z / magnitude,
this.w / magnitude
)
}
/*
* Calculates the dot product of two quaternions.
*
* @param {Quaternion} other - The other quaternion.
* @returns {number} - The dot product.
*/
function dot(other) {
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
}
/*
* Calculates the length (magnitude) of the quaternion.
*
* @returns {number} - The length of the quaternion.
*/
function length() {
return sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}
/*
* Calculates the inverse of the quaternion.
*
* @returns {Quaternion} - The inverse quaternion.
*/
function inverse() {
local lengthSquared = this.length() * this.length();
return math.Quaternion(this.x / lengthSquared, -this.y / lengthSquared, -this.z / lengthSquared, -this.w / lengthSquared);
}
/*
* Creates a quaternion from an axis and an angle.
*
* @param {Vector} axis - The axis of rotation (normalized vector).
* @param {number} angle - The angle of rotation in radians.
* @returns {Quaternion} - The quaternion.
*/
function fromAxisAngle(axis, angle) {
local halfAngle = angle * 0.5;
local sinHalfAngle = sin(halfAngle);
return math.Quaternion(axis.x * sinHalfAngle, axis.y * sinHalfAngle, axis.z * sinHalfAngle, cos(halfAngle));
}
/*
* Converts the quaternion to an axis and an angle.
*
* @returns {table} - A table with keys "axis" (Vector) and "angle" (number).
*/
function toAxisAngle() {
local scale = sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
if (scale < 0.001) {
return { axis = Vector(1, 0, 0), angle = 0 };
}
return {
axis = Vector(this.x / scale, this.y / scale, this.z / scale),
angle = 2 * acos(this.w)
};
}
/*
* Converts the quaternion to a vector representing Euler angles.
*
* @returns {Vector} - The vector representing Euler angles in degrees.
*/
function toVector() {
local sinr_cosp = 2 * (this.w * this.x + this.y * this.z);
local cosr_cosp = 1 - 2 * (this.x * this.x + this.y * this.y);
local roll = atan2(sinr_cosp, cosr_cosp);
local sinp = 2 * (this.w * this.y - this.z * this.x);
local pitch;
if (abs(sinp) >= 1) {
pitch = math.copysign(PI / 2, sinp); // PI/2 or -PI/2
} else {
pitch = asin(sinp);
}
local siny_cosp = 2 * (this.w * this.z + this.x * this.y);
local cosy_cosp = 1 - 2 * (this.y * this.y + this.z * this.z);
local yaw = atan2(siny_cosp, cosy_cosp);
// Convert angles to degrees
local x = pitch * 180 / PI;
local y = yaw * 180 / PI;
local z = roll * 180 / PI;
return Vector( x, y, z )
}
/*
* Checks if two quaternions are equal based on their components and length.
*
* @param {Quaternion} other - The other quaternion to compare.
* @returns {boolean} - True if the quaternions are equal, false otherwise.
*/
function isEqually(other) {
return this.cmp(other) == 0
}
/*
* Compares two quaternions based on their magnitudes.
*
* @param {Quaternion} other - The other quaternion to compare.
* @returns {number} - 1 if this quaternion's magnitude is greater, -1 if less, 0 if equal.
*/
function cmp(other) {
if (typeof other != "Quaternion") {
throw "Cannot compare quaternion with non-quaternion type";
}
local thisMagnitude = math.round(this.length(), 10000);
local otherMagnitude = math.round(other.length(), 10000);
if (thisMagnitude > otherMagnitude) {
return 1;
} else if (thisMagnitude < otherMagnitude) {
return -1;
} else {
return 0;
}
}
function _cmp(other) return this.cmp(other)
function _add(other) {
return math.Quaternion(
this.x + other.x,
this.y + other.y,
this.z + other.z,
this.w + other.w
)
}
function _sub(other) {
return math.Quaternion(
this.x - other.x,
this.y - other.y,
this.z - other.z,
this.w - other.w
)
}
/*
* Multiplies two quaternions.
*
* @param {Quaternion} other - The other quaternion.
* @returns {Quaternion} - The multiplication result.
*/
function _mul(other) {
if(typeof other == "Quaternion") {
return math.Quaternion(
this.w * other.x + this.x * other.w + this.y * other.z - this.z * other.y,
this.w * other.y - this.x * other.z + this.y * other.w + this.z * other.x,
this.w * other.z + this.x * other.y - this.y * other.x + this.z * other.w,
this.w * other.w - this.x * other.x - this.y * other.y - this.z * other.z
)
}
return math.Quaternion(
this.x * other,
this.y * other,
this.z * other,
this.w * other
)
}
function _tostring() {
return "Quaternion: (" + x + ", " + y + ", " + z + ", " + w + ")"
}
function _typeof() {
return "Quaternion"
}
}
math["Matrix"] <- class {
a = 1; b = 0; c = 0;
d = 0; e = 1; f = 0;
g = 0; h = 0; k = 1;
/*
* Creates a new matrix.
*
* @param {number} a - The value at row 1, column 1.
* @param {number} b - The value at row 1, column 2.
* @param {number} c - The value at row 1, column 3.
* @param {number} d - The value at row 2, column 1.
* @param {number} e - The value at row 2, column 2.
* @param {number} f - The value at row 2, column 3.
* @param {number} g - The value at row 3, column 1.
* @param {number} h - The value at row 3, column 2.
* @param {number} k - The value at row 3, column 3.
*/
constructor(a = 1, b = 0, c = 0,
d = 0, e = 1, f = 0,
g = 0, h = 0, k = 1
) {
this.a = a; this.b = b; this.c = c;
this.d = d; this.e = e; this.f = f;
this.g = g; this.h = h; this.k = k;
}
/*
* Creates a rotation matrix from Euler angles.
*
* @param {Vector} angles - Euler angles in degrees (pitch, yaw, roll).
* @returns {Matrix} - The rotation matrix.
*/
function fromEuler(angles) {
local sinX = sin(-angles.z / 180 * PI);
local cosX = cos(-angles.z / 180 * PI);
local sinY = sin(-angles.x / 180 * PI);
local cosY = cos(-angles.x / 180 * PI);
local sinZ = sin(-angles.y / 180 * PI);
local cosZ = cos(-angles.y / 180 * PI);
return math.Matrix(
cosY * cosZ, -sinX * -sinY * cosZ + cosX * sinZ, cosX * -sinY * cosZ + sinX * sinZ,
cosY * -sinZ, -sinX * -sinY * -sinZ + cosX * cosZ, cosX * -sinY * -sinZ + sinX * cosZ,
sinY, -sinX * cosY, cosX * cosY
);
}
/*
* Rotates a point using the matrix.
*
* @param {Vector} point - The point to rotate.
* @returns {Vector} - The rotated point.
*/
function rotateVector(point) {
return Vector(
point.x * this.a + point.y * this.b + point.z * this.c,
point.x * this.d + point.y * this.e + point.z * this.f,
point.x * this.g + point.y * this.h + point.z * this.k
);
}
/*
* Unrotates a point using the matrix.
*
* @param {Vector} point - The point to unrotate.
* @returns {Vector} - The unrotated point.
*/
function unrotateVector(point) {
return Vector(
point.x * this.a + point.y * this.d + point.z * this.g,
point.x * this.b + point.y * this.e + point.z * this.h,
point.x * this.c + point.y * this.f + point.z * this.k
);
}
/*
* Transposes the matrix.
*
* @returns {Matrix} - The transposed matrix.
*/
function transpose() {
return math.Matrix(
this.a, this.d, this.g,
this.b, this.e, this.h,
this.c, this.f, this.k
);
}
/*