KrYn0MoRe

atomic physics

Mar 13th, 2021 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. --just an amateur trying to figure out how to do this lol
  2. local parts = Instance.new("Folder",script)
  3.  
  4. local subatomic_particles = {
  5.     neutron = {
  6.         mass = 1,
  7.         charge = 0,
  8.         color = Color3.new(0,1,1),
  9.         size = 1,
  10.     },
  11.     proton = {
  12.         mass = 1,
  13.         charge = 1,
  14.         color = Color3.new(1,0,0),
  15.         size = 1,
  16.     },
  17.     electron = {
  18.         mass = 1/1840, -- 0 or 1/1840
  19.         charge = -1,
  20.         color = Color3.new(0.3,0.3,0.3),
  21.         size = 0.1,
  22.     },
  23. }
  24.  
  25. local atoms = {
  26.     hydrogen = {
  27.         n = 1,
  28.         p = 1,
  29.         e = 1,
  30.     }
  31. }
  32.  
  33. function get_subatom(n)
  34.     return subatomic_particles[n]
  35. end
  36.  
  37. function get_atom(n)
  38.     return atoms[n]
  39. end
  40.  
  41. function create_subatomic_particle(t,pos)
  42.     local pdata = get_subatom(t)
  43.     if pdata then
  44.         local size = pdata.size
  45.         local p = Instance.new("Part")
  46.         p.Massless = true
  47.         --p.Mass = pdata.mass/1000
  48.         p.CanCollide = false
  49.         p.CastShadow = false
  50.         p.Anchored = true
  51.         p.Size = Vector3.new(size,size,size)
  52.         p.Shape = Enum.PartType.Ball
  53.         p.Color = pdata.color
  54.         p.Name = t
  55.         p.Position = pos+Vector3.new(math.random()*5,math.random()*5,math.random()*5)
  56.         p.Parent = parts
  57.     end
  58. end
  59.  
  60. function create_atom(t,pos)
  61.     local atom = get_atom(t)
  62.     local n,p,e = atom.n,atom.p,atom.e
  63.     for i = 1,n do
  64.         create_subatomic_particle('neutron',pos)
  65.     end
  66.     for i = 1,p do
  67.         create_subatomic_particle('proton',pos)
  68.     end
  69.     for i = 1,e do
  70.         create_subatomic_particle('electron',pos)
  71.     end
  72. end
  73.  
  74. function create_covalent_bond(a1,a2)
  75.    
  76. end
  77.  
  78. function create_ionic_bond(a1,a2)
  79.    
  80. end
  81.  
  82. create_atom('hydrogen',Vector3.new(50,10,-50))
  83.  
  84. function nearest_part(t,pos)
  85.     local dist = math.huge
  86.     local closest = nil
  87.     for _,p in pairs(parts:GetChildren()) do
  88.         local cdist = (pos-p.Position).Magnitude
  89.         if p.Name == t and dist > cdist then
  90.             dist = cdist
  91.             closest = p
  92.         end
  93.     end
  94.     return closest,dist
  95. end
  96.  
  97. function goto(a1,a2,dist)
  98.     local diff = a2.Position-a1.Position
  99.     game:GetService("TweenService"):Create(a1,TweenInfo.new(dist/10,Enum.EasingStyle.Bounce,Enum.EasingDirection.In),{
  100.         Position = a1.Position+(diff*math.random()*1.5)
  101.     }):Play()
  102. end
  103.  
  104. function update() -- move the things or something lol
  105.     for _,p in pairs(parts:GetChildren()) do
  106.         local pos = p.Position
  107.         if p.Name == 'proton' then
  108.             local closest,dist = nearest_part('neutron',pos)
  109.             if closest then
  110.                 goto(p,closest,dist)
  111.             end
  112.         elseif p.Name == 'neutron' then
  113.             local closest,dist = nearest_part('proton',pos)
  114.             if closest then
  115.                 goto(p,closest,dist)
  116.             end
  117.         elseif p.Name == 'electron' then
  118.             local closest,dist = nearest_part('proton',pos)
  119.             if closest then
  120.                 goto(p,closest,dist)
  121.             end
  122.         end
  123.     end
  124. end
  125.  
  126. while true do
  127.     update()
  128.     wait(0)
  129. end
Add Comment
Please, Sign In to add comment