Actively Changing Sun Position

This might be a bit difficult to explain and frankly I don’t know the math behind this at all so I tried using a plugin’s code to make this work, but my goal is to make it so the sun always appears behind a specific part for the player.

I imagined to do that I’d need to get the line between the two points and find a way to make the sun appear on that line so I’ll show what I attempted to do, but it’s so far off that this may be beyond just a touch up.

--local dir = (Services.ShipPosition.Position - game.Workspace.Map.Sun.Position).Unit
	local dir = CFrame.lookAt(Services.ShipPosition.Position, game.Workspace.Map.Sun.Position)
	local body = {
		Name = "Sun";
		LongitudeFactor = -1;
		Icon = "rbxassetid://1458865781";
	}

	local lf = body.LongitudeFactor
	local lat = Services.atan2(dir.Z, Services.sqrt(dir.X^2 + dir.Y^2))
	local lon = Services.atan2(dir.Y * lf, dir.X * lf)

	local geoLatitude = (lat / Services.tau) * 360 + 23.5
	print(geoLatitude)
	local clockTime = (lon / Services.tau) * 24 - 6

	Services.Lighting.GeographicLatitude = geoLatitude
	Services.Lighting.ClockTime = clockTime % 24

Someone had a similar idea and a good lead as to how it could be done which is what this is based on right now but without the working part:

local playerPos = Services.ShipPosition.Position
local sunPos = game.Workspace.Map.Sun.Position

local dir = (sunPos - playerPos).Unit
local latitude = math.deg(math.asin(dir.Y)) -- Latitude angle (in degrees)
local longitude = math.deg(math.atan2(dir.X, dir.Z)) -- Longitude angle (in degrees)

Services.Lighting.GeographicLatitude = latitude
Services.Lighting.ClockTime = (longitude / 360) * 24 % 24 -- Normalize to a 24-hour format
2 Likes

Hey thanks for replying. It ended up appearing on the opposite side but that might be because at night the sun doesn’t appear and the only visible light comes from the moon. My goal is to make it so the light always shines from the artificial sun so I guess in this case I need the moonlight to come from the artificial sun when clock time is night.

It also appeared to end on the opposite side of the position between me and the artificial sun.

I’m basically incapable of doing this kind of math. Is it possible to do what you were doing with the moon too?

Did you fix this? If not you can just inverse the math.

I would assume so

1 Like

Hey again, thanks I’m still not able to figure it out.

I don’t think the answer worked. If it were to work forward or backward I’d assume the sun/moon position should be where ever this line ends up. In the corner you can see the moon is no where near the line of the ray. https://gyazo.com/fe2bd2f57c8fefd9169e35c83bc7df42

Then this is the sun side: https://gyazo.com/fe2bd2f57c8fefd9169e35c83bc7df42

It doesn’t appear at any end of the line.

Hey I’ve been struggling on this subject still after the whole week and I’ve asked multiple people I know and still could not figure it out. If you have an alternative solution I’d appreciate it.