-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
170 lines (137 loc) · 4.01 KB
/
main.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
local RK = require "RungeKutta"
local UI = require "UI"
function dydt(t, y)
local dydt = {}
local a1, a2, p1, p2 = y.a1, y.a2, y.p1, y.p2
local sin = math.sin(a1-a2)
local cos = math.cos(a1-a2)
local denom = m1 * l^2 * ( 1 + u * sin^2 )
local A1 = ( p1 * p2 * sin ) / denom
local A2 = ( p1^2 * u - 2 * p1 * p2 * u * cos + p2^2 * (1 + u) )
* math.sin(2 * (a1-a2))
/ ( 2 * m1 * l^2 * ( 1 + u * sin^2 )^2 )
dydt.a1 = ( p1 - p2 * cos ) / denom
dydt.a2 = ( p2 * (1 + u) - p1 * u * cos ) / denom
dydt.p1 = -m1 * (1 + u) * g * l * math.sin(a1) - A1 + A2
dydt.p2 = -m1 * u * g * l * math.sin(a2) + A1 - A2
return dydt
end
function init ()
u = initU[1]
m1 = 1
m2 = u
return
{ a1 = a1[1] / 360 * 2 * math.pi
, a2 = a2[1] / 360 * 2 * math.pi
, p1 = 0
, p2 = 0
}
end
function findPositions( a1, a2 )
x1 = x0 + l * scale * math.sin(y.a1)
x2 = x1 + l * scale * math.sin(y.a2)
y1 = y0 + l * scale * math.cos(y.a1)
y2 = y1 + l * scale * math.cos(y.a2)
end
function newCanvas ( canvas )
return love.graphics.newCanvas( love.graphics.getDimensions() )
end
function love.load ()
a1 = { 90 }
a2 = { 90 }
initSpeed = { 1 }
initU = { 1 }
l = 1
t = 0
g = 9.80665
y = init()
radius = 10
scale = 100
x0 = love.graphics.getWidth() * 0.6
y0 = love.graphics.getHeight() / 2
last = {}
startSimulation = function () pause = false end
pauseSimulation = function () pause = true end
resetSimulation = function ()
y = init()
findPositions( a1, a2 )
last.x2 = x2
last.y2 = y2
trace = newCanvas()
pause = true
end
resetSimulation()
love.graphics.setBackgroundColor(1,1,1)
end
function love.update ( dt )
if not pause then
dt = dt * initSpeed[1]
t, y = RK.rk4( y, dydt, t, dt )
end
end
function love.draw ()
UI.draw { x = 20, y = 20,
UI.horizontal {
UI.button( "Start", startSimulation ),
UI.button( "Pause", pauseSimulation ),
UI.button( "Reset", resetSimulation ),
},
UI.label {"alpha 1"},
UI.slider( 0, 360, a1 ),
UI.label {"alpha 2"},
UI.slider( 0, 360, a2 ),
UI.label {"mass"},
UI.slider( 0.1, 3, initU ),
UI.label {"animation speed"},
UI.slider( 0.5, 3, initSpeed ),
}
local r,g,b,a = love.graphics.getColor()
findPositions( a1, a2 )
local newtrace = newCanvas()
love.graphics.setCanvas(newtrace)
love.graphics.setColor(0,0,0,0.98)
love.graphics.draw(trace)
love.graphics.setColor(0,0,0,1)
love.graphics.line( last.x2 or x2, last.y2 or y2, x2, y2 )
trace = newtrace
love.graphics.setColor(r,g,b,a)
animation = newCanvas()
love.graphics.setCanvas(animation)
last.x2 = x2
last.y2 = y2
love.graphics.setColor(.5,.5,.5)
love.graphics.line( x0, y0, x1, y1 )
love.graphics.line( x1, y1, x2, y2 )
local u = initU[1]
love.graphics.setColor(0.5,0.5,0.5)
love.graphics.circle( "fill", x0, y0, 5 )
love.graphics.circle( "fill", x1, y1, radius * 1 )
love.graphics.setColor(0,0,0)
love.graphics.circle( "line", x1, y1, radius * 1 )
love.graphics.setColor(0.5,0.5,0.5)
love.graphics.circle( "fill", x2, y2, radius * u ^ (1/3) )
love.graphics.setColor(0,0,0)
love.graphics.circle( "line", x2, y2, radius * u ^ (1/3) )
love.graphics.setCanvas()
love.graphics.draw(trace)
love.graphics.draw(animation)
love.graphics.setColor(r,g,b,a)
end
function love.keypressed ( key )
if key == "escape" then
love.event.quit()
end
end
function love.mousepressed ( x, y, button )
if button == 1 then
UI.mousepressed {x = x, y = y}
end
end
function love.mousereleased ( x, y, button )
if button == 1 then
UI.mousereleased {x = x, y = y}
end
end
function love.mousemoved ( x, y )
UI.mousemoved {x = x, y = y}
end