Advertisement
Catsss

CC verlet intergration

Oct 27th, 2021 (edited)
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. --Created by Bloof--
  2. --WIP--
  3.  
  4. function getMag(self)
  5.   return math.sqrt(self.x^2 + self.y^2)
  6. end
  7.  
  8. local monitor = peripheral.wrap("right")
  9. local wid,hei = monitor.getSize()
  10. local canBreak = false
  11. local numberOfPoints = tonumber(read())
  12. local points = {{["pos"] = vector.new(4,2,0), ["vel"] = vector.new(1,0,0), ["lastPos"] = vector.new(2,2,0), ["acc"] = vector.new(0,0,0), ["bounce"] = 1, ["anc"] = true}}
  13. local constraints = {}
  14.  
  15. for i = 1, numberOfPoints do
  16.     table.insert(points, {["pos"] = vector.new(4+i,1,0), ["vel"] = vector.new(0,0,0), ["lastPos"] = vector.new(4+i,1,0), ["acc"] = vector.new(0,1.5,0), ["bounce"] = 5, ["anc"] = false})
  17. end
  18.  
  19. --table.insert(points, {["pos"] = vector.new(4 + numberOfPoints,2,0), ["vel"] = vector.new(1,0,0), ["lastPos"] = vector.new(4  + numberOfPoints,2,0), ["acc"] = vector.new(0,0,0), ["bounce"] = 1, ["anc"] = true})
  20.  
  21. for i = 1, numberOfPoints+1 do
  22.     if i <= numberOfPoints then
  23.         table.insert(constraints, {["p1"] = points[i], ["p2"] = points[i + 1], ["dist"] = 3})
  24.     else
  25.         table.insert(constraints, {["p1"] = points[i], ["p2"] = points[i - 1], ["dist"] = 3})
  26.     end
  27. end
  28.  
  29.  
  30. function solveConstraint(cons)
  31.     if cons["p1"] and cons["p2"] then
  32.         local p1 = cons["p1"]
  33.         local p2 = cons["p2"]
  34.         local diff = (p1["pos"]-p2["pos"]) or 0
  35.         local dist = getMag(diff) or 0
  36.         local scal = (cons["dist"] - dist) / dist or 0
  37.         local trans = diff * 0.5 * scal
  38.        
  39.         --print(diff, dist, scal, trans)
  40.        
  41.         if not p1["anc"] then
  42.             p1["pos"] = p1["pos"] + trans
  43.             --print(p1["pos"])
  44.         end
  45.        
  46.         if not p2["anc"] then
  47.             p2["pos"] = p2["pos"] - trans
  48.             --print(p2["pos"])
  49.         end
  50.        
  51.     end;
  52. end;
  53.  
  54. --[[for ind,obj in pairs(points) do
  55.     obj["vel"] = vector.new(math.random(-2,2),math.random(-2,2),0)
  56. end]]
  57.  
  58. while true do
  59.     sleep(0.25)
  60.     monitor.clear()
  61.     for ind, obj in pairs(points) do
  62.         obj["vel"] = (obj["pos"] - obj["lastPos"])
  63.  
  64.         --prevent from going into wall x0
  65.         if obj["pos"].x < 0 then
  66.             obj["pos"].x = 0
  67.             if obj["vel"].x < 0 then
  68.                 obj["vel"].x = (obj["vel"].x * -1) / obj["bounce"]
  69.             end
  70.         end
  71.  
  72.         --prevent from going into wall xW
  73.         if obj["pos"].x > wid then
  74.             obj["pos"].x = wid
  75.             if obj["vel"].x > 0 then
  76.                 obj["vel"].x = (obj["vel"].x * -1) / obj["bounce"]
  77.             end
  78.         end
  79.  
  80.         --prevent from going into wall y0
  81.         if obj["pos"].y < 0 then
  82.             obj["pos"].y = 0
  83.             if obj["vel"].y < 0 then
  84.                 obj["vel"].y = (obj["vel"].y * -1) / obj["bounce"]
  85.             end
  86.         end
  87.  
  88.         --prevent from going into wall yH
  89.         if obj["pos"].y > hei then
  90.             obj["pos"].y = hei
  91.             if obj["vel"].y > 0 then
  92.                 obj["vel"].y = (obj["vel"].y * -1) / obj["bounce"]
  93.             end
  94.         end
  95.  
  96.         local nextPos = obj["pos"] + obj["vel"] * 1 + obj["acc"]
  97.         obj["lastPos"] = obj["pos"]
  98.         obj["pos"] = nextPos
  99.     end
  100.  
  101.     for ind, cons in pairs(constraints) do
  102.         if ind <= numberOfPoints then
  103.             --print("test")
  104.             for i = 1, 100 do
  105.                 --print(obj["pos"], obj["vel"])
  106.                 solveConstraint(constraints[ind])
  107.             end
  108.         end
  109.     end
  110.  
  111.     for ind, obj in pairs(points) do
  112.         monitor.setCursorPos(obj["pos"].x, obj["pos"].y)
  113.         monitor.write("#")
  114.     end
  115. end
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement