I am creating a spaceship system for a single player project where a group control a spaceship.
To allow for smooth gameplay I am attempting to make a movement system where instead of the spaceship moving around objects in space, the objects are moved around the spaceship, essentially creating the illusion of the spaceship moving.
I like the idea of this system, a main advantage is that players will be able to walk around the ‘moving’ ship smoothly without stuttering like they normally do on moving platforms, as the spaceship itself doesn’t physically move.
Objects moving relative to the ship (Ship is rotating on the Y axis, and is then moving forward):
https://gyazo.com/82c75016fa8a43085665f4dd8c48c5ed
The ship movement works perfectly when moving the objects around the ship on only the Y axis, however the system breaks once the objects have to rotate around the ship on the Z or X axis.
Objects not rotating correctly relative to the ship (Ship is rotating on the Z axis, and then moves foward)
https://gyazo.com/0db10f6fdbe2135c8b72ac42062becea
To create this movement system I am essentially orbiting objects physically around the static ship when the ship rotates or moves, through CFrame math. Due to the ship not moving physically, a virtual ship position and rotation value is calculated from the ship ‘velocity’ and ‘rotation velocity’. An offset between the object and ships position and rotation is calculated, and the object is then CFramed by these offsets to give it the illusion of moving around the ship.
local RotAcceleration = ShipInfo.RotAcceleration.Value
ShipInfo.Rotation.Value += Vector3.new(RotAcceleration.x, RotAcceleration.y, RotAcceleration.z)
local ShipRot = ShipInfo.Rotation.Value
local ShipPosition = ShipMain.CFrame * CFrame.Angles(math.rad(ShipRot.x), math.rad(ShipRot.y), math.rad(ShipRot.z))
local Acceleration = ShipInfo.Acceleration.Value
ShipInfo.Position.Value +=ShipPosition * CFrame.new(Vector3.new(Acceleration.x, Acceleration.y, Acceleration.z)).Position
for i,Object in pairs(workspace.Enviroment:GetChildren())do
local ObjectMain = Object.PrimaryPart
local ObjectInfo = Object.ObjectInfo
local function Process()
local Pos = ShipInfo.Position.Value
local Rot = ShipInfo.Rotation.Value
local PosOff = -(Pos - ObjectInfo.Position.Value)
local RotOff = -(Rot - ObjectInfo.Rotation.Value)
local OffsetCFrame = ShipMain.CFrame * CFrame.Angles(math.rad(RotOff.x), math.rad(RotOff.y), math.rad(RotOff.z)):ToObjectSpace() * CFrame.new(PosOff)
ObjectMain.CFrame = ShipMain.CFrame:ToObjectSpace(CFrame.Angles(math.rad(RotOff.x), math.rad(RotOff.y), math.rad(RotOff.z)) * CFrame.new(PosOff))
end
coroutine.wrap(Process)
Process()
wait(0.01)
end
If I had to guess what the issue was I would pin the blame to my CFrame math, I don’t think I’m calculating the rotation of the objects relative to the ship correctly, and after hours of trying to fix it, I’m still stuck.