Accessing the 240hz Physics Loop

I absolutely love coding physics, and in Roblox that is pretty easy hence you can easily apply forces and access inner game loops like Stepped,Heartbeat and RenderStepped.

Sometimes my custom physics don’t work properly because they are working on the Stepped loop, which fires 60 time a second. Don’t get me wrong, 60 times a second is very good, but for extremely accurate physics, it has to be more than just 60. As an example, any type of collision system would bug out if there was a part moving at 100 st/s towards a wall.
The part would just tunnel through the wall without the collision even getting detected. This can be easily fixed if the number of updates per second was increased.
From my knowledge the Roblox engine has an Inner Physics loop at runs at 240hz. About 2 months ago when I last checked, it could be accessed by having a while loop and a stepped:wait() function.

local step = game:GetService('RunService');
while true do
    local total,t = step.Stepped:Wait();
    print(1/t);
end

This used to print out 240, now however it prints 60. Which means that it has been changed in some way to clamp the number of updates per second to 60.

And now my question.
Is there any possible way to access or to fire code at the same rate as the Physics Loop? If so, please share it with me.

15 Likes

I believe Roblox has purposely limited this for performance… You are able to still use connectFirst to access the event at that speed but that requires identity level 3 while plain scripts have identity level 2. I don’t believe this is possible anymore in anything outside of plugins, the command bar, or CoreScripts in that case.

2 Likes

Not only is your code sample broken (you’re trying to call :Wait() on RunService itself), but I’m fairly certain you’ve never been able to run code at 240hz; Stepped has been 60 for as long as I remember. There certainly isn’t any way now.

He meant RunService.Stepped… It is still possible, I just tested it, but it’s only possible using connectFirst which requires identity 3. You can test this by running an empty baseplate and typing game:GetService(“RunService”).Stepped:connectFirst(print) and then printing 1 over the second value. This should output roughly 240.

1 Like

So then it’s not relevant–that’s not what the OP is asking for, they want to use it in actual scripts.

Physics are locked to 60 updates a second. That’s why FPS is locked to 60 FPS, etc etc etc.

There’s no way to reliably get 240 updates a second, and even if you did, any physics-related updates you did would mostly be pointless.

2 Likes

Why not just… run your code more times every frame but with a fixed dt of 1/240?

For example, if it’s been 1/60 seconds since the last frame, just run your physics simulation four times with a dt of 1/240

You’re probably referring to the Stepped event. This has always been run at every frame (60hz usually).

5 Likes

I am leaving information for newer users to this post in case they are looking for that information. I know that this is not what the OP is looking for and I’ve said I do not think this is possible anymore due to that fact.

@XAXA
Stepped has not always run at 1/60th of a second (a very very long time ago it did not run at 60hz, it ran at 240hz). Stepped can still even run at 240hz with connectFirst, but due to this being limited to connectFirst it can only run at 1/60th of a second in normal scripts.

1 Like

Umm, I don’t think that would work.

That’s like asking, “why doesn’t Roblox just run all 240 frames at the exact same time and then only run it every one second?”

It wouldn’t be anywhere near as smooth and basically just defeats the purpose.

FYI: if you look at the microprofiler, it is actually exactly how the engine does it: once per frame, it does 4 steps in quick succession (not 240Hz in smooth intervals, but bursts, once per frame). So what @XAXA is suggesting is very similar to what the engine does with these update steps. The amount of update steps also varies depending on performance or how many steps it has done in the previous frames.

9 Likes

There are ways to tackle your problem differently. Regarding collision for example, for each run of your 60hz loop, calculate interpolated positions for your part and check for collisions on each of them. Or you could check whether a part crossed another part during its “movement” throughout the last frame using some sort of casting.

2 Likes

If I were to do that, it would run 4 times slower. You can try it out youself.

Physics are not locked at 60hz. They run on a 240hz loop.
The loops that we are given access to run at 60hz maximum.

That is very inefficient due to having to loop through all of the triangles in every object and solve for the intersection of the triangle with the ray of every vertex

Physics used to run at 4000hz with the legacy solver, however, recently that solver was removed and replaced with the PGS solver, which is run at 240hz.

2 Likes

Oh, that makes sens why it went from 240hz to 60hz.

Fixed it

Not any slower than if you were simulating at 240 frames per second. In the end, the # of FLOPS doesn’t change. And as buildthomas said, this is essentially what the ROBLOX engine does.

It’s worth pointing out that ROBLOX is a real-time physics simulator, not a high-precision one. These two categories are to a large extent mutually exclusive, as the constraints of real-time means you only have so much processing power to work with. If computing power continues to increase exponentially, this might change to a degree, as we re-evaluate what “high-precision” means. But the same fundamental problem remains.

Add that to the fact that, for a multitude of reasons, ROBLOX Lua is slower than anything written on the C-side by ROBLOX’s programmers, and we’re left with a very limited frontier of real-time, high precision simulations that we can do. Don’t get me wrong; physics expertise is useful in a wide range of scenarios, ROBLOX included. But if you’re looking for “extremely accurate” physics, plenty of software exists to meet your needs more effectively than ROBLOX ever could.

That’s wrong. You’re getting the numbers confused. The legacy simulator was at 4000hz. The new one is at 240.

I think it would really help if you posted your simulation code, or at the very least your code’s outermost loop. And before dismissing my suggestion, I think you should really try it first. If your physics simulation is correct, then my suggestion should have made the simulation more accurate, not less.

3 Likes

I can assure you that my code is perfectly fine. If I simulate the 240hz physics engine inside the Stepped loop, it is VERY accurate, yet it is 4 times as slow of course.
Everything seems to be running is slowmotion. Aha

My code is a collision algorithm between a sphere and a cube, both of them are completely anchored.
Sometimes when I increase the Velocity of the sphere, if the update rate is at 60hz, the sphere would just tunnel through the Cube, which should’ve obviously never happened.
However when I simulate the same exact situation at 240hz, it works perfectly fine, the collision is detected and dealt with in the code.