Advertisement
beezing

Bouncing Balls - Coders

Nov 3rd, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. -- Bouncing Balls Animation
  2. -- by @beezing
  3.  
  4. abs = math.abs
  5. sin = math.sin
  6. cos = math.cos
  7. sqrt = math.sqrt
  8. random = math.random
  9.  
  10. function rectangle(x, y, width, height)
  11.   move(x, y)
  12.   draw(x+width, y)
  13.   draw(x+width, y+height)
  14.   draw(x, y+height)
  15.   draw(x, y)
  16. end
  17.  
  18. function circle(x, y, radius)
  19.   move(x+radius, y)
  20.   for i = 0, 3.14*2 + 0.1, 0.1 do
  21.     draw(x + radius*cos(i), y + radius*sin(i))
  22.   end
  23. end
  24.  
  25. function moveBall(ball, wall)
  26.   -- calculate movement borders
  27.   minX = wall.x + ball.sz + 2
  28.   maxX = wall.width - ball.sz + 2
  29.   minY = wall.y + ball.sz + 2
  30.   maxY = wall.height - ball.sz + 2
  31.  
  32.   -- check ball for borders
  33.   if ball.x <= minX then newX = minX
  34.     elseif ball.x >= maxX then newX = maxX
  35.       else newX = ball.x end
  36.   if ball.y <= minY then newY = minY
  37.     elseif ball.y >= maxY then newY = maxY
  38.       else newY = ball.y end
  39.  
  40.   -- check ball for collision
  41.   hitX = (ball.x <= minX) or (ball.x >= maxX)
  42.   hitY = (ball.y <= minY) or (ball.y >= maxY)
  43.  
  44.   return hitX, hitY, newX, newY
  45. end
  46.  
  47. -- define wall
  48. wall = {}
  49. wall.x = 4
  50. wall.y = 24
  51. wall.width  = 312 --312 --760 --312
  52. wall.height = 740 --388 --760 --740
  53.  
  54. -- define animation constants
  55. ballCount = 5
  56. minSize   = 10
  57. maxSize   = 50
  58. maxSpeed  = 50
  59. slowRate  = 0.1
  60. infinite  = false
  61.  
  62. -- create balls
  63. balls = {}
  64. for i = 1, ballCount do
  65.   -- randomly create ball
  66.   ball = {}
  67.   ball.sz = random(minSize, maxSize)
  68.   ball.x  = random(wall.width -2*ball.sz) + ball.sz
  69.   ball.y  = random(wall.height-2*ball.sz) + ball.sz
  70.   ball.dx = random(-maxSpeed, maxSpeed)
  71.   ball.dy = random(-maxSpeed, maxSpeed)
  72.   ball.r  = random(192)
  73.   ball.g  = random(192)
  74.   ball.b  = random(192)
  75.   balls[i] = ball
  76. end
  77.  
  78. -- animate bouncing balls
  79. stopAll = 1
  80. while stopAll > 0 do
  81.   disableScreenRefresh()
  82.  
  83.   -- draw wall
  84.   clear()
  85.   setcolor(0,0,0)
  86.   rectangle(wall.x, wall.y, wall.width, wall.height)
  87.  
  88.   -- animate balls
  89.   for i = 1, #balls do
  90.     -- draw ball at its current position
  91.     setcolor(balls[i].r, balls[i].g, balls[i].b)
  92.     circle(balls[i].x, balls[i].y, balls[i].sz)
  93.  
  94.     -- move ball to its next position based on its speed
  95.     balls[i].x = balls[i].x + balls[i].dx
  96.     balls[i].y = balls[i].y + balls[i].dy
  97.  
  98.     -- check ball's movement for collision with the wall
  99.     hX, hY, balls[i].x, balls[i].y = moveBall(balls[i], wall)
  100.  
  101.     -- bounce ball if it hits the wall
  102.     if hX then balls[i].dx = -balls[i].dx end
  103.     if hY then balls[i].dy = -balls[i].dy end
  104.   end
  105.  
  106.   if not infinite then
  107.     -- slow down animation
  108.     stopAll = 0
  109.     for i = 1, #balls do
  110.       -- calculate deacceleration
  111.       a  = balls[i].dy * balls[i].dy
  112.       b  = balls[i].dx * balls[i].dx
  113.       c  = sqrt(a+b)
  114.       c_ = c-slowRate
  115.       b_ = abs(balls[i].dx) - abs((balls[i].dx * c_)/c)
  116.       a_ = abs(balls[i].dy) - abs((balls[i].dy * c_)/c)
  117.  
  118.       -- deaccelerate ball speed
  119.       if balls[i].dx < 0 then
  120.         balls[i].dx = balls[i].dx + b_
  121.       elseif balls[i].dx > 0 then
  122.         balls[i].dx = balls[i].dx - b_ end
  123.       if balls[i].dy < 0 then
  124.         balls[i].dy = balls[i].dy + a_
  125.       elseif balls[i].dy > 0 then
  126.         balls[i].dy = balls[i].dy - a_ end
  127.  
  128.       -- check for remaining movement based on velocity
  129.       if abs(c_) >= slowRate then
  130.         stopAll = stopAll + abs(c_)
  131.       end
  132.     end
  133.     -- stop if sum of velocity is less then slowing rate
  134.     if stopAll < slowRate then stopAll = 0 end
  135.   end
  136.  
  137.   enableScreenRefresh()
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement