D_rawest16

Boids

May 14th, 2021
651
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Max = 200
  2. local start = game.Workspace.Flock.Part
  3. local pos,size = game.Workspace.Area:GetBoundingBox()
  4. local RunService = game:GetService("RunService")
  5. local physics = game:GetService("PhysicsService")
  6. local modelgroup = physics:CreateCollisionGroup('p')
  7.  
  8. physics:CollisionGroupSetCollidable('p','p',false)
  9. pos = pos * Vector3.new() -- turning it into a vector3 value
  10. size = size/2
  11.  
  12. local function Rand()
  13.     return Vector3.new(math.random(-size.X,size.X),math.random(-size.Y,size.Y),math.random(-size.Z,size.Z))
  14. end
  15.  
  16. local parts = {}
  17. physics:SetPartCollisionGroup(game.Workspace.Baseplate,'p')
  18. table.insert(parts,start)
  19.  
  20. for i = 1,Max do
  21.     local clone = start:Clone()
  22.     clone.Position = pos + Rand()
  23.     clone.CanCollide = true
  24.     clone.Massless = true
  25.     clone.CFrame = clone.CFrame * CFrame.Angles(0,math.rad(90),0)
  26.     clone.Parent = workspace.Flock
  27.     physics:SetPartCollisionGroup(clone,'p')
  28.    
  29.     table.insert(parts,clone)
  30. end
  31.  
  32. local Speed = 20
  33.  
  34. for i = 1,#parts do
  35.     local v = parts[i]
  36.     local force = Instance.new("BodyVelocity")
  37.     force.MaxForce = Vector3.new(5e4,5e4,5e4)
  38.     force.Velocity = Vector3.new(0,0,0)
  39.     force.P = 1000 * Speed
  40.    
  41.     local gryo = Instance.new("BodyGyro")
  42.     gryo.MaxTorque = Vector3.new(5e4,5e4,5e4)
  43.    
  44.     gryo.Parent = v
  45.     force.Parent = v
  46. end
  47.  
  48. local MaxDist = 5
  49. local SeperationPriority = 2e8
  50. local CohestionProprity = 2
  51. local AllignmentProprity = 2
  52.  
  53.  
  54. function Seperationmth(vector)
  55.     return vector * SeperationPriority
  56. end
  57. function AverageDistancemath(vector,num)
  58.     return (vector / num) * CohestionProprity
  59. end
  60. function Allign(vector)
  61.     return (vector).Unit * AllignmentProprity
  62. end
  63. local function StartMovement()
  64.     for Boid = 1,#parts do
  65.         local v = parts[Boid]
  66.         local force = v.BodyVelocity
  67.         local gyro = v.BodyGyro
  68.        
  69.         local num = 0
  70.         local seperation = Vector3.new()
  71.         local avgDirection = Vector3.new()
  72.         local Allignment = Vector3.new()
  73.        
  74.         for i = 1,#parts do
  75.             local other = parts[i]
  76.             local mag = (v.Position - other.Position).Magnitude
  77.             if other ~= v and mag < MaxDist then
  78.                 num = num + 1
  79.                 seperation = seperation + ((v.Position-other.Position) * mag)
  80.                 avgDirection = avgDirection + other.Position
  81.                 Allignment = Allignment + other.Velocity
  82.             end
  83.         end
  84.        
  85.         force.Velocity = v.CFrame.LookVector * 8
  86.        
  87.         local NewPos = CFrame.new(v.Position,num ~= 0 and Seperationmth(seperation) + AverageDistancemath(avgDirection,num) + Allign(Allignment) or pos)
  88.         gyro.CFrame = NewPos
  89.     end
  90. end
  91. RunService.Heartbeat:Connect(StartMovement)
  92.  
RAW Paste Data