Sphere Generation, "spherized" cube?

Hello fellow devs :wave:

Recently I’ve been researching the different ways to generate a sphere in Roblox using triangles.

I managed to generate quite a few spheres but I can’t solve a problem.
the distance between each “Dot” on the sphere isn’t equal, which generates a lot of triangles for nothing and slows down the game.

Is there any known method to generate a “perfect sphere” using triangles usable on Roblox?
if yes, could I get some hints?
I’m not asking for a source code or anything, just a general direction.

WARNING
This topic needs the knowledge in maths to be understood !

code

--Made by Dsioul

local globe = {}

local RADIUS = 40
local RESOLUTION = 100

local wedge = Instance.new("WedgePart");
wedge.Anchored = true;
wedge.TopSurface = Enum.SurfaceType.Smooth;
wedge.BottomSurface = Enum.SurfaceType.Smooth;

local function map(norm, MinCurrentRange, MaxCurrentRange, MinNewRange, MaxNewRange) --standart mapping function
	local res
	if MinNewRange == 0 or MinCurrentRange == 0 then
		if MaxNewRange == 0 or MaxCurrentRange == 0 then
			error("Error map function : Can't map with Currents Ranges")
		else
			res = (norm * MaxNewRange) / MaxCurrentRange
		end
	else
		res = (norm * MinNewRange) / MinCurrentRange
	end
	return res
end

local function draw3dTriangle(a, b, c) -- the functions to generate a 3D triangle from 3 dot
	local ab, ac, bc = b - a, c - a, c - b
	local abd, acd, bcd = ab:Dot(ab), ac:Dot(ac), bc:Dot(bc)

	if (abd > acd and abd > bcd) then
		c, a = a, c
	elseif (acd > bcd and acd > abd) then
		a, b = b, a
	end

	ab, ac, bc = b - a, c - a, c - b

	local right = ac:Cross(ab).unit
	local up = bc:Cross(right).unit
	local back = bc.unit

	local height = math.abs(ab:Dot(up))

	local w1 = wedge:Clone()
	w1.Size = Vector3.new(0, height, math.abs(ab:Dot(back)))
	w1.CFrame = CFrame.fromMatrix((a + b)/2, right, up, back)
	w1.Parent = workspace.Sphere
	
	local w2 = wedge:Clone()
	w2.Size = Vector3.new(0, height, math.abs(ac:Dot(back)))
	w2.CFrame = CFrame.fromMatrix((a + c)/2, -right, up, -back)
	w2.Parent = workspace.Sphere

	return w1, w2
end

local function GenerateSphere(radius, resolution)
	for i = 0, resolution + 1, 1 do
		local tableI = {}
		table.insert(globe,tableI)
		wait()
		local lat = map(i, 0, resolution + 1, (math.pi) * -1, math.pi)
		for j = 0, resolution + 1, 1 do
			local lon = map(j, 0, resolution, (math.pi * -1) * 2, math.pi * 2)
			local x = radius * math.sin(lon) * math.cos(lat)
			local y = radius * math.sin(lon) * math.sin(lat)
			local z = radius * math.cos(lon)
			--point(Vector3.new(x, y, z))
			table.insert(tableI, Vector3.new(x, y, z))
		end
	end
end

local function BuildSphere(globe)
	for i = 1, RESOLUTION + 1, 1 do
		wait()
		for j = 1, RESOLUTION, 1 do
			local a = globe[i][j]
			local b = globe[i+1][j]
			local c = globe[i][j+1]
			local d = globe[i+1][j+1]
			
			local wedgeA, wedgeB = draw3dTriangle(a, b, c)
			local wedgeC, wedgeD = draw3dTriangle(b, c, d)
		end
	end
end

GenerateSphere(RADIUS, RESOLUTION)
BuildSphere(globe)

I wish to find and optimized way to create such a “spherized” cube

2 Likes

Maybe the size of the Sphere affects the dots to be at unequal Positions from each other? Try changing the properties of the radius, Or in my opinion, I think it has something to do with Creating the Triangles on different axis, Maybe on some axis it generates the triangles unequal compared to the other axis. I hope it helps

the problem is the generation of the sphere in itself


In this picture, you can clearly see the problem
instead of being centred on the origin of the sphere, it’s centred on the poles.

1 Like

It looks like the sphere generates only one axis all the way along to the second sphere point. It’s probably caused by not generating the other axis or might be an error, Sorry if this information doesn’t help, I hope you’ll figure it out / find the proper solution.

1 Like

I’m not too sure if this is the error but I will try, thank you

1 Like

Well, it didn’t change much, just discovered I had some overlapping points. But thank you for your help

1 Like

Try generating the points with an equidistant plotting algorithm such as Fibonacci spheres

2 Likes

thank you very much for the assist !

1 Like


thanks to you, I managed to upgrade the sphere quite a bit.
Only need to render it now :+1:

1 Like