KaoSDlanor

Physics.love

Jun 7th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. local nRuntime=0
  2. local nBounces,rot=0
  3. local tObjects={}
  4. local nSpeed=50
  5. local fps=0
  6. local tFrames={}
  7. local g=6.674*10^-11 -- in (m/kg)^2
  8. local lastKey="none"
  9. local lastUni=0
  10.  
  11. local function returnlist(...)
  12.     return ...
  13. end
  14.  
  15. function love.keypressed(key,unicode)
  16.     lastKey=key
  17.     lastUni=unicode
  18.     if key=="kp-" and nSpeed>5 then
  19.         nSpeed=nSpeed-5
  20.     elseif key=="kp+" and nSpeed<100 then
  21.         nSpeed=nSpeed+5
  22.     elseif key=="insert" then
  23.         table.insert(tObjects,{
  24.             pos={
  25.                 x=math.random(1,love.graphics.getWidth()),
  26.                 y=math.random(1,love.graphics.getHeight())
  27.             },
  28.             look={
  29.                 radius=5,
  30.                 color={
  31.                     math.random(0,255),
  32.                     math.random(0,255),
  33.                     math.random(0,255)
  34.                 }
  35.             },
  36.             vel={
  37.                 x=0,
  38.                 y=0
  39.             },
  40.             bounce=0.5,
  41.             bounces=0,
  42.             grav=10^math.random(10,11),
  43.             trail={}
  44.         })
  45.     elseif key=="delete" then
  46.         tObjects[#tObjects]=nil
  47.     end
  48. end
  49.  
  50. function love.update(nSecs)
  51.     table.insert(tFrames,1,nSecs)
  52.     tFrames[11]=nil
  53.     local totTime=0
  54.     for i=1,#tFrames do
  55.         totTime=totTime+tFrames[i]
  56.     end
  57.     fps=#tFrames/totTime
  58.     for run=1,nSpeed do
  59.         for k,v in pairs(tObjects) do
  60.             if run%10==1 then
  61.                 table.insert(v.trail,1,{x=v.pos.x,y=v.pos.y})
  62.                 v.trail[100]=nil
  63.             end
  64.             v.vel.newx=v.vel.x
  65.             v.vel.newy=v.vel.y
  66.             v.pos.x=v.pos.x+v.vel.newx*nSecs
  67.             v.pos.y=v.pos.y+v.vel.newy*nSecs
  68.             local bBounced=false
  69.             for a,b in pairs(tObjects) do
  70.                 if b~=v and v.grav and b.grav then
  71.                     local dx,dy=b.pos.x-v.pos.x,b.pos.y-v.pos.y
  72.                     local nDist=(dx^2+dy^2)^0.5
  73.                     local accel=((g*v.grav*b.grav)/(nDist))/v.grav
  74.                     local nAng=math.atan2(dy,dx)
  75.                     if nDist>0 then
  76.                         v.vel.newx=v.vel.newx+accel*math.cos(nAng)*nSecs
  77.                         v.vel.newy=v.vel.newy+accel*math.sin(nAng)*nSecs
  78.                     end
  79.                 end
  80.             end
  81.             --[[for a,b in pairs(tObjects) do
  82.                 if b~=v then
  83.                     local dx,dy=b.pos.x-v.pos.x,b.pos.y-v.pos.y
  84.                     local dvx,dvy=v.vel.x-b.vel.x,v.vel.y-b.vel.y
  85.                     local nDist=(dx^2+dy^2)^0.5
  86.                     relVel=(dvx^2+dvy^2)^0.5
  87.                     local rD=(math.atan2(dy,dx)-math.atan2(dvy,dvx))%(math.pi*2)
  88.                     rot=k==1 and rD or rot
  89.                     if nDist<=v.look.radius+b.look.radius and (rD<math.pi/2 or rD>math.pi*3/2) then
  90.                         local nBounce=v.bounce*b.bounce
  91.                         v.vel.newx=(v.grav*v.vel.newx+b.grav*b.vel.x)*nBounce/(v.grav+b.grav)
  92.                         v.vel.newy=(v.grav*v.vel.newy+b.grav*b.vel.y)*nBounce/(v.grav+b.grav)
  93.                         bBounced=true
  94.                         v.bounces=v.bounces+1
  95.                     end
  96.                 end
  97.             end]]
  98.         end
  99.         for k,v in pairs(tObjects) do
  100.             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)
  101.             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)
  102.         end
  103.     end
  104. end
  105.  
  106. function love.draw()
  107.     for k,v in pairs(tObjects) do
  108.         if type(v.look.color)=="table" then
  109.             love.graphics.setColor(unpack(v.look.color))
  110.         else
  111.             love.graphics.setColor(returnlist(255,255,255))
  112.         end
  113.         for i=#v.trail,1,-1 do
  114.             love.graphics.circle("fill",v.trail[i].x,v.trail[i].y,v.look.radius*i/#v.trail,v.look.radius*math.pi)
  115.         end
  116.         love.graphics.circle("fill",v.pos.x,v.pos.y,v.look.radius,v.look.radius*math.pi)
  117.         --love.graphics.line(v.pos.x,v.pos.y,v.pos.x+v.vel.x*2,v.pos.y+v.vel.y*2)
  118.     end
  119.     love.graphics.setColor(255-math.ceil(200*fps/60),math.ceil(200*fps/60)+55,0)
  120.     love.graphics.print("FPS: "..math.floor(fps).."/"..60,0,0)
  121.     love.graphics.setColor(0,math.ceil(255*nSpeed/100),255-math.ceil(255*nSpeed/100))
  122.     love.graphics.print("Emulation Speed: "..nSpeed.."/"..100,0,12)
  123.     love.graphics.setColor(255,255,255)
  124.     love.graphics.print("Objects: "..#tObjects,0,24)
  125.     love.graphics.print("Key Name: "..lastKey,0,36)
  126.     love.graphics.print("Key Unicode: "..lastUni,0,48)
  127. end
  128. tObjects[1]={
  129.             pos={
  130.                 x=math.random(1,love.graphics.getWidth()),
  131.                 y=math.random(1,love.graphics.getHeight())
  132.             },
  133.             look={
  134.                 radius=10,
  135.                 color={
  136.                     0,
  137.                     0,
  138.                     0
  139.                 }
  140.             },
  141.             vel={
  142.                 x=0,
  143.                 y=0
  144.             },
  145.             bounce=0.8,
  146.             bounces=0,
  147.             grav=10^12,
  148.             trail={}
  149.         }
Advertisement
Add Comment
Please, Sign In to add comment