-
-
Save ArbitRandomUser/463d18b1b08e1ba25b2d5e9cc6be3471 to your computer and use it in GitHub Desktop.
collision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Javis | |
video = Video(600,600) | |
nframes= 1500 | |
nballs = 50 | |
radii = 20 | |
Background(1:nframes, (v,o,f)-> background("grey27")) | |
ball(color,p=O) = Object(1:nframes , (v,o,f)-> begin | |
sethue(color) | |
circle(O,radii,:fill) | |
return O | |
end, | |
p | |
) | |
dot(p1,p2) = p1[1]*p2[1] + p1[2]*p2[2] | |
normvec(p) = p ./ sqrt(dot(p,p)) | |
""" | |
checks of collision b/w o1,o2 | |
and updates velocities | |
""" | |
function collision(o1::Object,o2::Object,f) | |
p1 = f==1 ? o1.start_pos : get_position(o1) | |
p2 = f==1 ? o2.start_pos : get_position(o2) | |
unitnorm = normvec(p1-p2) | |
v1 = o1.opts[:velocity] | |
v2 = o2.opts[:velocity] | |
dir = dot(v1,unitnorm) - dot(v2, unitnorm) | |
v1n = dot(v1,unitnorm) .* unitnorm | |
v2n = dot(v2,unitnorm) .* unitnorm | |
v1t = dot(v1,perpendicular(unitnorm)) .* perpendicular(unitnorm) | |
v2t = dot(v2,perpendicular(unitnorm)) .* perpendicular(unitnorm) | |
if distance(p1 , p2) <= 2*radii && dir < 0 | |
o1.opts[:velocity] = v1t + v2n | |
o2.opts[:velocity] = v2t + v1n | |
end | |
end | |
bbox = BoundingBox(box(O,video.width-2*radii,video.height-2*radii,:stroke)) | |
xbounds = (bbox[1].x,bbox[2].x) | |
ybounds = (bbox[1].y,bbox[2].y) | |
""" | |
checks for collision with wall | |
and updates velocities | |
""" | |
function collision(o1::Object,f::Int) | |
position = f==1 ? o1.start_pos : get_position(o1) | |
if !( xbounds[1] < position.x < xbounds[2]) | |
v = o1.opts[:velocity] | |
o1.opts[:velocity] = (-v[1],v[2]) | |
end | |
if !( ybounds[1] < position.y < ybounds[2]) | |
v = o1.opts[:velocity] | |
o1.opts[:velocity] = (v[1],-v[2]) | |
end | |
end | |
move() = (v,o,a,f)-> begin | |
if f == 1 | |
translate(o.opts[:velocity]...) | |
else | |
translate(get_position(o) - o.start_pos + o.opts[:velocity] ) | |
end | |
end | |
randpoint(f=1) = 2*f*video.height/2*rand()-f*video.height/2 | |
randvel() = 2*rand()-1 | |
updaterobj = Object(1:nframes , (v,o,f) -> begin | |
for (i,ball) in enumerate(balls) | |
for j in i+1:length(balls) | |
collision(ball,balls[j],f) | |
end | |
end | |
for ball in balls | |
collision(ball,f) | |
end | |
end) | |
balls = [] | |
for i in 1:nballs | |
local p = O + (randpoint(0.9),randpoint(0.9)) | |
col=Luxor.julia_red | |
if p.x<=0 && p.y <=0 | |
col = Luxor.julia_red | |
end | |
if p.x<0 && p.y > 0 | |
col = Luxor.julia_blue | |
end | |
if p.x>0 && p.y <0 | |
col = Luxor.julia_green | |
end | |
if p.x>0 && p.y > 0 | |
col = Luxor.julia_purple | |
end | |
balli = ball(col,p) | |
balli.opts[:velocity] = (randvel(),randvel()) | |
push!(balls,balli) | |
end | |
for ball in balls | |
act!(ball , Action(2:nframes,move(),keep=false)) | |
end | |
render(video,pathname="vid.mp4") | |
run(`mpv vid.mp4`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment