Physics Optimization
Computation of physical interactions can be very demanding. There are optimization techniques providing high performance and overall stability for your interactive projects.
Common Settings#
Keep the global physics settings at a reasonable demanding level.
-
Specify the Distance from the camera, from which physical interactions are not calculated. If the distance between the camera and the physics-based node exceeds this limit, the node freezes and its physical calculations are skipped until the node gets closer to the camera.
Cloth simulation halts starting from the specified distance from the camera
- Using the Iterations parameter may increase stability, but can also result in a higher load. A high number of iterations may lead to noticeable lags as very time-consuming calculations are automatically skipped. One physics iteration is quite enough for simple scenes.
- Adjust the time Budget for Physics simulation.
Collision Detection Optimization#
Filtering Interactions#
It is very unlikely that each physics-based object in your project will interact with all other objects. Most probably, objects can be divided into groups dedicated to certain tasks.
If you have multiple objects that take part in physical simulation, divide the ones that are hardly going to interact with each other, into different "groups" by using the Bit Masking mechanism.
Limiting Collisions#
Set matching Collision Masks for those particular shapes, surfaces, and bodies that are expected to collide. If the masks do not match, shapes and bodies will simply ignore each other by going through.
Collisions are calculated for a plane and a sphere while a box is ignored due to unmatching Collision Masks
|
The Collision Mask of the sphere's physical shape
|
The Collision Mask of the box's physical shape
|
The Collision Mask of the plane's surface
|
The Collision Mask of the ground's surface
|
In Unigine, you can set up filtering accurately by using the Exclusion Mask. This mask allows ignoring shapes with the matching exclusion masks regardless of their Collision Masks.
Surfaces that are not intended for physical interactions should have the Collision flag disabled. For example, a dashboard inside a cockpit would never take part in collision detection on the outside.
Avoiding detection of undesired and unnecessary collisions is a basic way to reduce the amount of physical calculations.
Filtering Physical Effects#
Specify certain physicals that should affect only corresponding bodies by using the Physical Masks the same way as for collisions. On the image below, a Physical Wind object affects a cloth banner but not a sphere.
A physical wind affects a cloth but not a sphere |
Physical mask of the cloth banner
|
Physical mask of the sphere
|
Physical mask of the Physical Wind object
|
Intersection Mask#
Intersection is a generic point of a specified area (or line) and an object. Calculation of intersection between a ray cast and a surface is fast and inexpensive and therefore sometimes can be used instead of computing collision. For example, calculation of collisions of car wheels with the ground can be simplified by using intersections.
Although it is a programmatic approach only, it requires Intersection Masks to be set for filtering purposes.
Reducing Colliders Complexity#
As a rule, most 3D objects should be represented by a complex and detailed visible mesh and an invisible simplified shape used by a physics engine for collision detection (collider).
Detailed visible mesh
|
Invisible physical representation by using simplified shapes
|
Using Simpler Shapes#
Physical shapes consisting of simple primitives make collision calculations easier while keeping performance high and accuracy acceptable. Therefore, it is recommended to approximate geometry with simple primitives (Sphere, Capsule, Cylinder, Box), which are faster and less memory demanding.
Complex shapes, such as Convex Hull and a set of autogenerated convex hulls, should be selected for representing complex and composite geometry when simple shapes don't allow achieving the desired goals.
At the same time you should keep the number of shapes as low as possible in order to avoid heavy calculations leading to the performance drop. Remember that a shape doesn't have to duplicate the mesh it approximates. Even though primitives are not precise, in most cases they provide acceptable results.
Using Simpler Geometry#
The simpler geometry is used for surface collisions detection, the more performance will be gained.
If a mesh representing a physical obstacle has several levels of detail (LODs), the least detailed one is the most preferable for collision and intersection detection. Enable the Collision and Intersection options for this LOD and disable them for the other ones.
Approximating collision shapes by using primitives is also a good practice. You can apply the following approach:
-
Ensure that the original node is not a Collider — check that Collision flag is disabled for its surfaces, bodies, and shapes. Since now it is not taken into account by collision detection.
-
Add a cylinder object to the scene by choosing Create -> Primitive -> Cylinder in the Menu Bar and cover the original mesh with the primitive.
- Make sure that the Collision option of the surface is enabled for the primitive node.
-
Hide the visual representation of the primitive's surface. You can do it either by clearing its Viewport and Shadow masks or simply by setting its Max Visibility parameter to the negative infinity (-inf) — this ensures that the surface is not visible at any distance.
Now the detailed mesh provides only visual representation, while physical interactions are calculated for the primitive cylinder.
Collision Detection Approach#
Continuous Collision Detection is a very demanding operation, therefore, it is recommended to use this approach only for fast-moving objects and objects requiring precise calculations.
In most cases, Discrete Collision Detection would be enough. For this purpose, select a shape and uncheck the Continuous flag for it.
Optimizing Physical Bodies#
Freezing#
It is recommended to enable Freezing to all Rigid, Ragdoll, and Fracturebodies. This allows saving a great deal of computational resources by skipping physical calculations of immobile objects until they are affected by any force or object.
To enable freezing, perform the following steps:
- Enable the Freezable flag for all types of physical bodies listed above.
- Set appropriate values of the Frozen Linear Velocity and Frozen Angular Velocity parameters for each physical body or adjust the global parameters. These global freezing thresholds are compared to the ones set for each body, and the highest value is chosen to freeze the body.
- Adjust the number of Frozen Frames. The lower the value, the faster objects get frozen.
Fracturing#
Although the fracture body is a relatively inexpensive type, in case of large number of fracture pieces, the impact on performance may become significant. To avoid performance drops, the following tips can be used:
- Use the Volume Threshold parameter to reduce the number of fracture pieces.
- Remove the pieces of a fractured body from the scene.
A code-based example illustrating how to remove (fade with the time) the fracture pieces from the scene can be found in the Physics section of the UnigineScript samples.