Physics
Set up physics simulation with collision layers and materials.
Introduction
Physics simulation makes your game world feel real. This guide covers physics engine setup, collision layers, physics materials, and performance tuning.
Guide 10 of 25
Screenshot
Step-by-Step Guide
Configure physics settings
In Project Settings > Physics, set gravity (-9.81 Y), fixed timestep (0.0167 for 60Hz), and solver iterations (4-8).
Set up collision layers
Define layers: Player, Enemy, Environment, Projectile, Trigger. Configure interaction matrix.
Create physics materials
Make materials: ice (friction 0.1, bounce 0), rubber (friction 0.8, bounce 0.9), metal (friction 0.3, bounce 0.2).
Add colliders to objects
Assign shapes: box for walls, sphere for balls, capsule for characters, mesh for complex static geometry.
Configure rigidbodies
For dynamic objects, set mass, drag, angular drag, and gravity. Use kinematic for player-controlled objects.
Tune solver iterations
Increase (8-16) for stacked objects or complex joints. Decrease (2-4) for simple scenes to save CPU.
FluxScript Example
Here is a complete FluxScript implementation for this guide:
on start {
physics.setGravity(0, -9.81, 0)
physics.setFixedTimestep(0.0167)
physics.setSolverIterations(6)
physics.setLayer("Player", 0)
physics.setLayer("Enemy", 1)
physics.setLayer("Environment", 2)
physics.setLayer("Projectile", 3)
physics.setCollision(0, 1, true)
physics.setCollision(0, 2, true)
physics.setCollision(3, 1, true)
physics.setCollision(0, 3, false)
ice = physics.createMaterial("ice", { friction: 0.1, bounce: 0.0 })
rubber = physics.createMaterial("rubber", { friction: 0.8, bounce: 0.9 })
scene.find("IceFloor").setPhysicsMaterial(ice)
scene.find("RubberBall").setPhysicsMaterial(rubber)
}Node Graph Example
Prefer visual programming? Here is the equivalent Node Graph:
[On Start] --> [Set Gravity + Timestep + Iterations]
--> [Set Layers: Player, Enemy, Environment, Projectile]
--> [Set Collision Matrix: 4 pairs]
--> [Create Materials: ice, rubber]
--> [Apply to objects]Optimization Tips
Keep your implementation fast and efficient with these optimization tips:
- Use primitive colliders (box, sphere, capsule) instead of mesh colliders for dynamic objects. Mesh colliders are 10x more expensive.
- Put sleeping rigidbodies to rest. Objects that have not moved for 2 seconds should sleep — saves significant CPU.
- Use higher fixed timestep on mobile (0.02 = 50Hz) to save CPU. 60Hz is ideal but 50Hz is 20% cheaper.
- Limit solver iterations to 4-6 for most scenes. Only increase to 8-16 for stacking scenarios.
Common Mistakes
Using mesh colliders for all objects
Mesh colliders are extremely expensive. Use primitives for 99% of objects.
Collision matrix not configured
Without layer filtering, every object checks against every other. Configure the matrix.
