Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- t = 0
- love.window.setFullscreen(true, "desktop")
- gravityX = 0
- gravityY = 400
- points = {}
- springs = {}
- totalPoints = 0
- totalSprings = 0
- globalStiffness = 550
- --Square
- p1 = createPoint(100,100,1,1,0,0)
- p2 = createPoint(300,100,1,1,0,0)
- p3 = createPoint(100,300,1,1,0,0)
- p4 = createPoint(300,300,1,1,0,0)
- s1 = createSpring(p1,p2,200,globalStiffness,1,0,1,0)
- s2 = createSpring(p1,p3,200,globalStiffness,1,0,1,0)
- s3 = createSpring(p2,p4,200,globalStiffness,1,0,1,0)
- s4 = createSpring(p3,p4,200,globalStiffness,1,0,1,0)
- c1 = createSpring(p1,p4,(math.sqrt(2)*200),globalStiffness,1,0,0,1)
- c2 = createSpring(p2,p3,(math.sqrt(2)*200),globalStiffness,1,0,0,1)
- --Double Pendulum
- g1 = createPoint(love.graphics.getWidth()/2,100,1)
- g2 = createPoint((love.graphics.getWidth()/2)+150,100,1)
- g3 = createPoint((love.graphics.getWidth()/2)+300,100,1)
- sg1 = createSpring(g1,g2,150,500,0.0001)
- sg2 = createSpring(g2,g3,150,500,0.0001)
- --Higher Res Square
- local square1 = createSquare()
- end
- function love.update(dt)
- t = t + dt
- fps = 1/dt
- if love.mouse.isDown(2) == true then
- for _, point in ipairs(points) do
- point.velocityX = 0
- point.velocityY = 0
- end
- end
- for _, spring in ipairs(springs) do
- local normalVectorB2A = normalizedVector(spring.pointA.x,spring.pointB.x,spring.pointA.y,spring.pointB.y)
- local normalVectorA2B = normalizedVector(spring.pointB.x,spring.pointA.x,spring.pointB.y,spring.pointA.y)
- local velocityDifferenceX = spring.pointB.velocityX - spring.pointA.velocityX
- local velocityDifferenceY = spring.pointB.velocityY - spring.pointA.velocityY
- local springForce = spring.stiffness * (distance(spring.pointA.x,spring.pointB.x,spring.pointA.y,spring.pointB.y)-spring.restingLength)
- local dampingForce = (dotProduct(normalVectorB2A[1],velocityDifferenceX,normalVectorB2A[2],velocityDifferenceY))*spring.damping
- local totalForce = springForce + dampingForce
- spring.pointA.springForceX = spring.pointA.springForceX + (totalForce * normalVectorB2A[1])
- spring.pointA.springForceY = spring.pointA.springForceY + (totalForce * normalVectorB2A[2])
- spring.pointB.springForceX = spring.pointB.springForceX + (totalForce * normalVectorA2B[1])
- spring.pointB.springForceY = spring.pointB.springForceY + (totalForce * normalVectorA2B[2])
- end
- for _, point in ipairs(points) do
- point.forceX = 0
- point.forceY = 0
- point.forceX = (gravityX * point.mass) + point.springForceX
- point.forceY = (gravityY * point.mass) + point.springForceY
- point.springForceX = 0
- point.springForceY = 0
- point.velocityX = point.velocityX + ((point.forceX/point.mass)*dt)
- point.velocityY = point.velocityY + ((point.forceY/point.mass)*dt)
- point.x = point.x + (point.velocityX*dt)
- point.y = point.y + (point.velocityY*dt)
- if point.y >= love.graphics.getHeight() then
- point.velocityY = (love.graphics.getHeight() - point.y) * 5
- end
- if point.x >= love.graphics.getWidth() then
- point.velocityX = (love.graphics.getWidth() - point.x) * 5
- end
- if point.x <= 0 then
- point.velocityX = (0 - point.x) * 5
- end
- end
- if love.mouse.isDown(2) == true then
- for _, point in ipairs(points) do
- point.velocityX = 0
- point.velocityY = 0
- end
- end
- g1.x = love.graphics.getWidth()/2
- g1.y = 100
- end
- function love.draw()
- love.graphics.setColor(1,1,1)
- love.graphics.print(string.sub(t,1,5),10,10)
- love.graphics.print(string.sub(fps,1,5),10,25)
- love.graphics.print("Springs: "..totalSprings,10,55)
- love.graphics.print("Points: "..totalPoints,10,40)
- for _, spring in ipairs(springs) do
- love.graphics.setColor(spring.red,spring.green,spring.blue)
- love.graphics.line(spring.pointA.x,spring.pointA.y,spring.pointB.x,spring.pointB.y)
- end
- for _, point in ipairs(points) do
- love.graphics.setColor(point.red,point.green,point.blue)
- love.graphics.circle("fill",point.x,point.y,5)
- end
- end
- function createPoint(x, y, mass, r,g,b)
- totalPoints = totalPoints + 1
- 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}
- table.insert(points,point)
- return point
- end
- function createSpring(pointA, pointB, restingLength, stiffness, damping, r,g,b)
- totalSprings = totalSprings + 1
- 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}
- table.insert(springs, spring)
- return spring
- end
- function dotProduct(x1, x2, y1, y2)
- local x = (x1 * x2) + (y1 * y2)
- return x
- end
- function distance(x1, x2, y1, y2)
- local x = ((((x2-x1)^2) + ((y2-y1)^2))^0.5)
- return x
- end
- function normalizedVector(x1, x2, y1, y2)
- local dist = distance(x1,x2,y1,y2)
- local x = (x2 - x1)/dist
- local y = (y2 - y1)/dist
- return {x,y}
- end
- function createSquare()
- local h11 = createPoint(400,100,1,1,0,0)
- local h12 = createPoint(450,100,1,1,0,0)
- local h13 = createPoint(500,100,1,1,0,0)
- local h21 = createPoint(400,150,1,1,0,0)
- local h22 = createPoint(450,150,1,1,0,0)
- local h23 = createPoint(500,150,1,1,0,0)
- local h31 = createPoint(400,200,1,1,0,0)
- local h32 = createPoint(450,200,1,1,0,0)
- local h33 = createPoint(500,200,1,1,0,0)
- local u1 = createSpring(h11,h12,51,globalStiffness,1)
- local u2 = createSpring(h12,h13,50,globalStiffness,1)
- local u3 = createSpring(h11,h21,50,globalStiffness,1)
- local u4 = createSpring(h12,h22,50,globalStiffness,1)
- local u5 = createSpring(h13,h23,50,globalStiffness,1)
- local u6 = createSpring(h21,h22,50,globalStiffness,1)
- local u7 = createSpring(h22,h23,50,globalStiffness,1)
- local u8 = createSpring(h21,h31,50,globalStiffness,1)
- local u9 = createSpring(h22,h32,50,globalStiffness,1)
- local u10 = createSpring(h23,h33,50,globalStiffness,1)
- local u11 = createSpring(h31,h32,50,globalStiffness,1)
- local u12 = createSpring(h32,h33,50,globalStiffness,1)
- local i1 = createSpring(h11,h22,(math.sqrt(2)*50),globalStiffness,1)
- local i2 = createSpring(h12,h21,(math.sqrt(2)*50),globalStiffness,1)
- local i3 = createSpring(h12,h23,(math.sqrt(2)*50),globalStiffness,1)
- local i4 = createSpring(h22,h13,(math.sqrt(2)*50),globalStiffness,1)
- local i5 = createSpring(h21,h32,(math.sqrt(2)*50),globalStiffness,1)
- local i6 = createSpring(h22,h31,(math.sqrt(2)*50),globalStiffness,1)
- local i7 = createSpring(h22,h33,(math.sqrt(2)*50),globalStiffness,1)
- local i8 = createSpring(h23,h32,(math.sqrt(2)*50),globalStiffness,1)
- end
- function love.mousepressed( x, y, button, istouch, presses )
- local square = createSquare()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement