Physics
Physics in Hiber3D utilizes the JoltPhysics library .
Rigid Body
To add physics to an entity start by adding a RigidBody component to it.
Type
The first thing you want to decide is the Type, which lets you pick from three different options:
Static
- A physical object that will never moveKinematic
- A physical object controlled by code or scriptDynamic
- A physical object that will move when force is applied to it
Motion Quality
The motion quality determines how the engine checks for collision.
Discrete
- Checks for collisions at fixed intervals, offering better performance but with a higher risk of tunneling, where fast-moving objects may pass through others without detecting a collision.Continuous
- Detects collisions along the object’s path between frames, reducing the risk of tunneling but at the cost of lower performance.
Motion Type | Performance | Collision Accuracy | Risk of Tunneling |
---|---|---|---|
Discrete | High | Lower | Higher |
Continuous | Lower | High | Low |
Collision group and mask
The collision group and mask defines what your entity should collide with.
Collision group
- defines what group(s) your entity will belong toCollision mask
- defines what group(s) your entity will collide with
Shape
When you add a RigidBody to an entity using the editor, a Shape component will be automatically added as well. Using it, you can define the type, size, density, and offset of the collider shape of your entity.
Additionally, you can manually add Shapes to entities that are children to the one with the RigidBody component - these will be combined.
Debugging
Under the debug settings button, the button with a gear icon, you can find various debugging tools for visualizing physics.
Collision events
To enable code that reacts to collisions between entities, there are three different events you can use:
CollisionStarted
CollisionPersisted
CollisionEnded
Some of these may include extra information regarding the collision, such as the velocity and contact points of the colliding entities, or the normal vector of the collision.
Scripting
If you want to react to a type of collision event in a script, subscribe to the event in the onCreate()
function of the script:
onCreate() {
hiber3d.addEventListener(this.entity, "Hiber3D::CollisionStarted");
}
and then you can react to the event in the onEvent()
function:
onEvent(event, payload) {
if (event === "Hiber3D::CollisionStarted") {
hiber3d.print("entityA: '"+payload.entityA+"' just collided with entityB: '"+payload.entityB+"'");
}
}
C++
Similarly, you can react to events in an ECS system from a C++ module like so:
static void mySystem(Hiber3D::EventView<Hiber3D::CollisionStarted> events) {
for (const auto& event : events) {
LOG_INFO("entityA: '{}' just collidied with entityB: '{}'", event.entityA, event.entityB);
}
}
void MyModule::onRegister(Hiber3D::InitContext& context) {
context.addSystem(Hiber3D::Schedule::ON_TICK, mySystem);
}