luaphysicsrobloxlagluau

Roblox Physics Calculation Lag


In my roblox game, you use a rocket launcher to destroy parts, but when a large amount of parts are being calculated at the same time, it causes immense lag to all physics-based items (including required gameplay items such as a bomb). I've tried using SetNetworkOwner to assign items to the server, but that only made things much worse.

If there is no direct solution to lowering the lag, is there any way to group physics calculations to separate the core items from the destructible ones?

Code that causes parts to be flung:

part:BreakJoints()
part:ApplyImpulse(((part.Position - explosion.Position).Unit * BLAST_FORCE.Value * part:GetMass()))

Solution

  • While having many parts explode and get unanchored will always result in lag because of physics calculations, they are ways to improve the physics performance of your experience.

    Technically, you could just have the parts disappear, it takes away from the player's experience. Even with removing other factors like collision calculations it will suffer because of physics.

    1. One way you can improve the performance of physics is to use Adaptive Timestepping which improves performance of physics by changing the physics frequency of specific physics calculations.

      By default, Roblox simulates physics at 240 Hz. Given cycles of approximately 60 frames per second, around 4 worldsteps are advanced per frame. With adaptive timestepping, the physics engine automatically assigns parts to three "solver islands" by varying their simulation timestep, with an emphasis on 60 Hz for best performance. However, parts that are "harder" to solve will use a faster timestep like 240 Hz to ensure physical stability.

      It is also important to note that adaptive timestepping isn't always suitable if you want accuracy on your explosions or physics.

      Adaptive timestepping can improve physics performance by up to 2.5 times and it is recommended in most cases. However, some experiences should use Fixed mode (240 Hz), including:

      Experiences that require highly accurate simulations and stability, such as racing games, "destruction" simulations, or games featuring complex mechanisms like tanks.

    2. Another optimization you can perform is to set the FallenPartsDestroyHeight which dictates how low a part can fall being destroyed. This is especially helpful if they are a lot of parts falling off the map because the parts can be removed sooner resulting in less physics calculations and lag generally.

      This property determines the height at which the Roblox engine automatically removes falling BaseParts and their ancestor Models from Workspace by parenting them to nil. This is to prevent parts that have fallen off the map from continuing to fall forever.

    3. You can properly set Physics and Rendering Parameters for models, parts, and meshes in your experience to improve performace. Important properties include CanCollide, CanTouch, and CollisionFidelity. While they are more properties that impact performance, those are the most important regarding physics. If you want to learn more why those properties matter towards physics, click the links for the documentation reason why.

    4. For larger maps, it may be more suitable to use Instance Streaming to only show parts important to the player and not have them need to render some falling parts from across the map.

      In-experience instance streaming allows the Roblox engine to dynamically load and unload 3D content and related instances in regions of the world. This can improve the overall player experience in several ways.

    Essentially what streaming does is only load parts of the in-game world to the player as opposed to the entire world leading to improved performance due to not needing to render as much. Note that this is typically for bigger maps since it does take time to load a new region and if a player fires a rocket into an unloaded region, it leads to some problems.

    For more stuff about optimization I suggest you read Performance Optimization and Physic Computation Optimization which gives a very board overview on how to improve performance in your experience.

    The steps I've listed above are very general but provide you good resources on how to improve physics performance without needing to go deeper into your experience to find the root cause such as using the MicroProfiler to find which physics step causes the most lag and which assembly is causing that lag or using the Developer Console to visualize which physics are being computed a lot.

    I hope this explanation helps, if you want any questions or issues with my answer, feel free to comment it out and I'll edit my answer as needed or clarify in comments.