-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo_body.jl
executable file
·69 lines (52 loc) · 1.89 KB
/
two_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
using Animations
using Javis
using NBodySimulator
using StaticArrays
# Define the bodies
body1 = MassBody(SVector(0.0, 1.0, 0.0), SVector(5.775e-6, 0.0, 0.0), 2.0)
body2 = MassBody(SVector(0.0, -1.0, 0.0), SVector(-5.775e-6, 0.0, 0.0), 2.0)
G = 6.673e-11
system = GravitationalSystem([body1, body2], G)
tspan = (0.0, 1111150.0)
# run the simulation
simulation = NBodySimulation(system, tspan)
sim_result = run_simulation(simulation, saveat = 10)
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)]
#Convert to Points
planet1_point =
Point.((200 .* getindex.(planet1_pos, 1)), ((200 .* getindex.(planet1_pos, 2) .- 100)))
planet2_point =
Point.((200 .* getindex.(planet2_pos, 1)), ((200 .* getindex.(planet2_pos, 2) .+ 100)))
# Start the animation
function ground(args...)
background("black")
sethue("white")
end
function planet(p, r, color)
sethue(color)
return circle(p, r, :fill)
end
function make_animation()
nframes = 200
vid = Video(500, 500)
Background(1:nframes, ground)
planet1 = Object(1:nframes, (args...) -> planet(planet1_point[1], 10.0, "red"))
planet2 = Object(1:nframes, (args...) -> planet(planet2_point[1], 10.0, "blue"))
planet1_anim = Animation(
collect(range(0, stop = 1, length = length(planet1_point))),
planet1_point,
[linear() for _ in 1:(length(planet1_point) - 1)],
)
planet2_anim = Animation(
collect(range(0, stop = 1, length = length(planet2_point))),
planet2_point,
[linear() for _ in 1:(length(planet2_point) - 1)],
)
act!(planet1, Action(1:nframes, planet1_anim, translate()))
act!(planet2, Action(1:nframes, planet2_anim, translate()))
render(vid; pathname = "two_body.gif")
end
make_animation()