-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwGeometry.lua
More file actions
1597 lines (1382 loc) · 48.3 KB
/
wGeometry.lua
File metadata and controls
1597 lines (1382 loc) · 48.3 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
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
-- Warcraft 3 Geometry module by ScorpioT1000 / 2020
-- Thanks to DGUI by Ashujon / 2009
-- Thankes to The Mono.Xna Team / 2006
wGeometry = Imp.export("wGeometry", function()
local Functions = nil
local Vector3 = nil
local Matrix3 = nil
local Matrix4 = nil
local Camera = nil
local Box = nil
local Sphere = nil
local Ray = nil
local zTesterLocation = Location(0,0)
local zUnlockAbility = FourCC('Aave')
local radToDeg = 180.0 / math.pi
local degToRad = math.pi / 180.0
local getTerrainZ = function(x,y)
MoveLocation(zTesterLocation, x, y)
return GetLocationZ(zTesterLocation)
end
local _GetUnitZ = function(u)
return GetUnitFlyHeight(u) + BlzGetLocalUnitZ(u)
end
local _SetUnitZ = function(u, z)
SetUnitFlyHeight(u, z - BlzGetLocalUnitZ(u), 0)
end
local _GetItemZ = function(i)
return getTerrainZ(GetItemX(u), GetItemY(u))
end
local _GetDestructableZ = function(d)
return getTerrainZ(GetDestructableX(d), GetDestructableY(d))
end
-- Must be called for each non-air unit before manipulating Z coordinate
--- @param u Unit
local unlockUnitZ = function(u)
UnitAddAbility(u, zUnlockAbility)
UnitRemoveAbility(u, zUnlockAbility)
end
-- Math functions
Functions = {
-- 1D clamp
clamp = function(value, _min, _max)
if value > _max then
value = _max
end
if value < _min then
value = _min
end
return value
end,
-- 1D distance
distance = function(value1, value2)
return math.abs(value1 - value2)
end,
-- 1D linear spline interpolation between 2 points
lerp = function(value1, value2, amount)
return value1 + (value2 - value1) * amount
end,
-- 1D hermite spline interpolation between 2 points
hermite = function(value1, tangent1, value2, tangent2, amount)
local v1 = value1
local v2 = value2
local t1 = tangent1
local t2 = tangent2
local s = amount
local result = 0.
local sCubed = s * s * s
local sSquared = s * s
if (amount == 0.) then
result = value1
elseif (amount == 1.) then
result = value2
else
result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed +
(3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared +
t1 * s +
v1
end
return result
end,
-- 1D bezier spline interpolation between 3 points
bezier = function(p0, p1, p2, amount)
local amountInv = 1-amount
return amountInv*amountInv*p0
+ 2*amount*amountInv*p1
+ amount*amount*p2
end
}
-- 3D Vector ====================
Vector3 = {
-- x, y, z
-- Create a new vector from coords. Skip the coords to create a zero vector
new = function(self, x, y, z)
local o = {}
setmetatable(o,self)
o.x = x or 0.
o.y = y or 0.
o.z = z or 0.
return o
end,
-- Create a vector from another
clone = function(self, that)
local o = {}
setmetatable(o,self)
o.x = that.x
o.y = that.y
o.z = that.z
return o
end,
--- @deprecated use :clone()
copyFrom = function(self,that) return Vector3:clone(that) end,
-- Copy vector from Unit X/Y/Z
--- @param u Unit
copyFromUnit = function(self, u)
local o = {}
setmetatable(o,self)
o.x = GetUnitX(u)
o.y = GetUnitY(u)
o.z = _GetUnitZ(u)
return o
end,
-- Copy vector from Location X/Y/Z
--- @param loc Location
copyFromLocation = function(self, loc)
local o = {}
setmetatable(o,self)
o.x = GetLocationX(loc)
o.y = GetLocationY(loc)
o.z = GetLocationZ(loc)
return o
end,
-- Copy vector from Item X/Y/Z
--- @param i Item
copyFromItem = function(self, i)
local o = {}
setmetatable(o,self)
o.x = GetItemX(i)
o.y = GetItemY(i)
o.z = _GetItemZ(i)
return o
end,
-- Copy vector from Destructable X/Y/Z
--- @param d Destructable
copyFromDestructable = function(self, d)
local o = {}
setmetatable(o,self)
o.x = GetDestructableX(d)
o.y = GetDestructableY(d)
o.z = _GetDestructableZ(d)
return o
end,
-- Create a new X-oriented unit vector
newRight = function(self)
return Vector3:new(1.,0.,0.)
end,
-- Create a new Y-oriented unit vector
newForward = function(self)
return Vector3:new(0.,1.,0.)
end,
-- Create a new Z-oriented unit vector
newUp = function(self)
return Vector3:new(0.,0.,1.)
end,
length = function(self)
return math.sqrt(self.x*self.x+self.y*self.y+self.z*self.z)
end,
lengthSquared = function(self)
return self.x*self.x+self.y*self.y+self.z*self.z
end,
length2d = function(self)
return math.sqrt(self.x*self.x+self.y*self.y)
end,
normalize = function(self)
local len = math.sqrt(self.x*self.x+self.y*self.y+self.z*self.z)
return Vector3:new(self.x/len, self.y/len, self.z/len)
end,
distance = function(self, that)
return math.sqrt(
(self.x-that.x)*(self.x-that.x) +
(self.y-that.y)*(self.y-that.y) +
(self.z-that.z)*(self.z-that.z)
)
end,
distanceSquared = function(self, that)
return
(self.x-that.x)*(self.x-that.x) +
(self.y-that.y)*(self.y-that.y) +
(self.z-that.z)*(self.z-that.z)
end,
--- @return Vector3 with zeroed Z coord
to2D = function(self)
return Vector3:new(self.x, self.y)
end,
__eq = function(self, that)
return self.x == that.x and self.y == that.y and self.z == that.z
end,
__add = function(self, that)
return Vector3:new(
self.x + that.x,
self.y + that.y,
self.z + that.z
)
end,
__sub = function(self, that)
return Vector3:new(
self.x - that.x,
self.y - that.y,
self.z - that.z
)
end,
__unm = function(self, that)
return Vector3:new(
-self.x,
-self.y,
-self.z
)
end,
-- Uses cross product (!)
--- @see https://en.wikipedia.org/wiki/Cross_product
__mul = function(self, that)
return self:crossProduct(that)
end,
-- Division by a number (!)
__div = function(self, aNumber)
return Vector3:new(
self.x / aNumber,
self.y / aNumber,
self.z / aNumber
)
end,
--- @param that Vector3
--- @return number
--- @see https://en.wikipedia.org/wiki/Dot_product
dotProduct = function(self, that)
return self.x*that.x + self.y*that.y + self.z*that.z
end,
--- @param that Vector3
--- @return Vector3
--- @see https://en.wikipedia.org/wiki/Cross_product
crossProduct = function(self, that)
return Vector3:new(
self.y * that.z - self.z * that.y,
self.z * that.x - self.x * that.z,
self.x * that.y - self.y * that.x
)
end,
scale = function(self, aNumber)
return Vector3:new(
self.x * aNumber,
self.y * aNumber,
self.z * aNumber
)
end,
-- Vector3 + Distance offset
--- @param distance number in game units
--- @param direction Vector3 normalized direction
--- @return Vector3
offset = function(self, distance, direction)
return self + direction:scale(distance)
end,
-- Spheric coordinates offset
--- @param distance number in game units
--- @param yaw number (angle in radians)
--- @param pitch number (angle in radians)
--- @return Vector3 result point
--- @see https://en.wikipedia.org/wiki/Aircraft_principal_axes
yawPitchOffset = function(self, distance, yaw, pitch)
return Vector3:new(
distance * math.cos(yaw) * math.cos(pitch),
distance * math.sin(yaw) * math.cos(pitch),
distance * math.sin(pitch)
)
end,
-- Spheric coordinates yaw angle
--- @return float angle in radians
getYaw = function(self)
return Atan2(self.y, self.x)
end,
-- Spheric coordinates pitch angle
--- @return float angle in radians
getPitch = function(self)
return Atan2(self.z, self:length2d())
end,
-- Transforms the vector by 3x3 matrix transformation components
--- @param Matrix3 m
--- @return Vector3
--- @see https://en.wikipedia.org/wiki/Transformation_matrix
transform3 = function(self, m)
return Vector3:new(
self.x*m.m11+self.y*m.m21+self.z*m.m31,
self.x*m.m12+self.y*m.m22+self.z*m.m32,
self.x*m.m13+self.y*m.m23+self.z*m.m33
)
end,
-- Transforms the vector by 4x4 matrix transformation components
--- @param Matrix4 m
--- @return Vector3
--- @see https://en.wikipedia.org/wiki/Transformation_matrix
transform4 = function(self, m)
local w = self.x*m.m14+self.y*m.m24+self.z*m.m34+m.m44
return Vector3:new(
(self.x*m.m11+self.y*m.m21+self.z*m.m31+m.m41)/w,
(self.x*m.m12+self.y*m.m22+self.z*m.m32+m.m42)/w,
(self.x*m.m13+self.y*m.m23+self.z*m.m33+m.m43)/w
)
end,
-- Applies linear interpolation
--- @param that Vector3
--- @param amount current animation state, number between 0 and 1
--- @return Vector3 result
lerp = function(self, that, amount)
return Vector3:new(
Functions.lerp(self.x, that.x, amount),
Functions.lerp(self.y, that.y, amount),
Functions.lerp(self.z, that.z, amount)
)
end,
-- Applies hermite spline interpolation
--- @param that Vector3
--- @param amount current animation state, number between 0 and 1
--- @param tangent1 (optional)
--- @param tangent2 (optional)
--- @return Vector3 result
hermite = function(self, that, amount, tangent1, tangent2)
if(tangent1 == nil) then
tangent1 = 0.
end
if(tangent2 == nil) then
tangent2 = 0.
end
return Vector3:new(
Functions.hermite(self.x, tangent1, that.x, tangent2, amount),
Functions.hermite(self.y, tangent1, that.y, tangent2, amount),
Functions.hermite(self.z, tangent1, that.z, tangent2, amount)
)
end,
-- Applies bezier spline interpolation
--- @param p2 Vector3 point 2
--- @param p3 Vector3 point 3
--- @param amount current animation state, number between 0 and 1
--- @return Vector3 result
bezier = function(self, p2, p3, amount)
return Vector3:new(
Functions.bezier(self.x, p2.x, p3.x, amount),
Functions.bezier(self.y, p2.y, p3.y, amount),
Functions.bezier(self.z, p2.z, p3.z, amount)
)
end,
-- Checks if the point is in a sphere
--- @param c Vector3 sphere center
--- @param r number sphere radius
--- @return boolean
--- @deprecated use Sphere:containsVector()
isInSphere = function(self, c, r)
return self:distanceSquared(c) < (r*r)
end,
-- Checks if the point is inside axis-aligned bounding box (AABB)
--- @param vMin
--- @param vMax
--- @return boolean
--- @deprecated use Box:containsVector()
isInAABB = function(self, vMin, vMax)
return (self.x >= vMin.x and self.x <= vMax.x) and
(self.y >= vMin.y and self.y <= vMax.y) and
(self.z >= vMin.z and self.z <= vMax.z)
end,
-- Checks if the vector is a zero-vector {0,0,0}
--- @return boolean
isZero = function(self)
return self.x == 0. and self.y == 0. and self.z == 0.
end,
-- Applies coords to a location
--- @param loc Location
--- @return Vector3 self
applyToLocation = function(self, loc)
MoveLocation(loc, self.x, self.y)
return self
end,
-- Adds coords to a location
--- @param loc Location
--- @return Vector3 self
addToLocation = function(self, loc)
MoveLocation(loc, GetLocationX(loc) + self.x, GetLocationY(loc) + self.y)
return self
end,
-- Applies coords to a unit
--- @param u Unit
--- @return Vector3 self
applyToUnit = function(self, u)
SetUnitX(u, self.x)
SetUnitY(u, self.y)
_SetUnitZ(u, self.z)
return self
end,
-- Applies to unit's yaw angle as direction vector
--- @param u Unit
--- @return Vector3 self
applyToUnitFacing = function(self, u)
if(self.x ~= 0. or self.y ~= 0.) then
BlzSetUnitFacingEx(u, self:getYaw() * radToDeg)
end
return self
end,
-- Applies to unit's yaw angle as direction vector
--- @param u Unit
--- @param duration time in seconds
--- @return Vector3 self
applyToUnitFacingAnimated = function(self, u)
if(self.x ~= 0. or self.y ~= 0.) then
SetUnitFacing(u, self:getYaw() * radToDeg)
end
return self
end,
-- Adds coords to a unit
--- @param u Unit
--- @return Vector3 self
addToUnit = function(self, u)
SetUnitX(u, GetUnitX(u) + self.x)
SetUnitY(u, GetUnitY(u) + self.y)
_SetUnitZ(u, _GetUnitZ(u) + self.z)
return self
end,
__tostring = function(self)
return "{ " .. tostring(self.x) .. ", " .. tostring(self.y) .. ", " .. tostring(self.z) .. " }"
end,
}
Vector3.__index = Vector3
-- 3x3 Matrix ====================
Matrix3 = {
-- m11, m12, m13
-- m21, m22, m23
-- m31, m32, m33
-- Create a new matrix from coords. Skip coords to create a zero matrix
--- @return Matrix3
new = function(self, m11, m12, m13, m21, m22, m23, m31, m32, m33)
local o = {}
setmetatable(o,self)
o.m11 = m11 or 0.
o.m12 = m12 or 0.
o.m13 = m13 or 0.
o.m21 = m21 or 0.
o.m22 = m22 or 0.
o.m23 = m23 or 0.
o.m31 = m31 or 0.
o.m32 = m32 or 0.
o.m33 = m33 or 0.
return o
end,
-- Create a matrix from another
--- @return Matrix3
clone = function(self, that)
local o = {}
setmetatable(o,self)
o.m11 = that.m11
o.m12 = that.m12
o.m13 = that.m13
o.m21 = that.m21
o.m22 = that.m22
o.m23 = that.m23
o.m31 = that.m31
o.m32 = that.m32
o.m33 = that.m33
return o
end,
--- @deprecated use :clone()
copyFrom = function(self,that) return Matrix3:clone(that) end,
-- Create a new identity matrix
--- @return Matrix3
newIdentity = function(self)
local o = {}
setmetatable(o,self)
o.m11 = 1.
o.m12 = 0.
o.m13 = 0.
o.m21 = 0.
o.m22 = 1.
o.m23 = 0.
o.m31 = 0.
o.m32 = 0.
o.m33 = 1.
return o
end,
-- Create a new scaling matrix
--- @return Matrix3
newScaling = function(self, scaleX, scaleY, scaleZ)
local o = {}
setmetatable(o,self)
o.m11 = scaleX or 1.
o.m12 = 0.
o.m13 = 0.
o.m21 = 0.
o.m22 = scaleY or 1.
o.m23 = 0.
o.m31 = 0.
o.m32 = 0.
o.m33 = scaleZ or 1.
return o
end,
-- Create a new scaling matrix
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Scaling_(geometry)
newScaling = function(self, scaleX, scaleY, scaleZ)
return Matrix3:new(
scaleX or 1., 0., 0.,
0., scaleY or 1., 0.,
0., 0., scaleZ or 1.
)
end,
-- Create a new rotation X matrix from the angle (in radians)
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Rotation_matrix
newRotationX = function(self, a)
return Matrix3:new(
1., 0., 0.,
0., math.cos(a), -math.sin(a),
0., math.sin(a), math.cos(a)
)
end,
-- Create a new rotation Y matrix from the angle (in radians)
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Rotation_matrix
newRotationY = function(self, a)
return Matrix3:new(
math.cos(a), 0., math.sin(a),
0., 1., 0.,
-math.sin(a), 0., math.cos(a)
)
end,
-- Create a new rotation Z matrix from the angle (in radians)
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Rotation_matrix
newRotationZ = function(self, a)
return Matrix3:new(
math.cos(a), -math.sin(a), 0.,
math.sin(a), math.cos(a), 0.,
0., 0., 1.
)
end,
-- Create a new axis rotation matrix from the vector and angle (in radians)
--- @param v Vector3
--- @param a number
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation
newRotationAxis = function(self, v, a)
local cosa = math.cos(a)
local sina = math.sin(a)
return Matrix3:new(
cosa+(1.-cosa)*v.x*v.x,
(1.-cosa)*v.x*v.y-sina*v.z,
(1.-cosa)*v.x*v.z+sina*v.y,
(1.-cosa)*v.y*v.x+sina*v.z,
cosa+(1.-cosa)*v.y*v.y,
(1.-cosa)*v.y*v.z-sina*v.x,
(1.-cosa)*v.z*v.x-sina*v.y,
(1.-cosa)*v.z*v.y+sina*v.x,
cosa+(1.-cosa)*v.z*v.z
)
end,
-- Create a new rotation matrix from the yaw-pitch-roll vector
--- @param v Vector3 where x = yaw, y = pitch, z = roll
--- @return Matrix3
--- @see https://en.wikipedia.org/wiki/Aircraft_principal_axes
newRotationYawPitchRoll = function(self, v)
local cosa = math.cos(v.x)
local sina = math.sin(v.x)
local cosb = math.cos(v.y)
local sinb = math.sin(v.y)
local cosy = math.cos(v.z)
local siny = math.sin(v.z)
return Matrix3:new(
cosa*cosb,
cosa*sinb*siny-sina*cosy,
cosa*sinb*cosy+sina*siny,
sina*cosb,
sina*sinb*siny+cosa*cosy,
sina*sinb*cosy-cosa*siny,
-sinb,
cosb*siny,
cosb*cosy
)
end,
__eq = function(self, that)
return self.m11 == that.m11 and self.m12 == that.m12 and self.m13 == that.m13
and self.m21 == that.m21 and self.m22 == that.m22 and self.m23 == that.m23
and self.m31 == that.m31 and self.m32 == that.m32 and self.m33 == that.m33
end,
-- Matrix multiplication
__mul = function(self, that)
return Matrix3:new(
self.m11*that.m11+self.m21*that.m12+self.m31*that.m13,
self.m12*that.m11+self.m22*that.m12+self.m32*that.m13,
self.m13*that.m11+self.m23*that.m12+self.m33*that.m13,
self.m11*that.m21+self.m21*that.m22+self.m31*that.m23,
self.m12*that.m21+self.m22*that.m22+self.m32*that.m23,
self.m13*that.m21+self.m23*that.m22+self.m33*that.m23,
self.m11*that.m31+self.m21*that.m32+self.m31*that.m33,
self.m12*that.m31+self.m22*that.m32+self.m32*that.m33,
self.m13*that.m31+self.m23*that.m32+self.m33*that.m33
)
end,
__tostring = function(self)
return "{ \n " .. tostring(self.m11) .. ", " .. tostring(self.m12) .. ", " .. tostring(self.m13) .. "\n"
.. " " .. tostring(self.m21) .. ", " .. tostring(self.m22) .. ", " .. tostring(self.m23) .. "\n"
.. " " .. tostring(self.m31) .. ", " .. tostring(self.m32) .. ", " .. tostring(self.m33) .. "\n}"
end,
}
Matrix3.__index = Matrix3
-- 4x4 Matrix ====================
Matrix4 = {
-- m11, m12, m13, m14
-- m21, m22, m23, m24
-- m31, m32, m33, m34
-- m41, m42, m43, m44
-- Create a new matrix from coords. Skip coords to create a zero matrix
--- @return Matrix4
new = function(self,
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44
)
local o = {}
setmetatable(o,self)
o.m11 = m11 or 0.
o.m12 = m12 or 0.
o.m13 = m13 or 0.
o.m14 = m14 or 0.
o.m21 = m21 or 0.
o.m22 = m22 or 0.
o.m23 = m23 or 0.
o.m24 = m24 or 0.
o.m31 = m31 or 0.
o.m32 = m32 or 0.
o.m33 = m33 or 0.
o.m34 = m34 or 0.
o.m41 = m41 or 0.
o.m42 = m42 or 0.
o.m43 = m43 or 0.
o.m44 = m44 or 0.
return o
end,
-- Create a matrix from another 4x4 matrix
--- @param Matrix4 that
--- @return Matrix4
clone = function(self, that)
local o = {}
setmetatable(o,self)
o.m11 = that.m11
o.m12 = that.m12
o.m13 = that.m13
o.m14 = that.m14
o.m21 = that.m21
o.m22 = that.m22
o.m23 = that.m23
o.m24 = that.m24
o.m31 = that.m31
o.m32 = that.m32
o.m33 = that.m33
o.m34 = that.m34
o.m41 = that.m41
o.m42 = that.m42
o.m43 = that.m43
o.m44 = that.m44
return o
end,
--- @deprecated use :clone()
copyFrom = function(self,that) return Matrix4:clone(that) end,
-- Creates a 4x4 matrix from 3x3 matrix
--- @param Matrix3 that
--- @return Matrix4
copyFrom3 = function(self, that)
local o = {}
setmetatable(o,self)
o.m11 = that.m11
o.m12 = that.m12
o.m13 = that.m13
o.m14 = 0.
o.m21 = that.m21
o.m22 = that.m22
o.m23 = that.m23
o.m24 = 0.
o.m31 = that.m31
o.m32 = that.m32
o.m33 = that.m33
o.m34 = 0.
o.m41 = 0.
o.m42 = 0.
o.m43 = 0.
o.m44 = 1.
return o
end,
-- Create a new identity matrix
--- @return Matrix4
newIdentity = function(self)
local o = {}
setmetatable(o,self)
o.m11 = 1.
o.m12 = 0.
o.m13 = 0.
o.m14 = 0.
o.m21 = 0.
o.m22 = 1.
o.m23 = 0.
o.m24 = 0.
o.m31 = 0.
o.m32 = 0.
o.m33 = 1.
o.m34 = 0.
o.m41 = 0.
o.m42 = 0.
o.m43 = 0.
o.m44 = 1.
return o
end,
__eq = function(self, that)
return self.m11 == that.m11 and self.m12 == that.m12 and self.m13 == that.m13 and self.m14 == that.m14
and self.m21 == that.m21 and self.m22 == that.m22 and self.m23 == that.m23 and self.m24 == that.m24
and self.m31 == that.m31 and self.m32 == that.m32 and self.m33 == that.m33 and self.m34 == that.m34
and self.m41 == that.m41 and self.m42 == that.m42 and self.m43 == that.m43 and self.m44 == that.m44
end,
-- Matrix multiplication
__mul = function(self, that)
return Matrix4:new(
self.m11*that.m11+self.m21*that.m12+self.m31*that.m13+self.m41*that.m14,
self.m12*that.m11+self.m22*that.m12+self.m32*that.m13+self.m42*that.m14,
self.m13*that.m11+self.m23*that.m12+self.m33*that.m13+self.m43*that.m14,
self.m14*that.m11+self.m24*that.m12+self.m34*that.m13+self.m44*that.m14,
self.m11*that.m21+self.m21*that.m22+self.m31*that.m23+self.m41*that.m24,
self.m12*that.m21+self.m22*that.m22+self.m32*that.m23+self.m42*that.m24,
self.m13*that.m21+self.m23*that.m22+self.m33*that.m23+self.m43*that.m24,
self.m14*that.m21+self.m24*that.m22+self.m34*that.m23+self.m44*that.m24,
self.m11*that.m31+self.m21*that.m32+self.m31*that.m33+self.m41*that.m34,
self.m12*that.m31+self.m22*that.m32+self.m32*that.m33+self.m42*that.m34,
self.m13*that.m31+self.m23*that.m32+self.m33*that.m33+self.m43*that.m34,
self.m14*that.m31+self.m24*that.m32+self.m34*that.m33+self.m44*that.m34,
self.m11*that.m41+self.m21*that.m42+self.m31*that.m43+self.m41*that.m44,
self.m12*that.m41+self.m22*that.m42+self.m32*that.m43+self.m42*that.m44,
self.m13*that.m41+self.m23*that.m42+self.m33*that.m43+self.m43*that.m44,
self.m14*that.m41+self.m24*that.m42+self.m34*that.m43+self.m44*that.m44
)
end,
__tostring = function(self)
return "{ \n "..tostring(self.m11)..", "..tostring(self.m12)..", "..tostring(self.m13)..", "..tostring(self.m14).."\n"
.." "..tostring(self.m21)..", "..tostring(self.m22)..", "..tostring(self.m23)..", "..tostring(self.m24).."\n"
.." "..tostring(self.m31)..", "..tostring(self.m32)..", "..tostring(self.m33)..", "..tostring(self.m34).."\n"
.." "..tostring(self.m41)..", "..tostring(self.m42)..", "..tostring(self.m43)..", "..tostring(self.m44).."\n}"
end,
}
Matrix4.__index = Matrix4
-- Bounding Box ====================
-- Axis-aligned bounding box (AABB)
Box = {
-- Vector3 min
-- Vector3 max
-- Creates a new box
--- @param minVector Vector3 (optional)
--- @param maxVector Vector3 (optional)
new = function(self, minVector, maxVector)
local o = {}
setmetatable(o,self)
o.min = minVector or Vector3:new()
o.max = maxVector or Vector3:new()
return o
end,
-- Creates a new box from another box
--- @param that Box
clone = function(self, that)
local o = {}
setmetatable(o,self)
o.min = Vector3:clone(that.min)
o.max = Vector3:clone(that.max)
return o
end,
-- Creates a new box from a sphere
--- @param sphere Sphere
newFromSphere = function(self, sphere)
local o = {}
setmetatable(o,self)
local corner = Vector3:new(sphere.radius, sphere.radius, sphere.radius)
o.min = sphere.center - corner
o.max = sphere.center + corner
return o
end,
-- Checks if the box contains a Vector3
--- @param v Vector3
containsVector = function(self, v)
return self.min.x <= v.x and self.max.x >= v.x
and self.min.y <= v.y and self.max.y >= v.y
and self.min.z <= v.z and self.max.z >= v.z
end,
-- Checks if the box contains a Box
--- @param that Box
containsBox = function(self, that)
return self.min.x <= that.min.x and self.max.x >= that.max.x
and self.min.y <= that.min.y and self.max.y >= that.max.y
and self.min.z <= that.min.z and self.max.z >= that.max.z
end,
-- Checks if the box contains a Sphere
--- @param sphere Sphere
containsSphere = function(self, sphere)
return sphere.center.x - self.min.x >= sphere.radius
and sphere.center.y - self.min.y >= sphere.radius
and sphere.center.z - self.min.z >= sphere.radius
and self.max.x - sphere.center.x >= sphere.radius
and self.max.y - sphere.center.y >= sphere.radius
and self.max.z - sphere.center.z >= sphere.radius
end,
-- Checks if the box intersects a Box
--- @param that Box
--- @return boolean
intersectsBox = function(self, that)
if((self.max.x >= box.min.x) and (self.min.x <= box.max.x)) then
if((self.max.y < box.min.y) or (self.min.y > box.max.y)) then
return false
end
return (self.max.z >= box.min.z) and (self.min.z <= box.max.z)
end
return false
end,
-- Checks if the box intersects a Sphere
--- @param sphere Sphere
--- @return boolean
intersectsSphere = function(self, sphere)
if (sphere.center.x - self.min.x > sphere.radius
and sphere.center.y - self.min.y > sphere.radius
and sphere.center.z - self.min.z > sphere.radius
and self.max.x - sphere.center.x > sphere.radius
and self.max.y - sphere.center.y > sphere.radius
and self.max.z - sphere.center.z > sphere.radius) then
return true
end
local dmin = 0.
if(sphere.center.x - self.min.x <= sphere.radius) then
dmin = dmin + (sphere.center.x - self.min.x) * (sphere.center.x - self.min.x)
elseif(self.max.x - sphere.center.x <= sphere.radius) then
dmin = dmin + (sphere.center.x - self.max.x) * (sphere.center.x - self.max.x)
end
if(sphere.center.y - self.min.y <= sphere.radius) then
dmin = dmin + (sphere.center.y - self.min.y) * (sphere.center.y - self.min.y)
elseif(self.max.y - sphere.center.y <= sphere.radius) then
dmin = dmin + (sphere.center.y - self.max.y) * (sphere.center.y - self.max.y)
end
if(sphere.center.z - self.min.z <= sphere.radius) then
dmin = dmin + (sphere.center.z - self.min.z) * (sphere.center.z - self.min.z)
elseif(self.max.z - sphere.center.z <= sphere.radius) then
dmin = dmin + (sphere.center.z - self.max.z) * (sphere.center.z - self.max.z)
end
return (dmin <= sphere.radius * sphere.radius)
end,
--- @return table[] corners indexed array (8 elements)
getCorners = function(self)
return {
Vector3:new(self.min.x, self.max.y, self.max.z),
Vector3:new(self.max.x, self.max.y, self.max.z),
Vector3:new(self.max.x, self.min.y, self.max.z),
Vector3:new(self.min.x, self.min.y, self.max.z),
Vector3:new(self.min.x, self.max.y, self.min.z),
Vector3:new(self.max.x, self.max.y, self.min.z),
Vector3:new(self.max.x, self.min.y, self.min.z),
Vector3:new(self.min.x, self.min.y, self.min.z)
}
end,
-- Converts the box to a vertex grid
--- @param edgeCount number divides planes into this number of edges
--- @return table Vector3 vertex array
toGrid = function(self, edgeCount)
local grid = {}
local length3d = self.max - self.min
local vertexCount = edgeCount + 1 -- add extreme edges
local chunkDistance3d = length3d / vertexCount
local i = 1
for ix = 0, vertexCount do