Advertisement
CapsAdmin

Untitled

Mar 5th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. rope = {}
  2.  
  3. rope.ActiveRopes = {}
  4.  
  5. local drag = -0.001
  6. local delta = 1
  7.  
  8. local function add_vel(a, b)
  9.     local vx,vy,vz = b.pos.x-a.pos.x, b.pos.y-a.pos.y, b.pos.z-a.pos.z -- offset
  10.     local len = vx*vx + vy*vy + vz*vz -- sqr len
  11.    
  12.     local force = len * 10
  13.    
  14.     vx = (vx/len) * force
  15.     vy = (vy/len) * force
  16.     vz = (vz/len) * force
  17.    
  18.     vz = vz - 10
  19.    
  20.     -- add the new velocity
  21.     a.vel.x = a.vel.x + vx
  22.     a.vel.y = a.vel.y + vy
  23.     a.vel.z = a.vel.z + vz
  24.        
  25.     -- velocity
  26.     a.pos.x = a.pos.x + a.vel.x * delta
  27.     a.pos.y = a.pos.y + a.vel.y * delta
  28.     a.pos.z = a.pos.z + a.vel.z * delta
  29.    
  30.     -- friction
  31.     a.vel.x = a.vel.x * drag
  32.     a.vel.y = a.vel.y * drag
  33.     a.vel.z = a.vel.z * drag   
  34. end
  35.  
  36. function rope.Think()
  37.     delta = math.Clamp(FrameTime(), 0, 0.01) * 10
  38.  
  39.     for key, rope in pairs(rope.ActiveRopes) do
  40.         local points = rope.points
  41.                
  42.         for i = 2, #points do
  43.             add_vel(points[i - 1], points[i])
  44.             add_vel(points[i], points[i - 1])
  45.         end
  46.        
  47.         for i, var in pairs(rope.attachments) do
  48.             local point = rope.points[i]
  49.            
  50.             if point then
  51.                 if type(var) == "function" then
  52.                     local val = var()
  53.                     point.pos.x = val.x
  54.                     point.pos.y = val.y
  55.                     point.pos.z = val.z
  56.                 else
  57.                     local val = var
  58.                     point.pos.x = val.x
  59.                     point.pos.y = val.y
  60.                     point.pos.z = val.z
  61.                 end
  62.             end
  63.         end
  64.     end
  65. end
  66.  
  67. function rope.Draw()
  68.     for key, rope in pairs(rope.ActiveRopes) do
  69.         local count = #rope.points
  70.            
  71.         render.SetMaterial(rope.mat)
  72.        
  73.         render.StartBeam(count)    
  74.             for i = 1, count do
  75.                 local v = rope.points[i].pos
  76.                 render.AddBeam(Vector(v.x, v.y, v.z), 20, i/count, color_white)    
  77.             end
  78.         render.EndBeam()
  79.     end
  80. end
  81.  
  82. function rope.Create(points, attachments, mat)
  83.     points = points or 128
  84.     mat = mat or Material("cable/rope")
  85.     attachments = attachments or {[1] = function() return LocalPlayer():EyePos() end, [#points] = function() return Vector(0,0,0) end}
  86.    
  87.     local data = {}
  88.    
  89.     data.mat = mat
  90.     data.points = {}
  91.     data.attachments = attachments
  92.    
  93.     local  _,pos = next(attachments)
  94.    
  95.     if type(pos) == "function" then pos = pos() end
  96.    
  97.     for i = 1, points do
  98.         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}}
  99.     end
  100.    
  101.     local id = table.insert(rope.ActiveRopes, data)
  102.    
  103.     data.Remove = function() table.remove(rope.ActiveRopes, id) end
  104.    
  105.     return data
  106. end
  107.  
  108. hook.Add("Think", "rope", rope.Think)
  109. hook.Add("PostDrawTranslucentRenderables", "rope", rope.Draw)
  110.  
  111. 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