-
Notifications
You must be signed in to change notification settings - Fork 1
/
three_body.jl
executable file
·86 lines (66 loc) · 2.51 KB
/
three_body.jl
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
using Animations
using Colors
using Javis
using NBodySimulator
using StaticArrays
m1 = MassBody(SVector(-0.995492, 0.0, 0.0), SVector(-0.347902, -0.53393, 0.0), 1.0)
m2 = MassBody(SVector(0.995492, 0.0, 0.0), SVector(-0.347902, -0.53393, 0.0), 1.0)
m3 = MassBody(SVector(0.0, 0.0, 0.0), SVector(0.695804, 1.067860, 0.0), 1.0)
G = 1
system = GravitationalSystem([m1, m2, m3], G)
tspan = (0.0, 2pi);
simulation = NBodySimulation(system, tspan)
sim_result = run_simulation(simulation,saveat = 0.01)
sim_data = sim_result.solution.u
# extract the data
planet1_pos = [(sim_data[x][1, 1], sim_data[x][2, 1]) for x in 1:length(sim_data)]
planet2_pos = [(sim_data[x][1, 2], sim_data[x][2, 2]) for x in 1:length(sim_data)]
planet3_pos = [(sim_data[x][1, 3], sim_data[x][2, 3]) for x in 1:length(sim_data)]
# scaling factor
scaling_factor = 210
#Convert to Points
planet1_point = Point.((scaling_factor .* getindex.(planet1_pos, 1)), ((scaling_factor .* getindex.(planet1_pos, 2))))
planet2_point = Point.((scaling_factor .* getindex.(planet2_pos, 1)), ((scaling_factor .* getindex.(planet2_pos, 2))))
planet3_point = Point.((scaling_factor .* getindex.(planet3_pos, 1)), ((scaling_factor .* getindex.(planet3_pos, 2))))
#Julia logo colors
logocolors = Colors.JULIA_LOGO_COLORS
red = logocolors.red
green = logocolors.green
purple = logocolors.purple
blue = logocolors.blue
function ground(args...)
background("black")
sethue(blue)
end
function planet(p, r, color)
sethue(color)
return circle(p, r, :fill)
end
function make_animation(red, green, purple)
nframes = 200
vid = Video(500, 500)
Background(1:nframes, ground)
planet1_anim = Animation(
collect(range(0, stop = 1, length = length(planet1_point))),
planet1_point,
linear()
)
planet2_anim = Animation(
collect(range(0, stop = 1, length = length(planet2_point))),
planet2_point,
linear()
)
planet3_anim = Animation(
collect(range(0, stop = 1, length = length(planet3_point))),
planet3_point,
linear()
)
planet1 = Object(1:nframes, (args...) -> planet(O, 20.0, red))
planet2 = Object(1:nframes, (args...) -> planet(O, 20.0, purple))
planet3 = Object(1:nframes, (args...) -> planet(O, 20.0, green))
act!(planet1, Action(1:nframes, planet1_anim, translate()))
act!(planet2, Action(1:nframes, planet2_anim, translate()))
act!(planet3, Action(1:nframes, planet3_anim, translate()))
render(vid; pathname = "three_body.gif")
end
make_animation(red, green, purple)