Advertisement
eea

gravitysimulationmoduel

eea
Mar 19th, 2022 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. local module = {}
  2. local ratio = math.sqrt(3) / math.sqrt(2)
  3. local e = 50 * ratio
  4. local f = 1000 * ratio
  5. function module:newParticle(x, y, z, mass, radius, locked)
  6.     local newparticle = {}
  7.     newparticle.pos = Vector3.new(x, y, z)
  8.     newparticle.vel = Vector3.new()
  9.     newparticle.acc = Vector3.new()
  10.     newparticle.mass = mass
  11.     newparticle.particle = Instance.new("SpawnLocation", script)
  12.     newparticle.particle.Anchored = true
  13.     newparticle.particle.Enabled = false
  14.     newparticle.particle.Position = newparticle.pos
  15.     newparticle.particle.Shape = "Ball"
  16.     newparticle.particle.Size = Vector3.new(radius*2, radius*2, radius*2)
  17.     newparticle.particle.Transparency = 1
  18.    
  19.     function newparticle:applyForce(force)
  20.         local applied_force = force/self.mass
  21.         self.acc += applied_force
  22.     end
  23.    
  24.     function newparticle:grav(other)
  25.         local dvector = self.pos - other.pos
  26.         local distance = dvector.Magnitude
  27.         local dsquared = math.clamp(distance*distance, e, f)
  28.         local g = 5
  29.         local GForcebot = g*self.mass*other.mass/dsquared -- lo
  30.         dvector = dvector * (1/dvector.Magnitude) * GForcebot
  31.         other:applyForce(dvector)
  32.     end
  33.    
  34.     function newparticle:updateMovements(other)
  35.         if not self.locked then
  36.             self:grav(other)
  37.             self.vel += self.acc
  38.             self.pos += self.vel
  39.             self.particle.Position = self.pos
  40.             self.acc = Vector3.new()
  41.         else
  42.             self:grav(other)
  43.         end
  44.     end
  45.    
  46.     function newparticle:visualize()
  47.         self.particle.Transparency = 0
  48.     end
  49.     return newparticle
  50. end
  51.  
  52. return module
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement