-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlockCollider.lua
242 lines (225 loc) · 7.08 KB
/
BlockCollider.lua
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
fysiks.BlockCollider = {
}
fysiks.BlockCollider.__index = fysiks.BlockCollider
function fysiks.BlockCollider:new(pos)
local c = setmetatable({}, fysiks.BlockCollider)
c.colliders = {}
c.nodes = {}
c.pos = pos
c.body = fysiks.Rigidbody:new()
c.body.position = c:getNodePos({x = 0, y = 0, z = 0})
c.body.static = true
c.body.friction = 1
c.body.sleepTimer = fysiks.SLEEP_TIME + 1
c.body.getFriction = function(body, point, normal) return c:getFriction(point, normal) end
c.body.getBounciness = function(body, point, normal) return c:getBounciness(point, normal) end
return c
end
function fysiks.BlockCollider:getLocalPos(pos)
return {
x = pos.x - self.pos.x * fysiks.BLOCKSIZE,
y = pos.y - self.pos.y * fysiks.BLOCKSIZE,
z = pos.z - self.pos.z * fysiks.BLOCKSIZE
}
end
function fysiks.BlockCollider:getNodePos(pos)
return {
x = self.pos.x * fysiks.BLOCKSIZE + pos.x,
y = self.pos.y * fysiks.BLOCKSIZE + pos.y,
z = self.pos.z * fysiks.BLOCKSIZE + pos.z,
}
end
function fysiks.BlockCollider:getFriction(point, normal)
local x = math.floor(point.x + 0.5 - normal.x * 0.5)
local y = math.floor(point.y + 0.5 - normal.y * 0.5)
local z = math.floor(point.z + 0.5 - normal.z * 0.5)
if self.nodes[x] and self.nodes[x][y] and
self.nodes[x][y][z] and self.nodes[x][y][z].friction then
return self.nodes[x][y][z].friction
end
return 1
end
function fysiks.BlockCollider:getBounciness(point, normal)
local x = math.floor(point.x + 0.5 - normal.x * 0.5)
local y = math.floor(point.y + 0.5 - normal.y * 0.5)
local z = math.floor(point.z + 0.5 - normal.z * 0.5)
if self.nodes[x] and self.nodes[x][y] and
self.nodes[x][y][z] and self.nodes[x][y][z].bounciness then
return self.nodes[x][y][z].bounciness
end
return 1
end
function fysiks.BlockCollider:extractNodeboxCollider(nodebox)
local boxes = {}
if nodebox.type == "regular" then
boxes = {{{x = -0.5, y = -0.5, z = -0.5}, {x = 0.5, y = 0.5, z = 0.5}}}
elseif nodebox.type == "fixed" or nodebox.type == "leveled"
or nodebox.type == "connected" then
if nodebox.fixed and #nodebox.fixed > 0 then
if type(nodebox.fixed[1]) == "number" then
local box = nodebox.fixed
table.insert(boxes, {{x = box[1], y = box[2], z = box[3]}, {x = box[4], y = box[5], z = box[6]}})
else
for k, box in ipairs(nodebox.fixed) do
table.insert(boxes, {{x = box[1], y = box[2], z = box[3]}, {x = box[4], y = box[5], z = box[6]}})
end
end
end
--TODO: figure out the leveled nodebox
--TODO: figure out the connections of the node to be more accurate
if nodebox.type == "connected" and nodebox.disconnected and #nodebox.disconnected > 0 then
if type(nodebox.disconnected[1]) == "number" then
local box = nodebox.disconnected
table.insert(boxes, {{x = box[1], y = box[2], z = box[3]}, {x = box[4], y = box[5], z = box[6]}})
else
for k, box in ipairs(nodebox.disconnected) do
table.insert(boxes, {{x = box[1], y = box[2], z = box[3]}, {x = box[4], y = box[5], z = box[6]}})
end
end
end
elseif nodebox.type == "wallmounted" then
--TODO: figure out wallmounted nodebox
end
return boxes
end
function fysiks.BlockCollider:getNodeColliderInfo(node)
local nodeDef = minetest.registered_nodes[node.name]
if not nodeDef.walkable then
return nil
end
local drawtype = nodeDef.drawtype
local collisionbox = nodeDef.collision_box
local boxtype = nil
if collisionbox then
boxtype = {type = "nodebox", boxes = self:extractNodeboxCollider(collisionbox)}
elseif drawtype == "normal" or drawtype == "mesh"
or drawtype:sub(1, #"allfaces") == "allfaces"
or drawtype:sub(1, #"glasslike") == "glasslike" then
boxtype = {type = "normal"}
elseif drawtype == "nodebox" then
local nodebox = nodeDef.node_box
boxtype = {type = "nodebox", boxes = self:extractNodeboxCollider(nodebox)}
end
if boxtype and fysiks.nodeDefinitions[node.name] then
boxtype.friction = fysiks.nodeDefinitions[node.name].friction or 1
boxtype.bounciness = fysiks.nodeDefinitions[node.name].friction or 0
end
return boxtype
end
function fysiks.BlockCollider:calculateNodePositions()
self.nodes = {}
for x = 0, fysiks.BLOCKSIZE - 1, 1 do
self.nodes[x] = {}
for y = 0, fysiks.BLOCKSIZE - 1, 1 do
self.nodes[x][y] = {}
for z = 0, fysiks.BLOCKSIZE - 1, 1 do
local pos = self:getNodePos({x = x, y = y, z = z})
self.nodes[x][y][z] = self:getNodeColliderInfo(minetest.get_node(pos))
end
end
end
end
function fysiks.BlockCollider:getNode(pos)
local localpos = self:getLocalPos(pos)
return self.nodes[localpos.x][localpos.y][localpos.z]
end
function fysiks.BlockCollider:recalculate()
for _, coll in ipairs(self.colliders) do
coll.removed = true
end
self.colliders = {}
local boxes = {}
for y = 0, fysiks.BLOCKSIZE - 1, 1 do
boxes[y] = {}
for x = 0, fysiks.BLOCKSIZE - 1, 1 do
local box = nil
boxes[y][x] = {}
for z = 0, fysiks.BLOCKSIZE - 1, 1 do
local boxtype = self.nodes[x][y][z]
if boxtype and boxtype.type == "normal" then
if box then
box.length = box.length + 1
else
box = {start = z, length = 1, width = 1, height = 1}
end
else
if box then
table.insert(boxes[y][x], box)
box = nil
end
if boxtype and boxtype.type == "nodebox" then
for k, box in ipairs(boxtype.boxes) do
local min = vector.add({x = x, y = y, z = z}, box[1])
local max = vector.add({x = x, y = y, z = z}, box[2])
local cuboid = fysiks.Cuboid:new(self.body, min, max)
cuboid:setPosition(self.body.position)
table.insert(self.colliders, cuboid)
end
end
end
end
if box then
table.insert(boxes[y][x], box)
end
end
end
for y = 0, fysiks.BLOCKSIZE - 1, 1 do
for x = 1, fysiks.BLOCKSIZE - 1, 1 do
for _, box in ipairs(boxes[y][x]) do
local done = false
for k2, box2 in ipairs(boxes[y][x - 1]) do
if box.start == box2.start and box.length == box2.length then
table.remove(boxes[y][x - 1], k2)
box.width = box.width + box2.width
done = true
break
end
end
if done then
break
end
end
end
end
for y = 1, fysiks.BLOCKSIZE - 1, 1 do
for x = 0, fysiks.BLOCKSIZE - 1, 1 do
for _, box in ipairs(boxes[y][x]) do
local done = false
for k2, box2 in ipairs(boxes[y - 1][x]) do
if box.start == box2.start and box.length == box2.length and box.width == box2.width then
table.remove(boxes[y - 1][x], k2)
box.height = box.height + box2.height
done = true
break
end
end
if done then
break
end
end
end
end
for y = 0, fysiks.BLOCKSIZE - 1, 1 do
for x = 0, fysiks.BLOCKSIZE - 1, 1 do
for _, box in ipairs(boxes[y][x]) do
local min = {
x = x - box.width + 0.5,
y = y - box.height + 0.5,
z = box.start - 0.5
}
local max = {
x = x + 0.5,
y = y + 0.5,
z = box.start + box.length - 0.5
}
local cuboid = fysiks.Cuboid:new(self.body, min, max)
cuboid:setPosition(self.body.position)
table.insert(self.colliders, cuboid)
end
end
end
self.body.collisionBoxes = self.colliders
self.body.sleepTimer = 0
self.body.forceCollisionCheck = true
table.insert(fysiks.updatedBlockColliders, self)
end