Rotating The Earth With Camera Position

I’m making a 3D atmosphere script. It works like @Pulsarnova’s 3D atmosphere script. My current goal is to make it rotate to simulate that we’re orbiting around the earth, by using world space orientation from camera position (only X and Z). It is a fake earth to trick your eyes like you’re moving above the globe when you’re actually moving on a flat plane. I don’t use linear or angular velocity because that won’t get a proper position of the earth and it can mess up easily when a player freezes or teleports.

This is how it works.

Example by Pulsarnova’s earth script
https://cdn.discordapp.com/attachments/808543204893917264/851377654367780874/Action_6-6-2021_3-52-54_PM.mp4

See how it rotates by X and Z linear velocity. For my version, I want to use position movement instead of
linear or angular velocity.

The problem is, I don’t know how to make it rotate by using world space instead of local. You can see a video below for the issue. X orientation (red) is rotating fine, but when Z orientation (blue) tilts to 90ish degrees, it turns into Y orientation and rotates in wrong direction.

https://cdn.discordapp.com/attachments/799943430314393630/851334393540509708/Action_6-6-2021_2-38-10_PM.mp4

So, the main problem is Z orientation is not rotating properly after X rotates to 90ish degrees. Notice the difference at 0:09 and 0:22.

Here’s my earth rotation example script. Put it in ReplicatedFirst.

Earth Example.rbxm (16.6 KB)

Also here’s the code if you would like to mess around.

local planet = -- Your planet inside this local script
planet.Parent = workspace
local camera = workspace.CurrentCamera
game:GetService("RunService").RenderStepped:Connect(function()
	local altitude = (camera.CFrame.Position.Y*0.1)
	local xPosition = camera.CFrame.Position.X
	local zPosition = -camera.CFrame.Position.Z
	planet.Orientation = Vector3.new(zPosition, 0, xPosition)
	planet.Position = Vector3.new(camera.CFrame.Position.X, altitude, camera.CFrame.Position.Z)
end)

I’ve been searching for other forums and none of them worked for me. I asked my friend for help and it didn’t work either.

Let me know if you find a solution. Thanks in advance!

4 Likes

The solution would to change the Axis. There are X, Y, Z. Try this script, and try fiddling with all the X, Y, and Z’s in the code until you get it right!

local camera = workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()
	local altitude = (camera.CFrame.Position.X*0.1)
	local xPosition = camera.CFrame.Position.X
	local zPosition = -camera.CFrame.Position.Z
	planetSurface.Orientation = Vector3.new(zPosition, 0, xPosition)
	planetSurface.Position = Vector3.new(camera.CFrame.Position.X, altitude, 
    camera.CFrame.Position.Z)
end)
1 Like

I don’t know what are changed in your script. Let me quote the exact code that I’m struggling with.

local xPosition = camera.CFrame.Position.X -- Get position from X axis
local zPosition = -camera.CFrame.Position.Z -- Get position from Z axis
planetSurface.Orientation = Vector3.new(zPosition, 0, xPosition) -- This is the code that makes the earth rotate

The X orientation (where zPosition located) works fine, even it’s tilted to another angle by Z orientation. The problem is, Z orientation (where xPosition located) changes its rotation from vertical to horizontal like Y orientation because of X orientation caused it. So, I need to solve the Z orientation. Sorry for bad grammar.

It isn’t because in this line:
planetSurface.Orientation = Vector3.new(zPosition, 0, xPosition)
you have the zPosition and xPosition reversed?

1 Like

zPosition needs to be in negative amount according to Roblox movement. -Z means forward, +X means right.

Try to use the tween service, maybe it will fix?

1 Like

If even thought I’m not well good in script but check the “local xPosition = camera.CFrame.Position”. also with the Vector3.new(zPosition, 0, xPosition)
planetSurface.Position = Vector3.new(camera.CFrame.Position.X, altitude,
camera.CFrame.Position.Z). Because the are connected to the rotation.

1 Like

But aren’t you getting the Position coordinates (xPosition & yPosition) and setting planetSurface.Orientation to that Position instead of its Orientation?

1 Like

The earth follows the camera. I use position to rotate it because that’s how a spacecraft orbits around the earth. It’s an illusion to trick your eyes that you’re orbiting around the globe when you’re actually moving striaght in any of these 2 axis in flat world.

This is how Pularnova’s earth rotation script works. It rotates to the direction where X or Z velocity go. For my version, I use position movement instead of linear velocity like Pulsarnova’s.

https://cdn.discordapp.com/attachments/808543204893917264/851377654367780874/Action_6-6-2021_3-52-54_PM.mp4

You can try his script here.

I’m sorry, but I don’t get what you’re trying to achieve here, I may sound dumb, but can you explain further?

1 Like

Also I noticed that your script doesn’t depend on time, but on framerate, which means the behavior of the rotation varies from device to device

I’d suggest using tick() along with it!

1 Like

I’ve edited the post. Sorry for bad grammar.

Why are you inputting the position into the orientation?

2 Likes

It’s local script. It gets client’s camera position (only X and Z) to rotate the earth. So, if you move 100 studs from 0,0,0 in X direction, that means X position will be 100 studs and the earth will rotate in Z orientation by that amount.

To simulate that you’re orbiting around the earth, like when you go from north pole to south pole. I don’t want to use velocity because that won’t get proper position and it can mess up the time like what happened to Pulsarnova’s script.

OHHHH, so you want to fake the player into believing that we are going around a round earth while moving on a flat plane?

1 Like

Exactly! Just like how Pulsarnova’s 3D atmosphere works.

I thought it’ll work if we modulate it by 360, but it still bugs at some places, not sure why-

Here’s the script so far:

local planet = script.Planet
planet.Parent = workspace
local camera = workspace.CurrentCamera
game:GetService("RunService").RenderStepped:Connect(function()
	local altitude = (camera.CFrame.Position.Y*0.1)
	local xPosition = (camera.CFrame.Position.X)%360
	local zPosition = (camera.CFrame.Position.Z)%360
	planet.Orientation = Vector3.new(-zPosition, 0, xPosition)
	planet.Position = Vector3.new(camera.CFrame.Position.X, altitude, camera.CFrame.Position.Z)
end)
1 Like

It’s still the same. See the Z orientation (blue) flipped to Y (green) after X (red) tilted by 90 degrees. The weirdest part is X is also tilted, but it still works properly.

https://cdn.discordapp.com/attachments/799943430314393630/851394952122466324/Action_6-7-2021_4-36-37_PM.mp4