Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local part = Instance.new("Part")
- part.Size = Vector3.new(10,10,1)
- part.Position = Vector3.new(0,10,20)
- part.Anchored = true
- part.CanCollide = false
- part.Parent = script
- local surface = Instance.new("SurfaceGui")
- surface.SizingMode = Enum.SurfaceGuiSizingMode.PixelsPerStud
- surface.PixelsPerStud = 50
- surface.Parent = part
- local main = Instance.new("Frame")
- main.Size = UDim2.new(1,0,1,0)
- main.BorderSizePixel = 0
- main.BackgroundColor3 = Color3.new(0,0,0)
- main.ClipsDescendants = true
- main.Parent = surface
- local particle_list = {}
- local groups = {}
- local particle_data = {}
- local scale = 10 -- size and velocity scaling
- local mas = main.AbsoluteSize
- function create_particle(x,y,mass)
- x = x or 0
- y = y or 0
- local p = Instance.new("Frame")
- p.Size = UDim2.new(0,scale,0,scale)
- p.Position = UDim2.new(0,mas.X/2,0,mas.Y/2)+UDim2.new(0,x*scale,0,y*scale)
- p.BackgroundColor3 = Color3.new(1,1,1)
- p.Name = #particle_list+1
- p.BorderSizePixel = 0
- p.Parent = main
- particle_data[p] = {}
- particle_data[p].force = Vector2.new(0,0)
- particle_data[p].position = Vector2.new(p.Position.X.Offset,p.Position.Y.Offset)
- particle_data[p].mass = mass or 1
- table.insert(particle_list,p)
- end
- function update_divs(divs)
- for _,div in pairs(divs) do
- for x = 0,div do
- groups[div][x] = {}
- for y = 0,div do
- groups[div][x][y] = 0
- local visualize = false
- if visualize then
- local p = Instance.new("Frame")
- p.Size = UDim2.new(3,0,0,1)
- p.Position = UDim2.new(0,mas.X/div*x,0,mas.Y/div*y)
- p.BackgroundColor3 = Color3.new(1,1,1)
- p.BackgroundTransparency = .9
- p.BorderSizePixel = 0
- p.Parent = main
- local p = Instance.new("Frame")
- p.Size = UDim2.new(0,1,3,0)
- p.Position = UDim2.new(0,mas.X/div*x,0,mas.Y/div*y)
- p.BackgroundColor3 = Color3.new(1,1,1)
- p.BackgroundTransparency = .9
- p.BorderSizePixel = 0
- p.Parent = main
- end
- end
- end
- end
- for i,v in ipairs(particle_list) do -- first establish group population
- local pos = v.Position
- for _,div in pairs(divs) do
- local x = math.floor(pos.X.Offset/mas.X*div)
- local y = math.floor(pos.Y.Offset/mas.Y*div)
- if div > x and div > y and x > 0 and y > 0 then
- groups[div][x][y] += 1 -- increase population in group area
- end
- end
- end
- end
- function update_groups()
- local divs = {60,40,20}
- local total_div = 0
- for _,div in pairs(divs) do
- total_div += div
- groups[div] = {}
- end
- update_divs(divs)
- for i,v in ipairs(particle_list) do -- update colors
- local pos = v.Position
- local pop = 0
- for _,div in pairs(divs) do
- local x = math.floor(pos.X.Offset/mas.X*div)
- local y = math.floor(pos.Y.Offset/mas.Y*div)
- if div > x and div > y and x > 0 and y > 0 then
- local divpop = groups[div][x][y]
- pop += divpop^2/(mas.X/div)
- end
- end
- local radiance = pop
- v.BackgroundColor3 = Color3.new(radiance,radiance,radiance)
- --v.BackgroundColor3 = Color3.new(0.4+radiance*0.4,0.8-radiance*0.4,0.8-radiance*0.4)
- end
- --[[ -- supposed to update gravity with large amounts of particles but im lazy to add
- for _,p in ipairs(particle_list) do -- update gravity
- local pos = p.Position
- local force = 0
- for _,div in pairs(divs) do
- local x = math.floor(pos.X.Offset/mas.X*div)
- local y = math.floor(pos.Y.Offset/mas.Y*div)
- if div > x and div > y and x > 0 and y > 0 then
- local divpop = groups[div][x][y]
- force += divpop/(mas.X/div) -- all the particles have mass of 1, no extra steps
- end
- end
- local p_vec = Vector2.new(p.Position.X.Offset,p.Position.Y.Offset)
- local new_vec = p_vec+force
- p.Position = UDim2.new(0,new_vec.X,0,new_vec.Y)
- end
- ]]
- end
- function update_particle(p)
- local G = 6.67408 * 10^-4--1/1000
- for i,v in ipairs(particle_list) do
- if v ~= p then
- local m1 = particle_data[p].mass
- local m2 = particle_data[v].mass
- local p_vec = particle_data[p].position
- local v_vec = particle_data[v].position
- local force_vec = v_vec-p_vec
- local r = force_vec.Magnitude
- local F = G*(m1*m2)/r^2
- local A = F/m1
- if r > 0 then
- force_vec = force_vec*A
- end
- force_vec = particle_data[p].force+force_vec
- particle_data[p].force = force_vec
- local new_vec = p_vec+force_vec
- particle_data[p].position = new_vec
- --p.Position = UDim2.new(0,new_vec.X,0,new_vec.Y)
- end
- end
- end
- function update_sim()
- for _,p in ipairs(particle_list) do
- update_particle(p)
- end
- for _,p in ipairs(particle_list) do
- local new_vec = particle_data[p].position
- p.Position = UDim2.new(0,new_vec.X,0,new_vec.Y)
- end
- update_groups()
- end
- local mx = 10
- local my = 10
- for x = 1,mx do
- for y = 1,my do
- create_particle((-mx*2/2)+x*2,(-my*2/2)+y*2)
- end
- end
- update_groups()
- task.wait(3)
- while true do
- update_sim()
- task.wait()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement