Custom physics simulation for Motor6D goes crazy on an inconsistent basis

Hello! Over the past year I have been developing a custom physics simulation for Roblox ski lifts that runs on Motor6Ds. It reacts to the lift changing speeds, chair occupancy, wind, etc.

I’ve been having a recurring issue where sometimes the physics simulation goes wack- sometimes when a Roblox minimized the chairs start swinging wildly. This is easily reproduced by holding down the close button on the application window and then releasing it again without closing the window (you might know this as the method to freezing your character mid-air).

Heres a video demonstrating the bug:

I’ve created a pendulum demo code to narrow this down to a more controlled environment, but the same strange bug occurs

Code:

local model = game:GetService("Workspace").PhysicsModel
local motor6d = model:WaitForChild("Root").Pendulum
local motor6dC1 = motor6d.C1
local angularSpeed = 1
local angularPosition = 0.5

--Step the physics sumulation--
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	local angularAcceleration = math.sin(math.rad(model.Pendulum.Orientation.Z))*10
	
	angularSpeed += angularAcceleration * deltaTime
	angularPosition += angularSpeed * deltaTime
	
	motor6d.C1 = motor6dC1 * CFrame.Angles(0,0,angularPosition)
end)

Video:

Heres the demo on roblox (uncopylocked): Physics Simulation Demo - Roblox

My best guess as to what is happening would be that this bug has something to do with how lua/roblox runs code- im really not experienced in this matter. I think it somehow applies acceleration and velocity without fetching the new angle after every frame, creating a massive increase in the speed of the pendulum as it would continuously apply extra speed based on an old/innacurate angle.

Any suggestions of whats happening here would be greatly appreciated. Thanks.

1 Like