Skip to content

Physics

Adrian edited this page Feb 5, 2021 · 18 revisions

Overview

As mentioned in the ZPhysicsComponent section, Zenith currently uses the Bullet Physics library as it's primary physics implementation. ZPhysics is subsystem abstraction built on top, which has a few useful methods we can use to manipulate physical game object behavior.

Every game has its own physics universe, queryable with a call to the game or scene's PhysicsUniverse() getter.

We can add a ZRigidBody to the physics universe by calling the AddRigidBody method:

    PhysicsUniverse()->AddRigidBody(rigidBody);

Note that currently part of the physics subsystem is directly coupled to the Bullet Physics implementation at the moment, but there are plans to abstract the interface away further for the sake of orthogonality.

We can perform simple ray casting using the RayCast method, which accepts the start position and direction of the ray and returns a ZRaycastHitResult with a pointer to the ZGameObject hit, if any, the position of the hit, and other information:

    ZRaycastHitResult hit = PhysicsUniverse()->RayCast(start, direction);
    if (hit.objectHit) {
        // Do something game-worthy
    }

Both the start position and direction parameters are glm::vec3 types.

If we'd like to visually debug a collision mesh, we can toggle DebugDraw in our rendering code, passing in the scene to draw:

    // ... Some rendering stuff happens
    PhysicsUniverse()->DebugDraw(scene);
    // ... Some more rendering stuff happens

An concrete example of this can be seen here.

Clone this wiki locally