Yup noticed that, it particularly glitches after 90 ish degrees
This is because Orientation is in LocalSpace.
When you rotate the X axis, the Z axis changes.
I thought we can fix that using the pivot point, but that doesn’t work either
Check this out:
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.PivotOffset = (planet.CFrame - planet.CFrame.Position):Inverse()
planet.Position = Vector3.new(camera.CFrame.Position.X, altitude, camera.CFrame.Position.Z)
end)
That’s the case. Is it still possible to make it world space? :ToWorldSpace() won’t work because I tried and it made the earth spin like crazy.
I just noticed that the Y coordinate changes at sometime in a distance at around 90 studs
it changes from 0 to 180 degrees automatically, idk why
Why are you trying to make this script if you can just use BodyAngularVelocity?
It’s fine if you want to rotate the planet according to the Position, but why are you taking a physical spot on the X,Y,Z axis (Position), and rotating the angles to that value (Orientation).
Position doesn’t have any Orientation values.
That might explain why it changes rotation when the XYZ Position gets close to 90 Studs, not 90 degrees.
I know BodyAngularVelocity uses world space, but it’s only for an unanchored part and it only rotates by angular velocity which is not what I want. What I want is to make it rotate using X and Z positions. I’m trying to make this script because I want to simulate the realistic orbit and it’s for my space game too.
I think this sounds impossible to make, but I still believe that it is possible. If Roblox had CFrame.Angles or orientation that uses world space and does not use angular velocity, that would be easier. If I find a solution, I sure will share the script too.
For who just see this. The topic has changed a little to help people understand easier.
Try doing this, this should avoid the gimbal lock problem as it uses CFrame multiplication in world space via the formula:
--rotation based on previous left CFrame multiplication term
--this case it's CFrame.new() which is world space
currentCFrame =CFrame.new()* rotationCFrame * currentCFrame--world space rotation
--as opposed to *= which is based on local space
--based on the CFrame of currentCFrame local axis
currentCFrame *= rotationCFrame
currentCFrame =currentCFrame* rotationCFrame
local planet = script.Planet
planet.Parent = workspace
local camera = workspace.CurrentCamera
local previousCameraCFrame = camera.CFrame
game:GetService("RunService").RenderStepped:Connect(function(dt)
local altitude = (camera.CFrame.Position.Y*0.1)
local XZChangeInPosition = (camera.CFrame.Position-previousCameraCFrame.Position)*Vector3.new(1,0,1)
previousCameraCFrame = camera.CFrame
local rotationCFrame = CFrame.fromOrientation(math.rad(-XZChangeInPosition.Z), 0, math.rad(XZChangeInPosition.X))
planet.CFrame = rotationCFrame*planet.CFrame
planet.Position = Vector3.new(camera.CFrame.Position.X, altitude, camera.CFrame.Position.Z)
end)
This solved the problem. Thank you so much!