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
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Vector3 = require( "vector3" )
local BoundingBox = {}
local metatable = {
__index = BoundingBox,
__tostring = function( b )
return "{{"..tostring( b.min.x )..", "..tostring( b.min.y )..", "..tostring( b.min.z ).."}, {"
..tostring( b.max.x )..", "..tostring( b.max.y )..", "..tostring( b.max.z ).."}}"
end,
-- __add = function( v1, v2 )
-- return Vector2:new( v1.x + v2.x, v1.y + v2.y )
-- end,
-- __sub = function( v1, v2 )
-- return Vector2:new( v1.x - v2.x, v1.y - v2.y )
-- end,
-- __mul = function( v1, v2 )
-- return Vector2:new( v1.x * v2.x, v1.y * v2.y )
-- end,
-- __div = function( v1, v2 )
-- return Vector2:new( v1.x / v2.x, v1.y / v2.y )
-- end,
-- __mod = function( v, value )
-- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
-- end,
-- __pow = function( v, value )
-- return Vector2:new( v.x ^ value, v.y ^ value )
-- end,
-- __unm = function( v )
-- return Vector2:new( -v.x, -v.y )
-- end,
__eq = function( b1, b2 )
return b1.min == b2.min and b1.max == b2.max
end,
}
--- Expects format { ... }, { ... }
function BoundingBox:new( min, max )
local object = setmetatable( {}, metatable )
object.min = Vector3:newT( min )
object.max = Vector3:newT( max )
return object
end
--- Expects format { { ... }, { ... } }
function BoundingBox:newT( t )
local object = setmetatable( {}, metatable )
object.min = Vector3:newT( t[1] )
object.max = Vector3:newT( t[2] )
return object
end
--- Expects format { Vector3, Vector3 }
function BoundingBox:newV( min, max )
local object = setmetatable( {}, metatable )
object.min = min:clone()
object.max = max:clone()
return object
end
--- Expects format BoundingBox
function BoundingBox:newB( b )
local object = setmetatable( {}, metatable )
object.min = b.min:clone()
object.max = b.max:clone()
return object
end
function BoundingBox:serialize()
return table.concat( { "BoundingBox:new({", self.min.x, ",", self.min.y, ",", self.min.z, "},{", self.max.x, ",", self.max.y, ",", self.max.z, "})" } )
end
--- Expects format { ... }, { ... }
function BoundingBox:set( min, max )
self.min:setT( min )
self.max:setT( max )
end
--- Expects format { { ... }, { ... } }
function BoundingBox:setT( t )
self.min:setT( t[1] )
self.max:setT( t[2] )
end
--- Expects format { Vector3, Vector3 }
function BoundingBox:setV( min, max )
self.min:setV( min )
self.max:setV( max )
end
--- Expects format BoundingBox
function BoundingBox:setB( b )
self.min:setV( b.min )
self.max:setV( b.max )
end
function BoundingBox:arr()
return { { self.min.x, self.min.y, self.min.z }, { self.max.x, self.max.y, self.max.z } }
end
function BoundingBox:unpack()
return self.min, self.max
end
function BoundingBox:clone()
return BoundingBox:newB( self )
end
function BoundingBox:scale( scalar )
return BoundingBox:newV( self.min, self.max:scale( scalar ) )
end
function BoundingBox:getPoints()
return {
self.min:clone(), -- Down back left.
Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right.
Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left.
Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right.
Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left.
Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right.
Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left.
self.max:clone(), -- Up front right.
}
end
-- Assumes max is used as size.
function BoundingBox:checkCollisionBox( b )
return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() )
end
-- Assumes max is used as size.
function BoundingBox:checkCollisionSphere( center, radius )
return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius )
end
-- Assumes max is used as size.
function BoundingBox:checkCollisionPoint( point )
local max = self.min + self.max
return self.min.x <= point.x
and self.min.y <= point.y
and self.min.z <= point.z
and point.x <= max.x
and point.y <= max.y
and point.z <= max.z
end
-- Assumes max is used as size.
function BoundingBox:getRayCollision( ray )
return RL.GetRayCollisionBox( ray, self:maxToPos() )
end
-- Max to position from size.
function BoundingBox:maxToPos()
return BoundingBox:newV( self.min, self.min + self.max )
end
-- Max to size from position.
function BoundingBox:maxToSize()
return BoundingBox:newV( self.min, self.max - self.min )
end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, BoundingBox:new( { 0, 0, 0 }, { 0, 0, 0 } ) )
end
--- Expects format { ... }, { ... }
function BoundingBox:temp( min, max )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.min = Vector3:tempT( min )
object.max = Vector3:tempT( max )
return object
end
--- Expects format { { ... }, { ... } }
function BoundingBox:tempT( t )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.min = Vector3:tempT( t[1] )
object.max = Vector3:tempT( t[2] )
return object
end
--- Expects format { Vector3, Vector3 }
function BoundingBox:tempV( min, max )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.min = Vector3:tempV( min )
object.max = Vector3:tempV( max )
return object
end
--- Expects format BoundingBox
function BoundingBox:tempB( b )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.min = Vector3:tempV( b.min )
object.max = Vector3:tempV( b.max )
return object
end
function BoundingBox:getTempId()
return curTemp
end
return BoundingBox
|