Advertisement
CapsAdmin

Untitled

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