Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.56 KB | None | 0 0
  1. functions = {}
  2. clients = {}
  3. spring = {}
  4.  
  5. renderRate = 120
  6.  
  7. --// Spring Class Setup
  8. do
  9.     local e = 2.7182818284590452353602875 -- euler's number
  10.  
  11.     function spring.new(position, velocity, target)
  12.         local self = setmetatable({}, {__index = spring})
  13.  
  14.         self.position = position
  15.         self.velocity = velocity
  16.         self.target = target
  17.         self.k = 1
  18.         self.d = 1 -- friction constant
  19.  
  20.         return self
  21.     end
  22.      
  23.     function spring:update()
  24.         local x = self.target - self.position
  25.         local f = x * self.k
  26.         self.velocity = (self.velocity * (1 - self.d)) + f
  27.         self.position = self.position + self.velocity
  28.     end
  29. end
  30.  
  31.  
  32. -- // Modules
  33. local chain = require(game.ReplicatedStorage.FABRIK)
  34. --
  35. local Character = script.Parent
  36. local RightShoulderBase0, RightShoulderBase1 = Character.RightUpperArm.RightShoulder.C0, Character.RightUpperArm.RightShoulder.C1
  37. local LeftShoulderBase0, LeftShoulderBase1 = Character.LeftUpperArm.LeftShoulder.C0, Character.LeftUpperArm.LeftShoulder.C1
  38.  
  39. local ElbowBase0, ElbowBase1 = Character.RightLowerArm.RightElbow.C0, Character.RightLowerArm.RightElbow.C1
  40.  
  41.  
  42.  
  43. Character.RightUpperArm.RightShoulder.C0 = RightShoulderBase0 --* CFrame.Angles(math.rad(45),0,0)
  44. Character.RightUpperArm.RightShoulder.C1 = RightShoulderBase1
  45.    
  46. Character.RightLowerArm.RightElbow.C0 = ElbowBase0 --* CFrame.Angles(math.rad(-45),0,0)
  47. Character.RightLowerArm.RightElbow.C1 = ElbowBase1
  48.  
  49.  
  50. character = {}
  51.  
  52. local function projectVector(a, b)
  53.     return a - a:Dot(b.unit)*b
  54. end
  55.  
  56. local function vectorToAngle(v0,v1,plane)
  57.     v0,v1 = projectVector(v0.unit, plane), projectVector(v1.unit, plane)
  58.     local angle = math.acos(v0:Dot(v1))
  59.     local cross = v0:Cross(v1)
  60.     if plane:Dot(cross) < 0 then
  61.         angle = -angle
  62.     end
  63.     return angle
  64. end
  65.  
  66. local rightArm = chain.new({
  67.         Vector3.new(0,0,0), --Shoulder
  68.         Vector3.new(0,-0.9,0), --Elbow
  69.         Vector3.new(0,-2,0) --Grip
  70.     },
  71.         Vector3.new(0,-2,0)
  72. )
  73.  
  74. local leftArm = chain.new({
  75.         Vector3.new(0,0,0), --Shoulder
  76.         Vector3.new(0,-0.9,0), --Elbow
  77.         Vector3.new(0,-2,0) --Grip
  78.     },
  79.         Vector3.new(0,-2,0)
  80.     )
  81.  
  82.  
  83. wait(2)
  84. local completed = false
  85. local lastUpdate = 0
  86. local part = Instance.new("Part",game.Workspace)
  87. part.Size = Vector3.new(.2,.2,.2)
  88. part.Anchored = true
  89. local parts = {}
  90.  
  91.  
  92. game:GetService('RunService').RenderStepped:connect(function()
  93.     if tick() > lastUpdate + 1/renderRate then
  94.         lastUpdate = tick()
  95.         local ShoulderCF = CFrame.new(Character.UpperTorso.RightShoulderRigAttachment.WorldPosition, Character.UpperTorso.RightShoulderRigAttachment.WorldPosition + Character.UpperTorso.CFrame.lookVector)
  96.         local LeftShoulderCF = CFrame.new(Character.UpperTorso.LeftShoulderRigAttachment.WorldPosition, Character.UpperTorso.LeftShoulderRigAttachment.WorldPosition + Character.UpperTorso.CFrame.lookVector)
  97.         local target = ShoulderCF:toObjectSpace(Character:FindFirstChild('Weapon').CFrame).p
  98.         rightArm.target = target
  99.         leftArm.target = LeftShoulderCF:toObjectSpace(Character:FindFirstChild('Weapon').CFrame * CFrame.new(0,0,-.8)).p   --+Right/-Left, +Up/-Down, +Back/-Forward
  100.         rightArm:solve()
  101.         leftArm:solve()
  102.         --part.CFrame = ShoulderCF * CFrame.new(rightArm.joints[3])
  103.         --// Right Arm
  104.         local Vectors = {
  105.             ShoulderBase    = Vector3.new(0,-1,0),
  106.             ShoulderFace    = Vector3.new(0,0,-1),
  107.             ShoulderToElbow = (rightArm.joints[2] - rightArm.joints[1]).unit,
  108.            
  109.             ElbowBase       = (rightArm.joints[2] - rightArm.joints[1]).unit,
  110.             --ElbowFace     =
  111.             ElbowToGrip     = (rightArm.joints[3] - rightArm.joints[2]).unit
  112.         }
  113.        
  114.         local Normals = {
  115.             Right           = Vector3.FromNormalId(Enum.NormalId.Right),
  116.             Up              = Vector3.FromNormalId(Enum.NormalId.Top),
  117.             Front           = Vector3.FromNormalId(Enum.NormalId.Front),
  118.         }
  119.         local RightShoulderX = vectorToAngle(Vectors.ShoulderBase, Vectors.ShoulderToElbow, Normals.Front)
  120.         local RightShoulderY = vectorToAngle(Vectors.ShoulderFace, Vectors.ShoulderToElbow, Normals.Up)
  121.        
  122.         local RightElbowX    = vectorToAngle(Vectors.ElbowBase, Vectors.ElbowToGrip, Normals.Front)
  123.         local RightElbowY    = vectorToAngle(Vectors.ShoulderFace, Vectors.ElbowToGrip, Normals.Up)
  124.         print(math.deg(RightElbowX),math.deg(RightShoulderY))
  125.         Character.RightUpperArm.RightShoulder.C0 = RightShoulderBase0 * CFrame.Angles(RightShoulderX, RightElbowY, -RightShoulderY) --ShoulderZ, ShoulderX), 1/20)
  126.         Character.RightLowerArm.RightElbow.C0 = ElbowBase0 * CFrame.Angles(RightElbowX,0,0)
  127.         for i,v in ipairs(parts) do
  128.             v:Destroy()
  129.         end
  130.         for i,v in ipairs(rightArm.joints) do
  131.             local part = Instance.new("Part",game.Workspace)
  132.             part.Size = Vector3.new(.2,.2,.2)
  133.             part.Anchored = true
  134.             part.CFrame = ShoulderCF * CFrame.new(v)
  135.             part.CanCollide = false
  136.             table.insert(parts,part)
  137.         end
  138.         --Character.RightLowerArm.RightElbow.C0 = ElbowBase0 * CFrame.Angles(RightElbowX,0,0)
  139.     -- 
  140.     --  --// Left Arm
  141.     --  local LeftShoulderX = vectorToAngle((Vector3.new(0,-1,0) - leftArm.joints[1]).unit, (leftArm.joints[2] - leftArm.joints[1]).unit)
  142.     --  local LeftShoulderY = vectorToAngle((Vector3.new(1,0,0) - leftArm.joints[1]).unit, (leftArm.joints[2] - leftArm.joints[1]).unit)
  143.     -- 
  144.     --  local LeftElbowX = vectorToAngle((leftArm.joints[2] - leftArm.joints[1]).unit, (leftArm.joints[3] - leftArm.joints[2]).unit)
  145.     --  local LeftElbowY = vectorToAngle((Vector3.new(-1,0,0)).unit, (leftArm.joints[2] - leftArm.joints[1]).unit)
  146.     --  print(math.deg(LeftElbowY))
  147.     --  Character.LeftUpperArm.LeftShoulder.C0 = LeftShoulderBase0 * CFrame.Angles(LeftShoulderX, LeftShoulderY, LeftElbowY) --ShoulderZ, ShoulderX), 1/20)
  148.     --  Character.LeftLowerArm.LeftElbow.C0 = ElbowBase0 * CFrame.Angles(LeftElbowX,0,0)
  149.     else
  150.         print("Didn't render")
  151.     end
  152. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement