Advertisement
Guest User

Untitled

a guest
Mar 25th, 2020
2,908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. -- Constants
  2.  
  3. local ITERATIONS    = 8
  4.  
  5. -- Module
  6.  
  7. local SPRING    = {}
  8.  
  9. -- Functions
  10.  
  11. function SPRING.create(self, mass, force, damping, speed)
  12.     local spring    = {
  13.         Target      = Vector3.new();
  14.         Position    = Vector3.new();
  15.         Velocity    = Vector3.new();
  16.        
  17.         Mass        = mass or 5;
  18.         Force       = force or 50;
  19.         Damping     = damping or 4;
  20.         Speed       = speed  or 4;
  21.     }
  22.    
  23.     function spring.shove(self, force)
  24.         local x, y, z   = force.X, force.Y, force.Z
  25.         if x ~= x or x == math.huge or x == -math.huge then
  26.             x   = 0
  27.         end
  28.         if y ~= y or y == math.huge or y == -math.huge then
  29.             y   = 0
  30.         end
  31.         if z ~= z or z == math.huge or z == -math.huge then
  32.             z   = 0
  33.         end
  34.         self.Velocity   = self.Velocity + Vector3.new(x, y, z)
  35.     end
  36.    
  37.     function spring.update(self, dt)
  38.         local scaledDeltaTime = math.min(dt,1) * self.Speed / ITERATIONS
  39.        
  40.         for i = 1, ITERATIONS do
  41.             local force         = self.Target - self.Position
  42.             local acceleration  = (force * self.Force) / self.Mass
  43.            
  44.             acceleration        = acceleration - self.Velocity * self.Damping
  45.            
  46.             self.Velocity   = self.Velocity + acceleration * scaledDeltaTime
  47.             self.Position   = self.Position + self.Velocity * scaledDeltaTime
  48.         end
  49.        
  50.         return self.Position
  51.     end
  52.    
  53.     return spring
  54. end
  55.  
  56. -- Return
  57.  
  58. return SPRING
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement