Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rope = {}
- rope.ActiveRopes = {}
- local drag = 0.9
- local delta = 1
- local function add_vel(a, b)
- -- update the position
- a.pos.x = a.pos.x + a.vel.x
- a.pos.y = a.pos.y + a.vel.y
- a.pos.z = a.pos.z + a.vel.z
- local vx,vy,vz = b.pos.x-a.pos.x, b.pos.y-a.pos.y, b.pos.z-a.pos.z -- offset
- local len = vx*vx + vy*vy + vz*vz -- sqr len
- local force = len * 5
- vx = (vx/len) * force
- vy = (vy/len) * force
- vz = (vz/len) * force
- vx = vx * delta
- vy = vy * delta
- vz = (vz - 1) * delta -- is this the right place to add graivty?
- a.vel.x = a.vel.x * drag
- a.vel.y = a.vel.y * drag
- a.vel.z = a.vel.z * drag
- -- add the new velocity
- a.vel.x = a.vel.x + vx
- a.vel.y = a.vel.y + vy
- a.vel.z = a.vel.z + vz
- end
- function rope.Think()
- delta = FrameTime()
- for key, rope in pairs(rope.ActiveRopes) do
- local points = rope.points
- for i = 2, #points do
- add_vel(points[i - 1], points[i])
- add_vel(points[i], points[i - 1])
- end
- for i, var in pairs(rope.attachments) do
- local point = rope.points[i]
- if point then
- if type(var) == "function" then
- local val = var()
- point.pos.x = val.x
- point.pos.y = val.y
- point.pos.z = val.z
- else
- local val = var
- point.pos.x = val.x
- point.pos.y = val.y
- point.pos.z = val.z
- end
- point.vel.x = 0
- point.vel.x = 0
- point.vel.x = 0
- end
- end
- end
- end
- function rope.Draw()
- for key, rope in pairs(rope.ActiveRopes) do
- local count = #rope.points
- render.SetMaterial(rope.mat)
- render.StartBeam(count)
- for i = 1, count do
- local v = rope.points[i].pos
- render.AddBeam(Vector(v.x, v.y, v.z), 20, i/count, color_white)
- end
- render.EndBeam()
- end
- end
- function rope.Create(points, attachments, mat)
- points = points or 128
- mat = mat or Material("cable/rope")
- attachments = attachments or {[1] = function() return LocalPlayer():EyePos() end, [#points] = function() return Vector(0,0,0) end}
- local data = {}
- data.mat = mat
- data.points = {}
- data.attachments = attachments
- local _,pos = next(attachments)
- if type(pos) == "function" then pos = pos() end
- for i = 1, points do
- data.points[i] = {pos = {x=pos.x+math.Rand(-10,10),y=pos.y+math.Rand(-10,10),z=pos.z+math.Rand(-10,10)}, vel = {x=0,y=0,z=0}}
- end
- local id = table.insert(rope.ActiveRopes, data)
- data.Remove = function() table.remove(rope.ActiveRopes, id) end
- return data
- end
- hook.Add("Think", "rope", rope.Think)
- hook.Add("PostDrawTranslucentRenderables", "rope", rope.Draw)
- rope.Create(100, {[1] = function() return Entity(473):GetPos() end, [100] = function() return Entity(474):GetPos() end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement