Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rope = {}
- rope.ActiveRopes = {}
- local A = Vector()
- local damping = -0.05
- local speed = 1
- local gravity = {x=0,y=0,z=0}
- local temp_vec = Vector() -- temp
- local point_contents = util.PointContents
- local band = bit.band
- local CONTENTS_SOLID = CONTENTS_SOLID
- local function add_vel(a, vx, vy, vz, ft)
- a.vel.x = a.vel.x + (vx * ft)
- a.vel.y = a.vel.y + (vy * ft)
- a.vel.z = a.vel.z + (vz * ft)
- end
- local function check_collision(a, ft)
- temp_vec.x = a.pos.x + (a.vel.x) * ft
- temp_vec.y = a.pos.y + (a.vel.y) * ft
- temp_vec.z = a.pos.z + (a.vel.z) * ft
- local content = point_contents(temp_vec)
- if band(content, CONTENTS_SOLID) == CONTENTS_SOLID then
- a.vel.x = a.vel.x * 0.25
- a.vel.y = a.vel.y * 0.25
- a.vel.z = -a.vel.z * 0.25
- return true
- end
- end
- local function calc(a, b, ft)
- if not a.first then
- add_vel(a, math.Rand(-1,1)*10, math.Rand(-1,1)*10, math.Rand(-1,1)*10, ft)
- a.first = true
- end
- add_vel(
- a,
- (b.pos.x - a.pos.x) * speed,
- (b.pos.y - a.pos.y) * speed,
- (b.pos.z - a.pos.z) * speed,
- ft
- )
- --add_vel(a, gravity.x, gravity.y, gravity.z, ft)
- if not check_collision(a, ft) then
- a.pos.x = a.pos.x + (a.vel.x) * ft
- a.pos.y = a.pos.y + (a.vel.y) * ft
- a.pos.z = a.pos.z + (a.vel.z) * ft
- end
- a.vel.x = a.vel.x + a.vel.x * damping
- a.vel.y = a.vel.y + a.vel.y * damping
- a.vel.z = a.vel.z + a.vel.z * damping
- end
- function rope.Think()
- A = Entity(483):GetPos()
- local ft = RealFrameTime() * 10
- for key, rope in pairs(rope.ActiveRopes) do
- local points = rope.points
- for i = 2, #points do
- calc(points[i - 1], points[i], ft)
- calc(points[i], points[i - 1], ft)
- 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.y = 0
- point.vel.z = 0
- end
- end
- end
- end
- local color_white = color_white
- local sprite_mat = Material("particle/particle_sphere")
- 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
- temp_vec.x = v.x
- temp_vec.y = v.y
- temp_vec.z = v.z
- render.AddBeam(temp_vec, 20, i/count, color_white)
- end
- render.EndBeam()
- render.SetMaterial(sprite_mat)
- for i = 1, count do
- local v = rope.points[i].pos
- temp_vec.x = v.x
- temp_vec.y = v.y
- temp_vec.z = v.z
- render.DrawSprite(temp_vec, 20, 20, color_white)
- end
- 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 = select(2, next(attachments))
- pos = type(pos) == "function" and pos() or pos or VectorRand()
- for i = 1, points do
- data.points[i] = {
- pos =
- {
- x = pos.x,
- y = pos.y,
- z = pos.z,
- },
- vel = {x=math.Rand(-1,1),y=math.Rand(-1,1),z=math.Rand(-1,1)}
- }
- end
- data.id = table.insert(rope.ActiveRopes, data)
- data.Remove = function()
- table.remove(rope.ActiveRopes, data.id)
- end
- return data
- end
- hook.Add("Think", "rope", rope.Think)
- hook.Add("PostDrawTranslucentRenderables", "rope", rope.Draw)
- rope.Create(128, {[1] = function() return Entity(483):GetPos() end, [128] = function() return Entity(485):GetPos() end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement