How to calculate the angle of a part relative to a sphere

So I have to calculate a wedge’s angle relative to a sphere like seen in the image below:

This is meant to know if triangles that make up mountains and relief inside a planet are too tilted to have grass or snow on them

2 Likes

image

Would this diagram be able to help you out? If not, I do have one more solution for you, however, this seems close to what you are trying to achieve.

1 Like

I don’t think so, see. my code generates planets using two for loops that generate a noised sphere.

Terrain is generated with wedges, one square = 2 wedges and I have to know how tilted is every wedge to know if it can have grass or other things

1 Like

Well then, you can use the tangent, so as the mountain is made up of wedges, use the closest edge of the wedge to the circle, then calculate the angle from that tangent to the furthest point of the wedge. Here is an example below.

In the example below, we have a mountain, which is red and uneven, like a normal mountain, the greenish part is the part we want the angle of, so we get the closest point to the circle, and draw a straight line to the middle of the circle, then we can use the tangent we found to find the angle of the furthest point.

1 Like

I’ll give this a try and tell you if it worked

Please refer to my edit, I forgot to mention, that to get the actual tangent and an accurate angle, you need to draw a straight line from the closest point to the MIDDLE of the circle, not the edge.

1 Like

oop, I’ll see if it works cuz another problem is script exhaustion when a lot of math is performed as planets are huge and are generated within seconds

1 Like

I have done something like this, try this:

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
		

		-- Rotate the part to face towards the center of the sphere
		
		part.CFrame = CFrame.lookAt(part.Position, center) 
		
	end
end
while true do
	wait(20)
	spawnPartsAroundSphere(script.Parent, 5)
end

You should avoid generating such a huge terrain in a quick time, I suggest using procedural generation, this way you can do much more operations on small parts of the terrain at a time, while the planet slowly gets generated.

there’s a problem with that code as it performs just too many operations and will exhaust the script

the problem here is that it might take some hours so yeah, Imma try to figure something out, btw this is a planet:

1 Like

I mean yeah, if you’re running a script on a lot of planets ofcourse, it would lag, but it won’t be for long and only for the first person that joins the server

1 Like

The problem isn’t lag as that isn’t even a problem now, the problem is the codes exhaust as they’re pretty much just a bunch of for loops working together

well… you could always run the code and make a planet, copy the planet and make like a “random libary” of it and spawn it in, not the best solution, but a solution

1 Like

too complicated due to my rendering system and it literally havind tenths of thousands of assets

yeah i’m not sure, the best i can come up with is use streamingenabled or mesh-ify all the modeles you can to decrease the object count for the player

1 Like

streaming enabled doesn’t work so I had to make my own rendering systems, performance issues are fixed already

Aight so I was able to fix this using some tricky math.

I found the lowest and highest point of every wedge and found the distance between them subtracting their positions and then knowing the resulting vector’s magnitude.

Then I calculated the change in height from lowest to heighest and divided it to know the slope percentage.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.