introduction
this document explains the system i developed for handling collision detection between the player and the 3d mesh representing the game environment. without such a system, the player would not interact with the environment and would walk through walls, or, if gravity is implemented, would fall through floors. early in development i used an AABB system similar to the one in example 5 (splitscreen) in HailToDodongo's tiny3d library. this system works well for small, simple environments consisting of rectangular obstacles aligned with the coordinate system, and is fairly simple and straightforward to set up. but for more complicated environments with diagonal surfaces, a more robust system is needed. additionally, in a project with many different complex environments the player could navigate between, i needed a system which would be able to automatically extract collision data from the environment mesh so that no manual intervention would be required every time i add a new area to the game.
world collision system overview
this diagram shows all of the components of the collision system starting from creating the model in blender to calling the collision resolution function while the game is running. the flow of the system is tightly correlated with the system for displaying the environment (since the collision must happen where the visual boundaries are), so on the right side of the diagram i have included the components of the tiny3d system for rendering the environment.
save_triangles.cpp -> loadCollTriangles()
save_triangles is a standalone script i wrote to extract triangle information (3d coordinates of vertices grouped by triangles) from a .glb file exported from blender. it uses the tinygltf library.
it can be run from the command line with the following syntax:
save_triangles <path/to/input.glb> <path/to/output.bin>
i modified the makefile for my game to automatically execute save_triangles if a new .glb file is added to the game assets or an existing one has been updated:
filesystem/%.bin: assets/models/%.glb
@mkdir -p $(dir $@)
@echo " [COLL] $@"
tools/save_triangles/save_triangles $< $@
resolveWallCollision()