-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.lua
More file actions
58 lines (44 loc) · 1.15 KB
/
world.lua
File metadata and controls
58 lines (44 loc) · 1.15 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
World = {}
World.__index = World
local sx, sy = guiGetScreenSize()
function World.new(gravity)
local self = setmetatable({}, World)
self.objects = {}
self.gravity = gravity
return self
end
function World:add(obj)
table.insert(self.objects, obj)
end
function World:remove(obj)
for i, v in ipairs(self.objects) do
if v == obj then table.remove(self.objects, i) end
end
end
function World:step(delta)
self:resolve_collision(delta)
for _, obj in ipairs(self.objects) do
obj.force = {
obj.force[1] + (obj.mass * self.gravity[1]),
obj.force[2] + (obj.mass * self.gravity[2]),
}
obj.velocity = {
obj.velocity[1] + (obj.force[1] / obj.mass * delta),
obj.velocity[2] + (obj.force[2] / obj.mass * delta)
}
obj.transform.position = {
obj.transform.position[1] + (obj.velocity[1] * delta),
obj.transform.position[2] + (obj.velocity[2] * delta)
}
obj.force = { 0, 0 }
end
end
function World:resolve_collision(delta)
local collisions = {}
for _, a in ipairs(self.objects) do
for _, b in ipairs(self.objects) do
if (a == b) then break end
AABB(a, b)
end
end
end