Gravitational Potential Fractal

So I saw this video and decided to recreate:
https://www.youtube.com/watch?v=3jJ51bj1jTM&ab_channel=prossello

These are the results:




This is the code:

local GRID_SIZE = 1

local WORLD_CENTER = Vector3.zero
local FRACTAL_RADIUS = 80

local FRACTAL = {}
local PULLED_OBJECT_MASS = 100

local PULLER_1 = workspace:WaitForChild('pullers')['1']
local PULLER_2 = workspace:WaitForChild('pullers')['2']
local PULLER_3 = workspace:WaitForChild('pullers')['3']
local PULLER_4 = workspace:WaitForChild('pullers')['4']

local PULLER_SIZE = 8

local PULLERS_TABLE = {
	{
		instance = PULLER_1,
		position = Vector2.new(PULLER_1.Position.X, PULLER_1.Position.Z),
		velocity = Vector2.zero,
		mass = 100
	},

	{
		instance = PULLER_2,
		position = Vector2.new(PULLER_2.Position.X, PULLER_2.Position.Z),
		velocity = Vector2.zero,
		mass = 100
	},

	{
		instance = PULLER_3,
		position = Vector2.new(PULLER_3.Position.X, PULLER_3.Position.Z),
		velocity = Vector2.zero,
		mass = 100
	},

	{
		instance = PULLER_4,
		position = Vector2.new(PULLER_4.Position.X, PULLER_4.Position.Z),
		velocity = Vector2.zero,
		mass = 100
	},
}

local G = 6.6743 * 10^-4

--------------------------------------------------------

local fractalModel = Instance.new('Model')
fractalModel.Name = 'Fractal'

print('Creating grid.')

local a = os.clock()

for x = WORLD_CENTER.X - FRACTAL_RADIUS, WORLD_CENTER.X + FRACTAL_RADIUS do

	FRACTAL[x] = {}

	for z = WORLD_CENTER.Z - FRACTAL_RADIUS, WORLD_CENTER.Z + FRACTAL_RADIUS do

		local real_x, real_z = x * GRID_SIZE, z * GRID_SIZE
		local realCellPosition = Vector3.new(real_x, WORLD_CENTER.Y, real_z)

		local cellPart = Instance.new('Part')
		cellPart.Anchored = true
		cellPart.Size = Vector3.one * GRID_SIZE * Vector3.new(1, 0, 1)
		cellPart.Position = realCellPosition
		cellPart.Name = `{x}_{z}`
		cellPart.Parent = fractalModel

		FRACTAL[x][z] = {cellPart}

	end

	if x % 2 == 0 then task.wait() end
end

print('Grid creation done.', os.clock() - a)

fractalModel.Parent = workspace

--------------------------------------------------------

task.wait(1)

print('Calculating image.')

local b = os.clock()

for x = WORLD_CENTER.X - FRACTAL_RADIUS, WORLD_CENTER.X + FRACTAL_RADIUS do
	for z = WORLD_CENTER.Z - FRACTAL_RADIUS, WORLD_CENTER.Z + FRACTAL_RADIUS do

		local real_x, real_z = x * GRID_SIZE, z * GRID_SIZE
		local realCellPosition = Vector2.new(real_x, real_z)

		local pulled_object = {
			position = realCellPosition,
			velocity = Vector2.zero,
			mass = PULLED_OBJECT_MASS
		}

		local function updateVelocityIteration()
			for puller_n = 1, 4 do
				local puller = PULLERS_TABLE[puller_n]
				local r = (puller.position - pulled_object.position).Magnitude
				
				if r <= PULLER_SIZE then return puller_n end -- Intersected with a puller.
				
				local m1, m2 = pulled_object.mass, puller.mass
				local F = G * (m1 * m2) / (r*r)

				local pulled_to_puller_direction = (pulled_object.position - puller.position).Unit * -1
				local deltaVelocity = pulled_to_puller_direction * F

				pulled_object.velocity += deltaVelocity
			end
			
			pulled_object.position += pulled_object.velocity
		end
		
		local intersectedPuller_n = nil
		local maximumIteration = 10000
		
		for i = 1, maximumIteration do
			if i % 1000 == 0 then task.wait() end
			intersectedPuller_n = updateVelocityIteration()
			
			if intersectedPuller_n then
				break
			end
		end
		
		FRACTAL[x][z][2] = intersectedPuller_n
	end
end

print('Calculation done.', os.clock() - b)

print('Rendering image.')

local c = os.clock()

for x = WORLD_CENTER.X - FRACTAL_RADIUS, WORLD_CENTER.X + FRACTAL_RADIUS do
	for z = WORLD_CENTER.Z - FRACTAL_RADIUS, WORLD_CENTER.Z + FRACTAL_RADIUS do
		local intersectedPuller_n = FRACTAL[x][z][2]
		local cellPart = FRACTAL[x][z][1]
		local puller = PULLERS_TABLE[intersectedPuller_n]
		
		cellPart.Color = puller and puller.instance.Color or Color3.new(0, 0, 0) 
	end
	
	task.wait()
end

print('Finished rendering image.', os.clock() - c)

How readable is it?
How could I improve the performance?
What improvements can there be made?

1 Like