Introduction
So I am currently working on a space game, and a big problem that I have run into is floating point error. As you know, space is massive, with planets being millions of miles apart. Obviously, this is impossible to replicate in ANY game engine, however, you can create the illusion that it is happening.The current system
The ship has two modes: normal and subspace. Normal allows the player to travel up to 450m/s, and subspace allows the ship to travel at up to 16 light seconds per second. As you can guess, the ship is not actually traveling at 16ls/s, it is more like 10,000m/s, but by making relatively âsmallâ things (such as space stations and other random parts) disappear when the player enters subspace, it creates the illusion of going extremely fast.
Now, planets are 50k to 100k studs away from 0,0. When the player is in Normal speed, the planets move away from the ship at the same speed as the ship is moving, which makes them think they arenât getting any closer at all, and when they enter subspace, the planets go back to their original position.
The problems
Let's say that the player has moved ~25,000 studs away from 0,0 in normal speed. That covers half the distance to a planet, but to the player, it looks like they haven't gotten any closer at all. But as soon as they enter subspace, the planet jumps back to its original position, which looks terrible.If the ship travels another ~15,000 studs towards the planet and then exits subspace into Normal, the planet will start to move away from the ship at the same sp eed at which it is moving, once again creating the illusion that the ship isnât getting any closer.
But if the ship travels another ~10,000 studs in Normal, it has technically reached the planet, however to the player, it doesnât look like it. And if the ship then enters subspace again, it will be inside the planet, which makes it look like the planet just disappeared from the playerâs pov.
An extension to this problem is the fact that the game will be multiplayer, and from other players' pov, it would also look like the player just went inside the planet.
Code/Videos
https://youtu.be/VIEAz2s_wYUMy code if anyone is interested:
local Bodies = {}
for i, v in pairs(game.Workspace.Bodies:GetChildren()) do
table.insert(Bodies, v)
end
local player = game.Players.LocalPlayer
local shipValues = require(script.Parent:WaitForChild("ShipValues")).shipValues
local userSettings = UserSettings().GameSettings
--How each quality level should effect the positioning of planetary bodies
local distanceConstantSettings = {
QualityLevel1 = 170;
QualityLevel2 = 340;
QualityLevel3 = 510;
QualityLevel4 = 625;
QualityLevel5 = 725;
QualityLevel6 = 170*6;
QualityLevel7 = 170*7;
QualityLevel8 = 170*8;
QualityLevel9 = 170*9;
QualityLevel10 = 170*10;
}
game:GetService("RunService").RenderStepped:connect(function()
for i,v in pairs(Bodies) do
local graphicsSetting = userSettings.SavedQualityLevel.Name
local distanceConstant = distanceConstantSettings[graphicsSetting]
if (v.Looped.Value == false or shipValues.subspace == true) then
v.CFrame = CFrame.new(workspace.CurrentCamera.CoordinateFrame.p+(v.OriginalPositionConst.Value-workspace.CurrentCamera.CoordinateFrame.p).unit*distanceConstant)
if (v.Looped.Value) == false then
v.FakeOriginalPosition.Value = v.Position
end
v.Looped.Value = true
local Constant = (v.OriginalPositionConst.Value-workspace.CurrentCamera.CoordinateFrame.p).magnitude/distanceConstant
v.Mesh.Scale = v.OriginalScale.Value/Constant
elseif (v.Looped.Value == true and shipValues.subspace == false) then
v.Position = Vector3.new(v.FakeOriginalPosition.Value.X + player.Character.HumanoidRootPart.Position.X, CFrame.new(workspace.CurrentCamera.CoordinateFrame.p+(v.OriginalPositionConst.Value-workspace.CurrentCamera.CoordinateFrame.p).unit*distanceConstant).Position.Y, v.FakeOriginalPosition.Value.Z + player.Character.HumanoidRootPart.Position.Z)
end
end
end)
I would greatly appreciate if anyone has a solution/idea on how to go about creating this planetary travel system.