Help with part placement around sphere

i am experimenting with a part placement script, it works alright so far, here is what the script looks like so far:

local function randomPointOnSphere(center, radius)
	local randomX = (math.random()*2)-1
	local randomY = (math.random()*2)-1
	local randomZ = (math.random()*2)-1

	local randomVector = Vector3.new(randomX, randomY, randomZ)
	local randomDirection = randomVector.Unit
	local randomDirectionWithRadius = randomDirection * radius
	local randomPointOnSurface = center + randomDirectionWithRadius

	-- Adjust the randomPointOnSurface to prevent clipping inside the planet
	local distanceToSurface = (randomPointOnSurface - center).Magnitude - radius
	randomPointOnSurface = randomPointOnSurface - randomDirectionWithRadius.Unit * distanceToSurface

	return randomPointOnSurface, randomDirection -- Return the random direction as well
end

local function spawnPartsAroundSphere(spherePart, numParts)
	local radius = spherePart.Size.X / 2
	local center = spherePart.Position

	for i = 1, numParts do
		local randomPoint, randomDirection = randomPointOnSphere(center, radius) -- Get the random direction from the randomPointOnSphere function

		local part = game.ReplicatedStorage.Core:Clone()
		part.Position = randomPoint
		part.Parent = workspace
		part.Anchored = true

		-- Rotate the part to face towards the center of the sphere
		
		part.CFrame = CFrame.lookAt(part.Position, center) 
		
	end
end

spawnPartsAroundSphere(game.Workspace.Planet, 1000)



but some parts are inside the sphere more than others, i am a bit confused, how would i solved this? so far i tried upvector, which produced a very odd result.

lookvector -10 looks fine right?

its like the whole sphere cubes got offset uniformally

2 Likes

This is because the ‘‘Sphere’’ is actually made of triangles with the corners of the triangles placed on a sphere this means that your cubes will sometimes float above the sphere.

Basically the ‘‘sphere’’ isn’t a sphere it is some triangle made to look like a sphere.

Best solution would be to make the ‘‘sphere’’ a union and ray cast to place the cubes on its surface.

The reason to union the ‘‘sphere’’ is so it doesn’t have the collision of an actual sphere.

is there a way to make the sphere has more sides? or do i need to use a mesh (i dont trust mesh hitboxes)

maybe i should put the cubes inside the sphere and let collisions do its job and hopefully clip to the surface?

wdym? Unanchor the cubes I said to ray cast onto the sphere to place the cubes on the spheres collision.

Firstly unioning the sphere to make the sphere’s collision be what you visually see.

ununioning? wdym by that? i dont think the sphere is unioned

Union not ununion.
Then raycast from above the sphere’s surface to project the cubes onto the sphere.

Have a look at the Meshes section of your Toolbox. someone may have uploaded a Sphere or Ball that has more tris (faces) than the standard Roblox Ball or Union.
Sometimes the creator of the Mesh will state in the description how many faces or tris there are. You can also zoom in on the view image to see if it has more or less flat faces.