-
Notifications
You must be signed in to change notification settings - Fork 0
/
many_points_in_box_no_interaction.jl
61 lines (57 loc) · 1.37 KB
/
many_points_in_box_no_interaction.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
using Plots
using LinearAlgebra
function many_points_in_box_no_interaction(N, L, H, T, dt)
number_iterations = Int(T/dt + 1)
positions = Array{Array{Float64, 2}, 1}(undef, N)
velocities = Array{Array{Float64, 2}, 1}(undef, N)
# initialize
for i=1:N
positions[i] = zeros(number_iterations, 2)
velocities[i] = zeros(number_iterations, 2)
positions[i][1, :] = rand(1, 2)
velocities[i][1, :] = rand(1, 2)
end
t = 0
my_plot = plot()
plot!(xlims = (0, L), ylims = (0, H))
for i=1:N
my_plot = scatter!((positions[i][1, 1], positions[i][1, 2]), show=false, legend=false)
end
display(my_plot)
sleep(0.025)
my_plot = plot(xlims = (0, L), ylims = (0, H))
j = 0
while(t<=T)
j+= 1
for i=1:N
x = positions[i][j, 1]
y = positions[i][j, 2]
v_x = velocities[i][j, 1]
v_y = velocities[i][j, 2]
## check for collision
if (x<=0) | (x>= 1) # side wall collision
v_x = -v_x
end
if (y<=0) | (y>= 1) # roof/ground collision
v_y = -v_y
end
# update position
x = x + v_x*dt
y = y + v_y*dt
if(t+dt>T)
return
else
# update
positions[i][j+1, 1] = x
positions[i][j+1, 2] = y
velocities[i][j+1, 1] = v_x
velocities[i][j+1, 2] = v_y
scatter!((positions[i][j, 1], positions[i][j, 2]), show=false, legend=false)
end
end
t += dt
display(my_plot)
sleep(0.025)
my_plot = plot(xlims = (0, L), ylims = (0, H))
end
end