This is code for a ball bouncing without gravity, but with walls all around it. Currently, I have not figured out an efficient way to get the ball to bounce off any wall. It has to be just straight lines that are preprogrammed; no cool zig zag bounces. Also, this equation only accounts for one ball. I plan on adding more balls later, but only have one right now.
to pooltable :V :A :CS
Make "h 0 Make "l 0 MAKE "y 0 Make "p 0 seth 0 rt :a
while [:V>.001] [MAKE "l xcor MAKE "h ycor fd :V IFelse ycor>:h [Make "y ycor-:h] [Make "y :h-ycor] Ifelse xcor>:l [Make "z xcor-:l] [Make "z :l-xcor]
while [ycor>300] [IF Heading>180 [lt 180-(arctan (:y) (:z))*2] IF Heading<180 [rt 180-(arctan (:y) (:z))*2] fd :V*:CS Make "V :V*:CS]
while [ycor<-300] [IF Heading>180 [rt 180-(arctan :y :z)*2] IF Heading<180 [lt 180-(arctan :y :z)*2] FD :V*:CS Make "V :V*:CS]
while [xcor>450] [If Heading>90 [rt 180-(arctan :z :y)*2] If heading<90 [lt 180-(arctan :z :y)*2] fd :V*:CS Make "V :V*:CS]
while [xcor<-450] [ IF heading<270>270 [rt 180-((arctan :z :y)*2) fd 1] fd :V*:CS Make "V :V*:CS ]]
end
Where :v is the starting velocity, :a is the starting angle, and :CS is coefficient of elasticity at which it will bounce off walls. I recomend putting the coefficient of elasticity at about .9 or .6 at the lowest. Keep velocity
very low. Preferably at only 1. Pick your own angle.