Adding physics to your models in the Untold Engine enables realistic interactions such as gravity, forces and collisions(not supported yet). Follow this tutorial to enable physics on an entity.
As always, begin by creating an entity for your model.
let redPlayer = createEntity()
Load the .usdc file for your model and link it to the entity.
setEntityMesh(entityId: redPlayer, filename: "redplayer", withExtension: "usdc")
Enable physics on the entity by calling the setEntityKinetics function. This will allow the engine to simulate movement and collisions for the entity.
setEntityKinetics(entityId: redPlayer)
Customize the physics behavior of your entity:
- Use setMass to define the entity's mass. A heavier object will require more force to move.
- Use setGravityScale to control how strongly gravity affects the entity.
setMass(entityId: redPlayer, mass: 0.5)
setGravityScale(entityId: redPlayer, gravityScale: 1.0)
You can apply a force to the entity dynamically in the Update function. This is useful for simulating motion or interactions in real time.
applyForce(entityId: redPlayer, force: simd_float3(0.0, 0.0, 5.0))
Note: Forces are applied per frame, so ensure you only apply them when needed to avoid unintended behavior.
Instead of applying forces manually, you can use the Steering System to apply forces indirectly. For example, if you want your entity to steer to a position, you can call the following function:
steerTo(entityId: redPlayer, targetPosition: simd_float3(0.0,0.0,5.0), maxSpeed: 2.0, deltaTime: deltaTime)
There are several other steer functions that you can use such as
steerAway()
steerPursuit()
followPath()
After you Run the project in Xcode:
- Your model will appear in the game window.
- Press P to enter Game Mode.
- Your entity will respond to gravity, forces, and collisions.
- If you applied a force, you’ll see your model move accordingly.
Next: Enabling Animation Previous: Detecting User Inputs