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.
Showing posts with label ball. Show all posts
Showing posts with label ball. Show all posts
Thursday, January 1, 2009
A bouncing ball
To calculate the formula for a bouncing ball, combine the formula for trajectory with a formula to rotate the turtle every time it hits the ground.
TO BOUNCE :A :V :CE
make "sx 0 make "T 0
WHILE [3 = 3] [
MAKE "T :T + 0.01
SETX (:V * :T * COS :A) + :SX
SETY :V * :T * (SIN :A) - 4.91 * :T*:T
IF YCOR < 0 [
MAKE "T 0
MAKE "V :V * :CE
MAKE "SX XCOR SETY 0
IF :V < 1 [STOP]
]
]
END
Where :A is the starting angle, :V is the starting trajectory, and :CE is the coefficient of elasticity. What is that? It's the thing you multiply velocity by to get the new velocity. CE is the reason when you bounce a ball, it doesn't go straight back up to your hand.
I recomend putting :V at a maximum of 100 and :CE about .9 or .5 at the lowest.
TO BOUNCE :A :V :CE
make "sx 0 make "T 0
WHILE [3 = 3] [
MAKE "T :T + 0.01
SETX (:V * :T * COS :A) + :SX
SETY :V * :T * (SIN :A) - 4.91 * :T*:T
IF YCOR < 0 [
MAKE "T 0
MAKE "V :V * :CE
MAKE "SX XCOR SETY 0
IF :V < 1 [STOP]
]
]
END
Where :A is the starting angle, :V is the starting trajectory, and :CE is the coefficient of elasticity. What is that? It's the thing you multiply velocity by to get the new velocity. CE is the reason when you bounce a ball, it doesn't go straight back up to your hand.
I recomend putting :V at a maximum of 100 and :CE about .9 or .5 at the lowest.
Subscribe to:
Posts (Atom)