Skip to content

Instantly share code, notes, and snippets.

# Sov-trotter/balls-collision_javis.jl

Created June 26, 2021 06:56
Show Gist options
• Save Sov-trotter/abc4b8bafc715814dafdd58c8f65bba7 to your computer and use it in GitHub Desktop.
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
 # const panewidth = video.width/2 # const paneheight = video.height/2 # asqu(args...; do_action=:stroke) = box(Point(0,0), 50, 50, 0,do_action) # # using Luxor, Colors, Combinatorics # mutable struct Ball # position::Point # velocity::Point # color::Color # ballradius::Float64 # end # layer = Javis.Layer() # function collisioncheck(balls) # for ballpair in combinations(1:length(balls), 2) # balla, ballb = balls[ballpair[1]], balls[ballpair[2]] # if intersection2circles(balla.position, balla.ballradius, ballb.position, ballb.ballradius) > 0.01 # collision = ballb.position - balla.position # distance = Luxor.distance(ballb.position, balla.position) # # Get the components of the velocity vectors which are parallel to the collision. # # The perpendicular component remains the same for both # collision = (collision / distance) # aci = dotproduct(balla.velocity, collision) # bci = dotproduct(ballb.velocity, collision) # # new velocities using the 1-dimensional elastic collision equations # # masses are the same # acf = bci # bcf = aci # # replace the velocity components # balls[ballpair[1]].velocity += (acf - aci) * collision # balls[ballpair[2]].velocity += (bcf - bci) * collision # end # end # return balls # end # function update(ball, balls) # (video, object, action, rel_frame) -> # _update(video, object, action, rel_frame, ball, balls) # end # function _update(video, object, action, rel_frame, ball, balls) # translate(O) # # balls = collisioncheck(balls) # # on the left # for ball in balls # ball.position = ball.position += ball.velocity # if (ball.position.x <= (-panewidth + ball.ballradius)) # ball.position = Point(-panewidth + ball.ballradius, ball.position.y) # ball.velocity = Point(-ball.velocity.x, ball.velocity.y) # end # if (ball.position.x >= (panewidth - ball.ballradius)) # ball.position = Point(panewidth - ball.ballradius, ball.position.y) # ball.velocity = Point(-ball.velocity.x, ball.velocity.y) # end # if (ball.position.y <= (-paneheight + ball.ballradius)) # ball.position = Point(ball.position.x, -panewidth + ball.ballradius) # ball.velocity = Point(ball.velocity.x, -ball.velocity.y) # end # if (ball.position.y >= (paneheight - ball.ballradius)) # ball.position = Point(ball.position.x, panewidth - ball.ballradius) # ball.velocity = Point(ball.velocity.x, -ball.velocity.y) # end # end # translate(ball.position) # end # function Ballcon(position::Point, velocity::Point, color::Color, ballradius::Number) # Ball(position, velocity, color, ballradius) # sethue(color) # box(position, ballradius,ballradius, :fill) # return position # end # function main() # ballradius = 14 # balls = [ # # set position and initial velocity vectors # Ball(O, Point(-5, 0), colorant"blue", ballradius), # # Ball(Point(15, 2), Point(-3, 0), colorant"forestgreen", ballradius), # # Ball(O-15, Point(2, -5), colorant"mediumorchid3", ballradius) # ] # Background(1:300, ground) # red_ball = Object(1:300, (args...)-> Ballcon(O, Point(-5, 0), colorant"blue", ballradius), O) # act!(red_ball, Action(1:300, update(balls[1], balls))) # # gree_ball = Object(1:300, (args...)-> Ballcon(O, Point(-3, 4), colorant"forestgreen", ballradius), O) # # act!(gree_ball, Action(1:300, update(balls[2], balls))) # # blue_ball = Object(1:300, (args...)-> Ballcon(O, Point(2, -5), colorant"mediumorchid3", ballradius), O) # # act!(blue_ball, Action(1:300, update(balls[3], balls))) # render(video, pathname = "julia_coll.gif") # end # main()
to join this conversation on GitHub. Already have an account? Sign in to comment