Advertisement
Guest User

main.lua

a guest
Jan 31st, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | None | 0 0
  1. function love.load()
  2.     t = 0
  3.    
  4.     love.window.setFullscreen(true, "desktop")
  5.     gravityX = 0
  6.     gravityY = 400
  7.    
  8.     points = {}
  9.     springs = {}
  10.     totalPoints = 0
  11.     totalSprings = 0
  12.     globalStiffness = 550
  13.    
  14.     --Square
  15.     p1 = createPoint(100,100,1,1,0,0)
  16.     p2 = createPoint(300,100,1,1,0,0)
  17.     p3 = createPoint(100,300,1,1,0,0)
  18.     p4 = createPoint(300,300,1,1,0,0)
  19.     s1 = createSpring(p1,p2,200,globalStiffness,1,0,1,0)
  20.     s2 = createSpring(p1,p3,200,globalStiffness,1,0,1,0)
  21.     s3 = createSpring(p2,p4,200,globalStiffness,1,0,1,0)
  22.     s4 = createSpring(p3,p4,200,globalStiffness,1,0,1,0)
  23.     c1 = createSpring(p1,p4,(math.sqrt(2)*200),globalStiffness,1,0,0,1)
  24.     c2 = createSpring(p2,p3,(math.sqrt(2)*200),globalStiffness,1,0,0,1)
  25.    
  26.     --Double Pendulum
  27.     g1 = createPoint(love.graphics.getWidth()/2,100,1)
  28.     g2 = createPoint((love.graphics.getWidth()/2)+150,100,1)
  29.     g3 = createPoint((love.graphics.getWidth()/2)+300,100,1)
  30.     sg1 = createSpring(g1,g2,150,500,0.0001)
  31.     sg2 = createSpring(g2,g3,150,500,0.0001)
  32.    
  33.     --Higher Res Square
  34.     local square1 = createSquare()
  35. end
  36.  
  37. function love.update(dt)
  38.     t = t + dt
  39.     fps = 1/dt
  40.    
  41.     if love.mouse.isDown(2) == true then
  42.         for _, point in ipairs(points) do
  43.             point.velocityX = 0
  44.             point.velocityY = 0
  45.         end
  46.     end
  47.    
  48.     for _, spring in ipairs(springs) do
  49.         local normalVectorB2A = normalizedVector(spring.pointA.x,spring.pointB.x,spring.pointA.y,spring.pointB.y)
  50.         local normalVectorA2B = normalizedVector(spring.pointB.x,spring.pointA.x,spring.pointB.y,spring.pointA.y)
  51.         local velocityDifferenceX = spring.pointB.velocityX - spring.pointA.velocityX
  52.         local velocityDifferenceY = spring.pointB.velocityY - spring.pointA.velocityY
  53.        
  54.         local springForce = spring.stiffness * (distance(spring.pointA.x,spring.pointB.x,spring.pointA.y,spring.pointB.y)-spring.restingLength)
  55.         local dampingForce = (dotProduct(normalVectorB2A[1],velocityDifferenceX,normalVectorB2A[2],velocityDifferenceY))*spring.damping
  56.         local totalForce = springForce + dampingForce
  57.        
  58.         spring.pointA.springForceX = spring.pointA.springForceX + (totalForce * normalVectorB2A[1])
  59.         spring.pointA.springForceY = spring.pointA.springForceY + (totalForce * normalVectorB2A[2])
  60.        
  61.         spring.pointB.springForceX = spring.pointB.springForceX + (totalForce * normalVectorA2B[1])
  62.         spring.pointB.springForceY = spring.pointB.springForceY + (totalForce * normalVectorA2B[2])
  63.     end
  64.    
  65.     for _, point in ipairs(points) do
  66.         point.forceX = 0
  67.         point.forceY = 0
  68.         point.forceX = (gravityX * point.mass) + point.springForceX
  69.         point.forceY = (gravityY * point.mass) + point.springForceY
  70.         point.springForceX = 0
  71.         point.springForceY = 0
  72.         point.velocityX = point.velocityX + ((point.forceX/point.mass)*dt)
  73.         point.velocityY = point.velocityY + ((point.forceY/point.mass)*dt)
  74.         point.x = point.x + (point.velocityX*dt)
  75.         point.y = point.y + (point.velocityY*dt)
  76.        
  77.         if point.y >= love.graphics.getHeight() then
  78.             point.velocityY =  (love.graphics.getHeight() - point.y) * 5
  79.         end
  80.         if point.x >= love.graphics.getWidth() then
  81.             point.velocityX = (love.graphics.getWidth() - point.x) * 5
  82.         end
  83.         if point.x <= 0 then
  84.             point.velocityX = (0 - point.x) * 5
  85.         end
  86.     end
  87.    
  88.     if love.mouse.isDown(2) == true then
  89.         for _, point in ipairs(points) do
  90.             point.velocityX = 0
  91.             point.velocityY = 0
  92.         end
  93.     end
  94.    
  95.     g1.x = love.graphics.getWidth()/2
  96.     g1.y = 100
  97.    
  98. end
  99.  
  100. function love.draw()
  101.     love.graphics.setColor(1,1,1)
  102.     love.graphics.print(string.sub(t,1,5),10,10)
  103.     love.graphics.print(string.sub(fps,1,5),10,25)
  104.     love.graphics.print("Springs: "..totalSprings,10,55)
  105.     love.graphics.print("Points: "..totalPoints,10,40)
  106.    
  107.     for _, spring in ipairs(springs) do
  108.         love.graphics.setColor(spring.red,spring.green,spring.blue)
  109.         love.graphics.line(spring.pointA.x,spring.pointA.y,spring.pointB.x,spring.pointB.y)
  110.     end
  111.    
  112.     for _, point in ipairs(points) do
  113.         love.graphics.setColor(point.red,point.green,point.blue)
  114.         love.graphics.circle("fill",point.x,point.y,5)
  115.     end
  116. end
  117.  
  118. function createPoint(x, y, mass, r,g,b)
  119.     totalPoints = totalPoints + 1
  120.     local point = {id = totalPoints, x = x, y = y, mass = mass, springForceX = 0, springForceY = 0, velocityX = 0, velocityY = 0, forceX = 0, forceY = 0, red = r or 1, green = g or 1, blue = b or 1}
  121.     table.insert(points,point)
  122.     return point
  123. end
  124.  
  125. function createSpring(pointA, pointB, restingLength, stiffness, damping, r,g,b)
  126.     totalSprings = totalSprings + 1
  127.     local spring = {id = totalSprings, pointA = pointA, pointB = pointB, stiffness = stiffness, damping = damping, restingLength = restingLength, red = r or 1, green = g or 1, blue = b or 1}
  128.     table.insert(springs, spring)
  129.     return spring
  130. end
  131.  
  132. function dotProduct(x1, x2, y1, y2)
  133.     local x = (x1 * x2) + (y1 * y2)
  134.     return x
  135. end
  136.  
  137. function distance(x1, x2, y1, y2)
  138.     local x = ((((x2-x1)^2) + ((y2-y1)^2))^0.5)
  139.     return x
  140. end
  141.  
  142. function normalizedVector(x1, x2, y1, y2)
  143.     local dist = distance(x1,x2,y1,y2)
  144.     local x = (x2 - x1)/dist
  145.     local y = (y2 - y1)/dist
  146.     return {x,y}
  147. end
  148.  
  149. function createSquare()
  150.     local h11 = createPoint(400,100,1,1,0,0)
  151.     local h12 = createPoint(450,100,1,1,0,0)
  152.     local h13 = createPoint(500,100,1,1,0,0)
  153.     local h21 = createPoint(400,150,1,1,0,0)
  154.     local h22 = createPoint(450,150,1,1,0,0)
  155.     local h23 = createPoint(500,150,1,1,0,0)
  156.     local h31 = createPoint(400,200,1,1,0,0)
  157.     local h32 = createPoint(450,200,1,1,0,0)
  158.     local h33 = createPoint(500,200,1,1,0,0)
  159.    
  160.     local u1 = createSpring(h11,h12,51,globalStiffness,1)
  161.     local u2 = createSpring(h12,h13,50,globalStiffness,1)
  162.     local u3 = createSpring(h11,h21,50,globalStiffness,1)
  163.     local u4 = createSpring(h12,h22,50,globalStiffness,1)
  164.     local u5 = createSpring(h13,h23,50,globalStiffness,1)
  165.     local u6 = createSpring(h21,h22,50,globalStiffness,1)
  166.     local u7 = createSpring(h22,h23,50,globalStiffness,1)
  167.     local u8 = createSpring(h21,h31,50,globalStiffness,1)
  168.     local u9 = createSpring(h22,h32,50,globalStiffness,1)
  169.     local u10 = createSpring(h23,h33,50,globalStiffness,1)
  170.     local u11 = createSpring(h31,h32,50,globalStiffness,1)
  171.     local u12 = createSpring(h32,h33,50,globalStiffness,1)
  172.    
  173.     local i1 = createSpring(h11,h22,(math.sqrt(2)*50),globalStiffness,1)
  174.     local i2 = createSpring(h12,h21,(math.sqrt(2)*50),globalStiffness,1)
  175.     local i3 = createSpring(h12,h23,(math.sqrt(2)*50),globalStiffness,1)
  176.     local i4 = createSpring(h22,h13,(math.sqrt(2)*50),globalStiffness,1)
  177.     local i5 = createSpring(h21,h32,(math.sqrt(2)*50),globalStiffness,1)
  178.     local i6 = createSpring(h22,h31,(math.sqrt(2)*50),globalStiffness,1)
  179.     local i7 = createSpring(h22,h33,(math.sqrt(2)*50),globalStiffness,1)
  180.     local i8 = createSpring(h23,h32,(math.sqrt(2)*50),globalStiffness,1)
  181. end
  182.  
  183. function love.mousepressed( x, y, button, istouch, presses )
  184.     local square = createSquare()
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement