Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if arg[#arg] == "-debug" then require("mobdebug").start() end
- function love.load()
- love.window.setMode(1900,1000)
- G = 1
- planetList = {}
- worldSize = {w=1900,h=1000}
- tool = {pause=false, active=false,x=0,y=0,dx=0,dy=0,mass=10,lastID = nil}
- tool.radius=math.sqrt(tool.mass/math.pi)
- love.graphics.clear = function() end
- end
- function newPlanet(x,y,m,vx,vy)
- local p = #planetList+1
- planetList[p] = {}
- planetList[p].ID = p
- planetList[p].x = x
- planetList[p].y = y
- planetList[p].m = m
- planetList[p].radius = math.sqrt(m/math.pi)
- planetList[p].velocityX = vx or 0
- planetList[p].velocityY = vy or 0
- tool.lastID = p
- end
- function collision(me,target)
- me.m = me.m + target.m
- me.velocityX = me.velocityX + target.velocityX*target.m/me.m
- me.velocityY = me.velocityY + target.velocityY*target.m/me.m
- me.radius = math.sqrt(me.m/math.pi)
- planetList[target.ID] = nil
- end
- function updatePlanet(me)
- dt = 0.001
- for _,target in pairs(planetList) do
- if target.ID ~= me.ID then
- local dx = target.x - me.x
- local dy = target.y - me.y
- local distance = math.sqrt((dx)^2+(dy)^2)
- local force = (G*(me.m * target.m)/distance^2)*dt
- me.velocityX = me.velocityX + dx*force/me.m
- me.velocityY = me.velocityY + dy*force/me.m
- if distance < me.radius then collision(me,target) end
- end
- end
- me.x = me.x + me.velocityX
- me.y = me.y + me.velocityY
- end
- function addRandomPlanets(n)
- for i=1,n do
- newPlanet(math.random(0,worldSize.w),math.random(0,worldSize.h),math.random(5,10+n),math.random(-2,2),math.random(-2,2))
- end
- end
- function updateAllPlanets()
- for _,planet in pairs(planetList) do
- updatePlanet(planet)
- end
- end
- function drawPlanet(me)
- love.graphics.setColor(0,1,0)
- love.graphics.circle("fill",me.x,me.y,me.radius)
- end
- function drawAllPlanets()
- for _,planet in pairs(planetList) do
- drawPlanet(planet)
- end
- end
- function love.mousepressed(x,y,b)
- if b==1 then
- tool.active = true
- tool.x=x
- tool.y=y
- tool.dx=x
- tool.dy=y
- end
- if b==2 then
- planetList = nil
- planetList = {}
- love.graphics.setCanvas(canvas)
- love.graphics.clear()
- love.graphics.setCanvas()
- end
- end
- function love.keypressed(key)
- if key=="1" then
- tool.mass = 10
- end
- if key=="r" then
- addRandomPlanets(100)
- end
- if key=="space" then
- tool.pause = not tool.pause
- end
- if key=="z" and love.keyboard.isScancodeDown("lctrl") then
- planetList[tool.lastID] = nil
- end
- end
- function love.mousemoved(x,y,dx,dy)
- if tool.active then
- tool.dx=x
- tool.dy=y
- end
- end
- function love.wheelmoved(x,y)
- if love.keyboard.isScancodeDown("lctrl") then y = y*20 end
- tool.mass=tool.mass+y*3
- if tool.mass<5 then
- tool.mass = 5
- end
- tool.radius=math.sqrt(tool.mass/math.pi)
- end
- function love.mousereleased(x,y,b)
- if b==1 then
- tool.dx=x
- tool.dy=y
- tool.active=false
- newPlanet(tool.x,tool.y,tool.mass,(tool.x-tool.dx)/200,(tool.y-tool.dy)/200)
- end
- end
- function love.update()
- if not tool.pause then
- updateAllPlanets()
- end
- end
- function love.draw()
- love.graphics.setColor(0,0,0,0.1)
- love.graphics.rectangle("fill",0,0,worldSize.w,worldSize.h)
- drawAllPlanets()
- love.graphics.setColor(1,1,1,0.2)
- love.graphics.circle("line",love.mouse.getX(),love.mouse.getY(),tool.radius)
- if tool.active then
- love.graphics.setColor(0,1,0,0.7)
- love.graphics.circle("line",tool.x,tool.y,tool.radius)
- love.graphics.line(tool.x,tool.y,tool.dx,tool.dy)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement