Use physics to enable physically plausible interactions with your scene content.
Different backends are supported such as Bullets Physics, NVIDIA PhysX and Newton Dynamics.
To fully declare a rigid body you must attach at least one collision shape to its node. Create a Collision component and call Node.SetCollision to do so. You can attach multiple collision components to a single node.
A rigid body can be of the following type:
RBT_Dynamic: Responds in a physically plausible manner to interaction with other rigid bodies. Its motion is fully controlled by the physics engine. A dynamic rigid body will automatically go to sleep and stop consuming simulation resources when it stops moving for a long enough period of time.
RBT_Kinematic: Follows the transformation of the node it belongs to. A kinematic body never goes to sleep automatically.
The rigid body intertia tensor is computed from its collision shape properties. A zero mass rigid body is considered infinitely heavy and cannot be moved.
Note: SceneNewtonPhysics.SceneCreatePhysicsFromAssets means that if setting up the physics states requires access to an external resource, such as a mesh, it should be loaded from the assets system. If you are working from the filesystem, use SceneNewtonPhysics.SceneCreatePhysicsFromFile.
Running the Simulation
This involves 3 steps on each update:
- Synchronize physics state with the scene declaration using SceneNewtonPhysics.SceneCreatePhysicsFromAssets. Alternatively, you can use a more fine-grained approach using SceneNewtonPhysics.NodeCreatePhysicsFromAssets to improve performance.
- Step the simulation using SceneNewtonPhysics.StepSimulation.
- Synchronize the updated physics transformations to the scene using SceneNewtonPhysics.SyncDynamicBodiesToScene.
Note: If you are using kinematic bodies you will also need to synchronize them from their node transformation on each update using SceneNewtonPhysics.SyncKinematicBodiesFromScene.
The Easy Way
While the low-level API offers the most flexibility most projects will use a straightforward implementation which is provided by the SceneUpdateSystems function. This function handles all of the above steps and does the same for other systems you may use.
When using a script system, this function will also dispatch collision events to it.
Keeping the System Synchronized
Call the physics system garbage collect method (eg. SceneNewtonPhysics.GarbageCollect) on each update to ensure that destroyed nodes or components are properly removed. If you know that no node or component was destroyed during a particular update, not calling the garbage collector will save on performance.
Reading Physics Transformation
When a node is assigned a dynamic rigid body its transformation matrix is overriden by the physics system. Its transform component however, is not. Transform methods such as Transform.SetPos or Transform.SetRot will have no effect unless you explicitely synchronize the node to the physics system after using them.
Transform.GetWorld will return the transformation matrix as set by the physics system.