Skip to main content
2014 C++ OpenGL Box2D GLFW DevIL

Tossing Balls

High school graduation project — 2D physics puzzle game with Box2D integration and data-driven level loading

Tossing Balls screenshot

Overview

A high school graduation project (maturitetna naloga) — an Angry Birds-style 2D physics puzzle game where the player fires balls from a rotatable cannon at structures made of wood, stone, and glass blocks. Box2D handles realistic physics simulation with impulse-based damage, levels are entirely data-driven via XML, and the rendering uses OpenGL with a custom camera mapping between Box2D world coordinates and screen coordinates. ~3,800 lines across 38 source files, built at age 17.

Architecture

Tossing Balls architecture

The core entity system uses a clean inheritance tree: gEntity (abstract base with Render/Update/MouseOver) branching into TBBody (physics-enabled), TBSprite (static), TBCannon (composite), gButton (interactive UI), and TBDialog (container). WorldContext acts as the central state container owning the b2World, Camera, all entities, and a deferred disposal queue. TBWorld is a factory class handling dual creation of game entities and corresponding Box2D physics bodies linked via SetUserData.

Code Highlights

Box2D impulse-based damage system
void TossingBalls::BeginContact(b2Contact* contact) {
    TBBody* a = static_cast<TBBody*>(body1->GetUserData());
    TBBody* b = static_cast<TBBody*>(body2->GetUserData());
    if(a && b) {
        if(a->getType() != EBT_groundObject) a->collision = true;
        if(b->getType() != EBT_groundObject) b->collision = true;
    }
}

void TossingBalls::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
    if(a->collision) { a->force = impulse->normalImpulses[0]; a->collision = false; }
    if(b->collision) { b->force = impulse->normalImpulses[0]; b->collision = false; }
}

Highlights

  • High school graduation project (age 17) — built a complete 2D physics game engine from scratch in C++ with a custom entity system, polymorphic animation framework, Box2D physics integration, and data-driven level loading from XML
  • Implemented a correct three-phase Box2D collision protocol: flag contacts in BeginContact, capture impulse magnitudes in PostSolve, apply HP damage in EndContact
  • Fully data-driven architecture: levels, UI layouts, game config, textures, and sounds all externalized into XML and INI files requiring zero code changes to add content
  • Composable animation system with five animator types (fade, blink, scale, image sequence, timed stop) attachable to any entity via a vector