In my eleventh video of the “Building a Game Development Framework” series, I implement Collision Detection and update the Screen Management system. This update allows game objects to interact with each other physically and simplifies how developers add entities to their game world.
I make the foundation of the collision system the BoundingBox class.
- AABB (Axis-Aligned Bounding Box): The framework uses simple rectangle-to-rectangle intersection checks. It verifies if the X or Y edges of one sprite fall within the span of another.
- Insets and Offsets: A major improvement I made is adding Insets. Often, a sprite’s image (for example, 64×64 pixels) is much larger than the actual character (e.g., a person is thin, but the image is a square). With Insets this allows you to shrink the physical “hitbox” independently of the visual image so collisions will feel more realistic.
I setup the framework to categorizes collisions based on whether an object is an “Actor” or “Decor”. Since Decors do not move, they don’t check if they collide with another sprite. Actors, which move, do check if they collide with another sprite.
- Impact Flag: Not all objects need to check for collisions. The
doesImpact()method allows the system to skip checks for static background objects (Decor) that don’t move, saving processing power. - Reversion Logic: When a collision is detected, the framework “reverts” the character’s movement. In the basic implementation, it simply moves the character back to their previous non-colliding position.
- Sliding Physics: In more advanced movement (like in 4-key or mouse movement), the framework checks horizontal and vertical collisions separately. If you walk diagonally into a wall, the X-movement might be blocked, but the system allows the Y-movement to continue, creating a “sliding” effect against the wall.
I also introduce the SimpleScreen, which automates the rendering loop:
- The Sprite List: Instead of manually updating every character, the screen maintains a list of all Sprites.
- Automated Loop: The
renderChildmethod automatically iterates through the list, callsupdate(), performsdetectCollision(), and then callsdraw()for every entity. - Separation of Concerns: New abstract methods
addActor()andaddDecor()provide a clean way for developers to populate their game world during theshow()phase.
To make the world feel more alive, I introduce a Random Sound class:
- Footstep Variance: Instead of a single repetitive sound, this class holds a list of similar sounds (e.g., four different footstep recordings). Every time an animation triggers a sound, it randomly picks one from the list to create a more natural, less “robotic” audio experience.
I conclude with a demonstration of an Actor (the player) navigating around our “Soda Machine” Decor objects. I show how the player is physically blocked by the machines and how the “sliding” logic feels smoother than a hard stop.
This set of features transforms the framework from a simple animation player into a functional game engine where objects have physical presence and sound.
