Guest User

Untitled

a guest
Sep 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. function love.load()
  2. printx = 0 --position to be drawn on when the mouse is pressed
  3. printy = 0 --position to be drawn on when the mouse is pressed
  4.  
  5. love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  6. world = love.physics.newWorld(0, 9.81*64, true) --The world the everything exists in. Horizontal gravity=0. Bertical gravity=9.81. True says that the world is allowed to sleep.
  7.  
  8. objects = {} -- table to hold all our physical objects
  9.  
  10. --let's create the ground
  11. objects.lines = {}
  12. objects.ground = {}
  13. objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2)
  14. objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
  15. objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body
  16.  
  17. --let's create a ball
  18. objects.ball = {}
  19. objects.ball.body = love.physics.newBody(world, 650/2, 600, "dynamic") --Determines where the object will start. In the center of the world and dynamic(moves around)
  20. objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20
  21. objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
  22.  
  23.  
  24. --initial graphics setup
  25. love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
  26. love.window.setMode(650, 650) --set the window dimensions to 650 by 650
  27. end
  28.  
  29.  
  30. function love.update(dt)
  31. world:update(dt) --this puts the world into motion
  32.  
  33. if love.mouse.isDown(1) then
  34. local printx = love.mouse.getX()
  35. local printy = love.mouse.getY()
  36. line = {}
  37. line.body = love.physics.newBody(world, printx, printy, "static")
  38. line.shape = love.physics.newRectangleShape(0, 0, 5, 5)
  39. line.fixture = love.physics.newFixture(line.body, line.shape, 5) -- A higher density gives it more mass.
  40. table.insert(objects.lines, line)
  41. love.graphics.setColor(50,50,50)
  42. love.graphics.polygon("fill", line.body:getWorldPoints(line.shape:getPoints()))
  43. end
  44.  
  45. end
  46.  
  47. function love.draw()
  48. love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground
  49. love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
  50.  
  51. love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
  52. love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())
  53.  
  54. for _, block in pairs(objects.lines) do
  55. love.graphics.polygon("fill", block.body:getWorldPoints(block.shape:getPoints()))
  56. end
  57.  
  58. end
  59.  
  60. if not love.mouse.isDown(1) then
  61. oldx = nil
  62. oldy = nil
  63. end
  64.  
  65.  
  66. --draw lines when the mouse is down
  67. if love.mouse.isDown(1) then
  68. local printx = love.mouse.getX() + Camera.x --x coordinate of the mouse
  69. local printy = love.mouse.getY() + Camera.y --y coordinate of the mouse
  70.  
  71. if oldx ~= nil then
  72. line = {}
  73. line.x1 = oldx
  74. line.x2 = printx
  75. line.y1 = oldy
  76. line.y2 = printy
  77. line.body = love.physics.newBody(world, 0, 0, "static")
  78. line.shape = love.physics.newEdgeShape(printx, printy, oldx, oldy)
  79. line.fixture = love.physics.newFixture(line.body, line.shape, 5)
  80. table.insert(objects.lines, line)
  81. end
  82.  
  83. oldx = printx
  84. oldy = printy
  85.  
  86. end
  87.  
  88. for _, line in pairs(objects.lines) do
  89. love.graphics.line( line.x1 - Camera.x, line.y1 - Camera.y, line.x2 - Camera.x, line.y2 - Camera.y) --:getWorldPoints(block.shape:getPoints()))
  90. end
Add Comment
Please, Sign In to add comment