# Needing assistance on building a sphere

I’m trying to build a planet, now by using fibonacci spheres. Vertices are placed, and got the triangle drawn out. However connecting the triangles to all of those points are something tricky I can’t get done with.

Here’s my attempt so far. Instead of a sphere, it makes this pattern.

I checked sources like Delaunay triangulation but I do not fully understand the source provided, I could try if there was additional information provided.

Source code:

``````local radius = 500

-- reference
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(5, 5, 5)

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

local triangles = {}
local function draw3dTriangle(selected)
local a = selected.Position
local b = selected.Position
local c = selected.Position

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 edge = bc

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;

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;

return w1, w2;
end

local function fibonacci_spiral_sphere(num_points)
local vectors = {}
local gr = (math.sqrt(5) + 1) / 2
local ga = (2 - gr) * (2 * math.pi)

for i = 1, num_points do
local lat = math.asin(-1 + 2 * i / (num_points + 1))
local lon = ga * i

local x = math.cos(lon) * math.cos(lat)
local y = math.sin(lon) * math.cos(lat)
local z = math.sin(lat)

table.insert(vectors, Vector3.new(x, y, z))
end

return vectors
end

local vec = fibonacci_spiral_sphere(1024)
for _, v in ipairs(vec) do
local part = part:Clone()
--table.insert(triangles,part)
part.Parent = workspace.Parts
--[[
if #triangles == 3 then
draw3dTriangle()
triangles = {}
end]]

end

wait(5)

-- this attempts to find the cloeset parts of all descendants
local PartsToFind = 2
for i,obj in pairs(workspace.Parts:GetChildren()) do

local dictionary = {}
for i, v in pairs(workspace.Parts:GetChildren()) do
if v:IsA("Part") and obj ~= v then
table.insert(dictionary,{(obj.Position - v.Position).Magnitude,v})
end
end

local array = {}
for _, value in pairs(dictionary) do
--print(value)
table.insert(array, tonumber(value))
end

table.sort(array)

local selected = {{0,obj}}
for i = 1, PartsToFind do
local key = array[i]
for i, v in pairs(dictionary) do

if v == key then
table.insert(selected,v)
end
end
end

draw3dTriangle(selected)

end
``````

It looks like the triangle function works but that you are passing it the wrong sets of points for the needed triangles, as evidenced by all the long skinny “longitudinal” triangles.

I’m not entirely sure why that causes it. I loop every vertices and find the two closest vertices, and make a triangle with those three points (one being part of the vertices for loop descendant, and the two being its surroundings)

That algorithm doesn’t sound right, the two closest vertices might still make a really thin triangle.

It appears you need this: Delaunay triangulation - Wikipedia

Alright so I decided to do more research and coding.

I also found an open sourced place on Polygon triangulation. I found out that the main problem is that the vertices of the spheres that I used were placed randomly instead of being close to each other.

I’m not sure whether I should rescript the vertices, or procedurally make another type of sphere, or leave it and find another solution.