Breaking Chests

How to implement breakable chests with physics

make your own site

Our game engine uses PhysX, from Nvidia, to handle the physics. By adding weight and realistic behavior to the world of Alita: Unbreakable Warrior, we aim to increase the immersion of the player in it. The physics engine covers the basics of the interaction of the player with the game world (rigid body simulation, collision detection, and scene queries) but also allows to create cosmetic effects. In addition, we have implemented a layer system to perform layer-based collision detection and scene queries.

Mobirise

Since physics are not core to our gameplay, this article will focus on of one of the cosmetic effects implemented using physics, the breakable chests. In Alita: Unbreakable Warrior, there are chests placed around the map, which are tiered apart into pieces if they are clicked by the player. Breakable chests are destructibles, elements of the environment that can be destroyed. Destructibles can be achieved in two ways:

1. Pre-Baked destruction:

The good:

- Easy to implement with the physics library (it does not require a destruction library).

- The effect can be hidden with particles.

The bad:

- All destructibles need two meshes, the original mesh and a split up mesh made of all the different pieces.

- An optional mesh collider may improve realism.

2. Procedural destruction:

The good:

- Realistic real-time breaking.

- Unique effect each time.

The bad:

- Really performance intensive.

- Too complex.

To satisfy the needs of our game, which are not too complex, the first way is enough. The steps involved in the implementation of breakable chests with pre-baked destruction are the following:

1. Create two models of the chest:

a) Full chest:

- Single mesh.

b) Broken chest:

- One mesh per each piece.

2. Make two prefabs, one for each chest:

a) Full chest:

- Rigid static.

- Box collider enclosing the whole geometry.

- Script: on click destroys the full chest prefab and instantiates the broken chest prefab at its position.

b) Broken chest:

- Rigid dynamic: tweak its values until it looks realistic.

- Box collider per each mesh.

- Script attached to each mesh: applies a force in a random direction.

3. Instantiate the full chest prefab and place it in the map.

Hit the play button and you are ready to start breaking chests!

Sandra Alvarez Garcia

Alita Unbreakable Warrior