linee

Nov 28th, 2023
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local points, sticks = {}, {}
2. local gravity = 0.1
3.
4. function point(x, y, z, pinned)
5.     return {
6.         pos = Vector3(x, y, z),
7.         oldPos = Vector3(x, y, z),
8.         pinned = (pinned or false)
9.     }
10. end
11.
12. function stick(a, b)
13.     return {
14.         p0 = points[a],
15.         p1 = points[b],
16.         length = getDistanceBetweenPoints3D(points[a].pos, points[b].pos)*2
17.     }
18. end
19.
20. function updatePoints(dt)
21.     for k,v in pairs(points) do
22.         if not v.pinned then
23.             if isLineOfSightClear(v.pos, Vector3(v.pos.x, v.pos.y, v.pos.z - gravity*dt), true, false, false, true) then
24.                 v.pos.z = v.pos.z - gravity*dt
25.             end
26.             while not isLineOfSightClear(v.pos, Vector3(v.pos.x, v.pos.y, v.pos.z + 0.1), true, false, false, true) do
27.                 v.pos.z = v.pos.z + 0.1
28.             end
29.         end
30.     end
31. end
32.
34.     for k,v in pairs(sticks) do
35.         local dx = v.p1.pos.x - v.p0.pos.x
36.         local dy = v.p1.pos.y - v.p0.pos.y
37.         local dz = v.p1.pos.z - v.p0.pos.z
38.         local dist = math.sqrt(dx * dx + dy * dy + dz * dz)
39.         local diff = v.length - dist
40.
41.         local percent = diff / dist / 2
42.         local offsetX = dx * percent
43.         local offsetY = dy * percent
44.         local offsetZ = dz * percent
45.
46.         if not v.p0.pinned then
47.             v.p0.pos.x = v.p0.pos.x - offsetX
48.             v.p0.pos.y = v.p0.pos.y - offsetY
49.             v.p0.pos.z = v.p0.pos.z - offsetZ
50.         end
51.
52.         if not v.p1.pinned then
53.             v.p1.pos.x = v.p1.pos.x + offsetX
54.             v.p1.pos.y = v.p1.pos.y + offsetY
55.             v.p1.pos.z = v.p1.pos.z + offsetZ
56.         end
57.     end
58. end
59.
60. function renderPoints()
61.     for k,v in pairs(points) do
62.         local x, y = getScreenFromWorldPosition(v.pos)
63.         if x and y then
64.             dxDrawRectangle(x-2, y-2, 4, 4, white)
65.         end
66.     end
67. end
68.
69. function renderSticks()
70.     for k,v in pairs(sticks) do
71.         dxDrawLine3D(v.p0.pos, v.p1.pos, tocolor(0, 0, 0), 3)
72.     end
73. end
74.
75. function init()
76.     table.insert(points, point(0, 0, 6, true))
77.     for i = 1, 20 do
78.         table.insert(points, point(0, 0, 6 - 0.1*i))
79.         table.insert(sticks, stick(i, i+1))
80.     end
81. end
82. init()
83.
85.     updatePoints(dt/100)
87.     --renderPoints()
88.     renderSticks()
89. end)
90.
91.
92.
93. -- debug
94.