Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local nRuntime=0
- local nBounces,rot=0
- local tObjects={}
- local nSpeed=50
- local fps=0
- local tFrames={}
- local g=6.674*10^-11 -- in (m/kg)^2
- local lastKey="none"
- local lastUni=0
- local function returnlist(...)
- return ...
- end
- function love.keypressed(key,unicode)
- lastKey=key
- lastUni=unicode
- if key=="kp-" and nSpeed>5 then
- nSpeed=nSpeed-5
- elseif key=="kp+" and nSpeed<100 then
- nSpeed=nSpeed+5
- elseif key=="insert" then
- table.insert(tObjects,{
- pos={
- x=math.random(1,love.graphics.getWidth()),
- y=math.random(1,love.graphics.getHeight())
- },
- look={
- radius=5,
- color={
- math.random(0,255),
- math.random(0,255),
- math.random(0,255)
- }
- },
- vel={
- x=0,
- y=0
- },
- bounce=0.5,
- bounces=0,
- grav=10^math.random(10,11),
- trail={}
- })
- elseif key=="delete" then
- tObjects[#tObjects]=nil
- end
- end
- function love.update(nSecs)
- table.insert(tFrames,1,nSecs)
- tFrames[11]=nil
- local totTime=0
- for i=1,#tFrames do
- totTime=totTime+tFrames[i]
- end
- fps=#tFrames/totTime
- for run=1,nSpeed do
- for k,v in pairs(tObjects) do
- if run%10==1 then
- table.insert(v.trail,1,{x=v.pos.x,y=v.pos.y})
- v.trail[100]=nil
- end
- v.vel.newx=v.vel.x
- v.vel.newy=v.vel.y
- v.pos.x=v.pos.x+v.vel.newx*nSecs
- v.pos.y=v.pos.y+v.vel.newy*nSecs
- local bBounced=false
- for a,b in pairs(tObjects) do
- if b~=v and v.grav and b.grav then
- local dx,dy=b.pos.x-v.pos.x,b.pos.y-v.pos.y
- local nDist=(dx^2+dy^2)^0.5
- local accel=((g*v.grav*b.grav)/(nDist))/v.grav
- local nAng=math.atan2(dy,dx)
- if nDist>0 then
- v.vel.newx=v.vel.newx+accel*math.cos(nAng)*nSecs
- v.vel.newy=v.vel.newy+accel*math.sin(nAng)*nSecs
- end
- end
- end
- --[[for a,b in pairs(tObjects) do
- if b~=v then
- local dx,dy=b.pos.x-v.pos.x,b.pos.y-v.pos.y
- local dvx,dvy=v.vel.x-b.vel.x,v.vel.y-b.vel.y
- local nDist=(dx^2+dy^2)^0.5
- relVel=(dvx^2+dvy^2)^0.5
- local rD=(math.atan2(dy,dx)-math.atan2(dvy,dvx))%(math.pi*2)
- rot=k==1 and rD or rot
- if nDist<=v.look.radius+b.look.radius and (rD<math.pi/2 or rD>math.pi*3/2) then
- local nBounce=v.bounce*b.bounce
- v.vel.newx=(v.grav*v.vel.newx+b.grav*b.vel.x)*nBounce/(v.grav+b.grav)
- v.vel.newy=(v.grav*v.vel.newy+b.grav*b.vel.y)*nBounce/(v.grav+b.grav)
- bBounced=true
- v.bounces=v.bounces+1
- end
- end
- end]]
- end
- for k,v in pairs(tObjects) do
- v.vel.x=v.vel.newx*(((v.pos.x-v.look.radius<=0 and v.vel.x<0) or (v.pos.x+v.look.radius>=love.graphics.getWidth() and v.vel.x>0)) and -1*v.bounce or 1)
- v.vel.y=v.vel.newy*(((v.pos.y-v.look.radius<=0 and v.vel.y<0) or (v.pos.y+v.look.radius>=love.graphics.getHeight() and v.vel.y>0)) and -1*v.bounce or 1)
- end
- end
- end
- function love.draw()
- for k,v in pairs(tObjects) do
- if type(v.look.color)=="table" then
- love.graphics.setColor(unpack(v.look.color))
- else
- love.graphics.setColor(returnlist(255,255,255))
- end
- for i=#v.trail,1,-1 do
- love.graphics.circle("fill",v.trail[i].x,v.trail[i].y,v.look.radius*i/#v.trail,v.look.radius*math.pi)
- end
- love.graphics.circle("fill",v.pos.x,v.pos.y,v.look.radius,v.look.radius*math.pi)
- --love.graphics.line(v.pos.x,v.pos.y,v.pos.x+v.vel.x*2,v.pos.y+v.vel.y*2)
- end
- love.graphics.setColor(255-math.ceil(200*fps/60),math.ceil(200*fps/60)+55,0)
- love.graphics.print("FPS: "..math.floor(fps).."/"..60,0,0)
- love.graphics.setColor(0,math.ceil(255*nSpeed/100),255-math.ceil(255*nSpeed/100))
- love.graphics.print("Emulation Speed: "..nSpeed.."/"..100,0,12)
- love.graphics.setColor(255,255,255)
- love.graphics.print("Objects: "..#tObjects,0,24)
- love.graphics.print("Key Name: "..lastKey,0,36)
- love.graphics.print("Key Unicode: "..lastUni,0,48)
- end
- tObjects[1]={
- pos={
- x=math.random(1,love.graphics.getWidth()),
- y=math.random(1,love.graphics.getHeight())
- },
- look={
- radius=10,
- color={
- 0,
- 0,
- 0
- }
- },
- vel={
- x=0,
- y=0
- },
- bounce=0.8,
- bounces=0,
- grav=10^12,
- trail={}
- }
Advertisement
Add Comment
Please, Sign In to add comment