Advertisement
DjSapsan

gravity

Jul 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. if arg[#arg] == "-debug" then require("mobdebug").start() end
  2.  
  3. function love.load()
  4.   love.window.setMode(1900,1000)
  5.  
  6.   G = 1
  7.  
  8.   planetList = {}
  9.  
  10.   worldSize = {w=1900,h=1000}
  11.  
  12.   tool = {pause=false, active=false,x=0,y=0,dx=0,dy=0,mass=10,lastID = nil}
  13.   tool.radius=math.sqrt(tool.mass/math.pi)
  14.  
  15.   love.graphics.clear = function() end
  16.  
  17. end
  18.  
  19. function newPlanet(x,y,m,vx,vy)
  20.   local p = #planetList+1
  21.   planetList[p] = {}
  22.   planetList[p].ID = p
  23.   planetList[p].x = x
  24.   planetList[p].y = y
  25.   planetList[p].m = m
  26.   planetList[p].radius = math.sqrt(m/math.pi)
  27.   planetList[p].velocityX = vx or 0
  28.   planetList[p].velocityY = vy or 0
  29.   tool.lastID = p
  30. end
  31.  
  32. function collision(me,target)
  33.   me.m = me.m + target.m
  34.   me.velocityX = me.velocityX + target.velocityX*target.m/me.m
  35.   me.velocityY = me.velocityY + target.velocityY*target.m/me.m
  36.   me.radius = math.sqrt(me.m/math.pi)
  37.   planetList[target.ID] = nil
  38. end
  39.  
  40. function updatePlanet(me)
  41.   dt = 0.001
  42.   for _,target in pairs(planetList) do
  43.     if target.ID ~= me.ID then
  44.       local dx = target.x - me.x
  45.       local dy = target.y - me.y
  46.       local distance = math.sqrt((dx)^2+(dy)^2)
  47.       local force = (G*(me.m * target.m)/distance^2)*dt
  48.       me.velocityX = me.velocityX + dx*force/me.m
  49.       me.velocityY = me.velocityY + dy*force/me.m
  50.       if distance < me.radius then collision(me,target) end
  51.     end
  52.   end
  53.   me.x = me.x + me.velocityX
  54.   me.y = me.y + me.velocityY
  55. end
  56.  
  57. function addRandomPlanets(n)
  58.   for i=1,n do
  59.     newPlanet(math.random(0,worldSize.w),math.random(0,worldSize.h),math.random(5,10+n),math.random(-2,2),math.random(-2,2))
  60.   end
  61. end
  62.  
  63. function updateAllPlanets()
  64.   for _,planet in pairs(planetList) do
  65.     updatePlanet(planet)
  66.   end
  67. end
  68.  
  69. function drawPlanet(me)
  70.   love.graphics.setColor(0,1,0)
  71.   love.graphics.circle("fill",me.x,me.y,me.radius)
  72. end
  73.  
  74. function drawAllPlanets()
  75.   for _,planet in pairs(planetList) do
  76.     drawPlanet(planet)
  77.   end
  78. end
  79.  
  80.  
  81. function love.mousepressed(x,y,b)
  82.   if b==1 then
  83.     tool.active = true
  84.     tool.x=x
  85.     tool.y=y
  86.     tool.dx=x
  87.     tool.dy=y
  88.   end
  89.  
  90.   if b==2 then
  91.     planetList = nil
  92.     planetList = {}
  93.     love.graphics.setCanvas(canvas)
  94.       love.graphics.clear()
  95.     love.graphics.setCanvas()
  96.   end
  97.  
  98. end
  99.  
  100. function love.keypressed(key)
  101.  
  102.   if key=="1" then
  103.     tool.mass = 10
  104.   end
  105.  
  106.   if key=="r" then
  107.     addRandomPlanets(100)
  108.   end
  109.  
  110.   if key=="space" then
  111.     tool.pause = not tool.pause
  112.   end
  113.  
  114.   if key=="z" and love.keyboard.isScancodeDown("lctrl") then
  115.     planetList[tool.lastID] = nil
  116.   end
  117. end
  118.  
  119. function love.mousemoved(x,y,dx,dy)
  120.   if tool.active then
  121.     tool.dx=x
  122.     tool.dy=y
  123.   end
  124. end
  125.  
  126. function love.wheelmoved(x,y)
  127.   if love.keyboard.isScancodeDown("lctrl") then y = y*20 end
  128.   tool.mass=tool.mass+y*3
  129.   if tool.mass<5 then
  130.     tool.mass = 5
  131.   end
  132.   tool.radius=math.sqrt(tool.mass/math.pi)
  133. end
  134.  
  135. function love.mousereleased(x,y,b)
  136.   if b==1 then
  137.     tool.dx=x
  138.     tool.dy=y
  139.     tool.active=false
  140.     newPlanet(tool.x,tool.y,tool.mass,(tool.x-tool.dx)/200,(tool.y-tool.dy)/200)
  141.   end
  142. end
  143.  
  144. function love.update()
  145.   if not tool.pause then
  146.     updateAllPlanets()
  147.   end
  148. end
  149.  
  150. function love.draw()
  151.   love.graphics.setColor(0,0,0,0.1)
  152.   love.graphics.rectangle("fill",0,0,worldSize.w,worldSize.h)
  153.  
  154.   drawAllPlanets()
  155.  
  156.   love.graphics.setColor(1,1,1,0.2)
  157.   love.graphics.circle("line",love.mouse.getX(),love.mouse.getY(),tool.radius)
  158.  
  159.   if tool.active then
  160.     love.graphics.setColor(0,1,0,0.7)
  161.     love.graphics.circle("line",tool.x,tool.y,tool.radius)
  162.     love.graphics.line(tool.x,tool.y,tool.dx,tool.dy)
  163.   end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement