Advertisement
br1wr2el3

Code 08b Physics Ballbounce

Apr 20th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. -- Code 08b - Physics BallBounce
  2. -- Will upload to Codea
  3.  
  4. -- Bruce Elliott
  5. -- April 2013
  6.  
  7. -- Ball will fall in the direction of the iPad tilt
  8. -- when it hits a wall it will bounce
  9.  
  10. -- Fix the orientation and use fullscreen
  11. supportedOrientations(PORTRAIT)
  12. displayMode(FULLSCREEN)
  13.  
  14. -- How much bounce there is change to get different bounce
  15. bouncy = .5
  16.  
  17. -- Create a physics edge
  18. function createWall(x1, y1, x2, y2)
  19. local temp = physics.body(EDGE, vec2(x1,y1), vec2(x2,y2))
  20. temp.type = STATIC
  21. temp.restitution = bouncy
  22. return temp
  23. end
  24.  
  25. function setup()
  26. -- Create circle named ball
  27.  
  28. ball = physics.body(CIRCLE, 28)
  29. ball.x = WIDTH/2
  30. ball.y = HEIGHT/2
  31. ball.restitution = bouncy
  32. ball.sleepingAllowed = false
  33. end
  34.  
  35. function draw()
  36.  
  37. background(40,40,50)
  38. fill(255)
  39.  
  40. strokeWidth(5)
  41. stroke(255)
  42.  
  43. -- Build physics walls
  44. createWall(50,50,50,HEIGHT -50)
  45. createWall(50,50,WIDTH -50,50)
  46. createWall(WIDTH -50,50,WIDTH -50,HEIGHT -50)
  47. createWall(WIDTH -50,HEIGHT-50,50,HEIGHT-50)
  48.  
  49. -- Draw visible walls
  50. line(50,50,50,HEIGHT-50)
  51. line(50,50,WIDTH-50,50)
  52. line(WIDTH-50,50,WIDTH-50,HEIGHT-50)
  53. line(WIDTH-50,HEIGHT-50,50,HEIGHT-50)
  54.  
  55. -- Gravity set to device gravity
  56. physics.gravity(Gravity)
  57.  
  58. -- Draw visible ball uses physics ball values
  59. ellipse(ball.x, ball.y, ball.radius*2)
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement