-
Notifications
You must be signed in to change notification settings - Fork 2
/
tilephysics.p8
220 lines (218 loc) · 4.2 KB
/
tilephysics.p8
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
physics = {}
function physics.collided(body1, body2)
local result = true
for i=1,2 do
result = result and
body1.pos[i] < body2.pos[i] + body2.size[i] and
body2.pos[i] < body1.pos[i] + body1.size[i]
end
return result
end
function physics.togrid(pos)
--return tools.map(pos, function(num)
--return flr(num / 8)
--end)
return {flr(pos[1]/8), flr(pos[2]/8)}
end
function physics.fromgrid(pos)
return {pos[1]*8, pos[2]*8}
end
physics.world = oop.class{
bodies={},
gravity={0, .5},
grid="req",
}
function physics.world:update()
for body in all(self.bodies) do
if not body.static then
if body.mass[1] or body.mass[2] then
body:shove{
self.gravity[1] * body.weight[1], self.gravity[2] * body.weight[2]
}
if body.friction[1] ~= 0 or body.friction[2] ~= 0 then
body:slow(body.friction)
end
end
if body.check then
body.collisions = {}
for body2 in all(self.bodies) do
if body ~= body2 then
body:checkcollided(
body2,
band(body.layer, body2.layer) != 0
)
end
end
body.tcollisions = {}
body:gridcollided(self)
end
body:update()
end
end
end
function physics.world:addbody(body)
add(self.bodies, body)
return body
end
function physics.world:gettile(pos)
local x = self.grid[pos[1]]
if x then
return x[pos[2]]
end
end
physics.body = oop.class({
pos="req",
size="req",
vel={0, 0},
mass={1, 1},
weight={1, 1},
friction={.1, 0},
collisions={},
tcollisions={},
static=false,
layer=0x1,
check=false,
}, {}, function(self)
self.tsize = physics.togrid(
self.size
)
end)
local body = physics.body
function body:shove(vel)
for i=1,2 do
self.vel[i] += vel[i] * self.mass[i]
end
end
function body:update()
for i=1,2 do
self.pos[i] += self.vel[i]
end
end
function body:slow(vel)
for i=1,2 do
if self.vel[i] > 0 then
self.vel[i] -= vel[i] * self.mass[i]
if self.vel[i] < 0 then
self.vel[i] = 0
end
elseif self.vel[i] < 0 then
self.vel[i] += vel[i] * self.mass[i]
if self.vel[i] > 0 then
self.vel[i] = 0
end
end
end
end
function body:align(i, body)
if self.vel[i] >= 0 then
self.pos[i] = body.pos[i] - self.size[i]
else
self.pos[i] = body.pos[i] + body.size[i]
end
self.vel[i] = 0
end
function body:checkcollided(body, move)
local oldpos = tools.assign(self.pos)
local checks = {}
for i=1,2 do
local pos = tools.assign(oldpos)
pos[i] += self.vel[i]
--if self:collided(body, pos) then
if physics.collided({
pos=pos, size=self.size
}, body) then
checks[i] = true
add(self.collisions, body)
if move then
self:align(i, body)
end
end
end
if
not checks[1] and
not checks[2]
then
local pos = {
oldpos[1] + self.vel[1],
oldpos[2] + self.vel[2],
}
--if self:collided(body, pos) then
if physics.collided({
pos=pos, size=self.size
}, body) then
add(self.collisions, body)
if move then
for i=1,2 do
self:align(i, body)
end
end
end
end
end
function body:gridcollided(world)
local oldpos = tools.assign(self.pos)
for i=1,2 do
local pos = tools.assign(oldpos)
pos[i] += self.vel[i]
local tpos = self:findtpos(pos)
for pos in all(tpos) do
tile = world:gettile(pos)
if tile then
if band(self.layer, tile) != 0 then
self:align(i, {
pos=physics.fromgrid(pos),
size={8, 8}
})
end
add(self.tcollisions, pos)
break
end
end
end
if #self.tcollisions == 0 then
local tpos = self:findtpos({
oldpos[1] + self.vel[1],
oldpos[2] + self.vel[2],
})
for pos in all(tpos) do
tile = world:gettile(pos)
if tile then
if band(self.layer, tile) != 0 then
for i=1,2 do
self:align(i, {
pos=physics.fromgrid(pos),
size={8, 8}
})
end
end
add(self.tcollisions, pos)
break
end
end
end
end
function body:findtpos(pos)
local tpos = physics.togrid(pos)
local poss = {tpos}
for size in all{
{1, 1},
{1, 0},
{0, 1},
} do
local tpos2 = physics.togrid{
pos[1]+(self.size[1]-1)*size[1],
pos[2]+(self.size[2]-1)*size[2],
}
if tpos2[1] != tpos[1] or
tpos2[2] != tpos[2] then
add(poss, tpos2)
end
end
return poss
end
--[[
function physics.collided(body, pos)
return physics.collided({
pos=pos, size=self.size
}, body)
end