Physics

Hology Engine allows you to simulate physics with both dynamic and kinematic physics bodies. Beyond just collision detection, you can use physics to apply forces, control velocities, and dampening.

Physics System

Most of the physics functionality is accessed using the Physics System service. You can access this service by injecting it in your actor or actor component class.

import { Actor, BaseActor, PhysicsBodyType, PhysicsSystem, attach, inject } from "@hology/core/gameplay";

@Actor()
class ExampleActor extends BaseActor {
    private physics = inject(PhysicsSystem)
}

Physical actors

In order for actors to represent physical objects that can be moved using physics simulation or collide with other physical objects, they need to be added to the physics system. While this can be done automatically when using existing components such as MeshComponent and CharacterMovementComponent, you can also add the actor to the physics system yourself. You would normally do this if you are not using the previously mentioned component that does this for you.

import { Actor, BaseActor, PhysicsBodyType, PhysicsSystem, attach, inject } from "@hology/core/gameplay";

@Actor()
class ExampleActor extends BaseActor {
    private physics = inject(PhysicsSystem)
    
    onInit() {
        this.physics.addActor(this, [new BoxCollisionShape(1,1,1)], { 
            isTrigger: false,
            mass: 1,
            friction: 1,
            continousCollisionDetection: false,
            type: PhysicsBodyType.dynamic
          })
    }
}

When you add an actor you add a reference to the actor itself, followed by an array of at least one collision shape and some additional options. You can have multiple collision shapes to represent a more complex shape.

  • isTrigger - Whether it should just detect overlapping events

  • mass - The mass of the actor which affects how its velocity will change when colliding with other objects.

  • friction - A lower value will make other objects slide more easily.

  • continousCollisionDetection - Whether to enable continuous collision detection which is a bit more computationally expensive but can lead to better collision detection with fast objects.

  • type - Which type of physics body. Read more about Physics body types

Last updated