-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_balls.lua
More file actions
220 lines (194 loc) · 6.3 KB
/
Copy pathdemo_balls.lua
File metadata and controls
220 lines (194 loc) · 6.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
local flint = require("flint")
local sys = flint.new(term)
local ok, why = sys:setGraphicsMode(true)
if not ok then
print("Grafikmodus not available: " .. why)
return
end
local vw, vh = sys.vWidth, sys.vHeight
local tw, th = sys.termWidth, sys.termHeight
local trailLayer = sys:addLayer(1)
local ballLayer = sys:addLayer(2)
local uiLayer = sys:addLayer(3)
local BALL_COLORS = {
{ ball = 0xff4444, trail = 0x661111 },
{ ball = 0x44aaff, trail = 0x113366 },
{ ball = 0x44ff88, trail = 0x116633 },
{ ball = 0xffcc00, trail = 0x664400 },
{ ball = 0xff66ff, trail = 0x662266 },
{ ball = 0x00ffee, trail = 0x005544 },
}
local GRAVITY = 0.18
local BOUNCE = 0.78
local FRICTION = 0.998
local BALL_R = 4
local NUM_BALLS = 6
local TRAIL_LEN = 18
local TRAIL_FADE = 0.82
math.randomseed(os.time and os.time() or 12345)
local balls = {}
for i = 1, NUM_BALLS do
local pal = BALL_COLORS[i]
local startX = math.floor(vw * (i / (NUM_BALLS + 1)))
local startY = math.floor(vh * 0.25)
balls[i] = {
x = startX,
y = startY,
vx = (math.random() - 0.5) * 4,
vy = (math.random() - 0.5) * 2 - 1,
r = BALL_R,
color = pal.ball,
trailColor = pal.trail,
trail = {},
trailHead = 1,
}
for t = 1, TRAIL_LEN do
balls[i].trail[t] = { x = startX, y = startY }
end
end
local function drawCircle(layer, cx, cy, r, color)
local r2 = r * r
for dy = -r, r do
local span = math.floor(math.sqrt(r2 - dy * dy) + 0.5)
for dx = -span, span do
local px = cx + dx
local py = cy + dy
if px >= 1 and px <= vw and py >= 1 and py <= vh then
layer:drawSubpixel(px, py, color)
end
end
end
end
local function drawTrailDot(layer, cx, cy, radius, color)
for dy = -radius, radius do
for dx = -radius, radius do
if dx*dx + dy*dy <= radius*radius then
local px = cx + dx
local py = cy + dy
if px >= 1 and px <= vw and py >= 1 and py <= vh then
layer:drawSubpixel(px, py, color)
end
end
end
end
end
local function resolveBallCollisions()
for i = 1, NUM_BALLS do
local a = balls[i]
for j = i + 1, NUM_BALLS do
local b = balls[j]
local dx = b.x - a.x
local dy = b.y - a.y
local dist2 = dx * dx + dy * dy
local minDist = a.r + b.r
if dist2 < minDist * minDist and dist2 > 0.0001 then
local dist = math.sqrt(dist2)
local nx = dx / dist
local ny = dy / dist
local dvx = a.vx - b.vx
local dvy = a.vy - b.vy
local dot = dvx * nx + dvy * ny
if dot > 0 then
local impulse = dot * BOUNCE
a.vx = a.vx - impulse * nx
a.vy = a.vy - impulse * ny
b.vx = b.vx + impulse * nx
b.vy = b.vy + impulse * ny
end
local overlap = (minDist - dist) * 0.5
a.x = a.x - nx * overlap
a.y = a.y - ny * overlap
b.x = b.x + nx * overlap
b.y = b.y + ny * overlap
end
end
end
end
local title = " FLINT BALL PHYSICS "
uiLayer:drawText(math.floor((tw - #title) / 2) + 1, 1, title, "0", "b")
uiLayer:drawText(2, th, "Q: Close R: Reset", "8", "f")
local running = true
local frame = 0
while running do
frame = frame + 1
for i = 1, NUM_BALLS do
local b = balls[i]
b.trail[b.trailHead] = { x = math.floor(b.x), y = math.floor(b.y) }
b.trailHead = b.trailHead % TRAIL_LEN + 1
b.vy = b.vy + GRAVITY
b.vx = b.vx * FRICTION
b.vy = b.vy * FRICTION
b.x = b.x + b.vx
b.y = b.y + b.vy
if b.x - b.r < 1 then
b.x = b.r + 1
b.vx = math.abs(b.vx) * BOUNCE
elseif b.x + b.r > vw then
b.x = vw - b.r
b.vx = -math.abs(b.vx) * BOUNCE
end
if b.y - b.r < 1 then
b.y = b.r + 1
b.vy = math.abs(b.vy) * BOUNCE
elseif b.y + b.r > vh then
b.y = vh - b.r
b.vy = -math.abs(b.vy) * BOUNCE
if math.abs(b.vy) < 0.5 then b.vy = -0.5 end
end
end
resolveBallCollisions()
trailLayer:clear()
ballLayer:clear()
for i = 1, NUM_BALLS do
local b = balls[i]
for t = 0, TRAIL_LEN - 2 do
local idx = (b.trailHead + t - 1) % TRAIL_LEN + 1
local pos = b.trail[idx]
if pos then
local ageFraction = t / (TRAIL_LEN - 1)
local radius = math.floor(ageFraction * (b.r - 1) + 1)
drawTrailDot(trailLayer, pos.x, pos.y, radius, b.trailColor)
end
end
end
for i = 1, NUM_BALLS do
local b = balls[i]
drawCircle(ballLayer, math.floor(b.x), math.floor(b.y), b.r, b.color)
local hx = math.floor(b.x) - math.floor(b.r * 0.35)
local hy = math.floor(b.y) - math.floor(b.r * 0.35)
if hx >= 1 and hy >= 1 and hx <= vw and hy <= vh then
trailLayer:drawSubpixel(hx, hy, 0xffffff)
end
end
sys:compileSubpixels()
sys:present()
local timer = os.startTimer(0.033) -- ~30 FPS
while true do
local ev, p1 = os.pullEvent()
if ev == "timer" and p1 == timer then
break
elseif ev == "char" then
if p1 == "q" or p1 == "Q" then
running = false
break
elseif p1 == "r" or p1 == "R" then
for i = 1, NUM_BALLS do
local b = balls[i]
b.x = math.floor(vw * (i / (NUM_BALLS + 1)))
b.y = math.floor(vh * 0.25)
b.vx = (math.random() - 0.5) * 5
b.vy = (math.random() - 0.5) * 3 - 1
for t = 1, TRAIL_LEN do
b.trail[t] = { x = b.x, y = b.y }
end
b.trailHead = 1
end
break
end
end
end
end
sys:setGraphicsMode(false)
sys:compileSubpixels()
sys:present()
term.setCursorPos(1, th)