I’m making a game where players can walk around on different planets and moons that are orbiting each other in real time, and to do this, I’m using VectorForces on physic parts and CFraming them on RunService:Stepped() to keep up with the planet, as well as using EgoMoose’s WallStick Controller to keep the player oriented the to the planet.
Everything is going pretty well, except when I try to sit in seat on a physic object, the object starts to wobble, and sometimes flings itself around or out into space:
robloxapp-20210212-2207373.wmv (2.5 MB)
I know the problem only occurs on a physics object I’m moving on RunService:Stepped, because everything works just fine when seated on an object that isn’t being CFramed even with the VectorForce and WallStick enabled.
I initially thought it was a problem with the part briefly intersecting with the planet during my :Stepped connection, but after modifying it so that it never intersects anymore, nothing changed.
This is the relevant code physics object in the :Stepped function, where Gravity is currently storing data on the planet’s orbit, with Gravity.Satellite is the planet itself:
local Descendents = MasterGravityManager.GetDescendents(Gravity.Satellite)
--Find relative position of each physics object
local DescendentsLocalPos = {}
for index,value in pairs(Descendents) do
DescendentsLocalPos[value] = GetPosition(value) - GetPosition(Gravity.Satellite)
end
--Current position of planet, new position, and change in position
local OrbitPos = GetPosition(Gravity.Satellite)
local NewOrbitPos = Gravity:OrbitalPosition(step) + GetPosition(Gravity.Parent)
local dp = NewOrbitPos - OrbitPos
--First physics object position correction for objects in front of the line of motion to prevent collisions
for index,value in pairs(Descendents) do
if(MasterGravityManager.GetData(value).Type == "PartGravity") then
if(dp:Dot(GetPosition(value)-OrbitPos) > 0) then
SetCFrame(value,CFrame.new(NewOrbitPos+DescendentsLocalPos[value])*CFrame.Angles(GetOrientation(value)))
end
end
end
--Move the planet
SetCFrame(Gravity.Satellite,CFrame.new(NewOrbitPos)*CFrame.Angles(GetOrientation(Gravity.Satellite)))
--Second physics object position correction to make sure everything is in the correct spot
for index,value in pairs(Descendents) do
if(MasterGravityManager.GetData(value).Type == "PartGravity") then
SetCFrame(value,CFrame.new(NewOrbitPos+DescendentsLocalPos[value])*CFrame.Angles(GetOrientation(value)))
end
end
GetPosition() and GetOrientation() are just general functions to make working with parts and models at the same time a lot easier.