Hello fellow devs
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