What You'll Learn
- Fixed-timestep game loop
- Entity Component System (ECS)
- Collision detection (Circle, AABB)
- Game architecture patterns
Game Development Essentials in C++
C++ powers virtually every AAA game engine — Unreal, Unity's core, Godot, and custom engines at studios like id Software and Naughty Dog. This lesson covers the three pillars: the game loop (timing), ECS architecture (data), and collision detection (physics).
The Game Loop — Fixed Timestep
A game loop runs continuously: process input → update game state → render. The fixed timestep pattern ensures physics runs at exactly 60Hz regardless of frame rate. An accumulator tracks elapsed time, and multiple update steps run per frame if the game falls behind.
Pro Tip: Separate update rate from render rate. Physics at 60Hz, rendering at monitor refresh. Interpolate positions between physics steps for smooth visuals.
Game Loop
Fixed-timestep loop with player movement
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
// The game loop — the heartbeat of every game
// Fixed timestep with variable rendering
class GameClock {
using Clock = chrono::high_resolution_clock;
Clock::time_point lastTime;
double accumulator = 0;
int frameCount = 0;
double fpsTimer = 0;
public:
double dt = 1.0 / 60.0; // 60 updates per second
int fps = 0;
GameClock() : lastTime(Clock::now()) {}
double tick() {
aut
...Entity Component System (ECS)
ECS separates data from behavior. Entities are just IDs. Components are plain data structs (Position, Health, Renderable). Systems process all entities with specific components. This data-oriented design is cache-friendly and scales to thousands of entities.
Common Mistake: Making components too complex. Each component should be one piece of data (Position, not PositionAndVelocityAndHealth). Small, focused components compose better.
Entity Component System
Build entities with composable components and systems
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
// Entity Component System (ECS) — data-oriented game architecture
// Entities are IDs, Components are data, Systems process components
using EntityID = int;
struct Position { float x, y; };
struct Velocity { float vx, vy; };
struct Health { int hp, maxHp; };
struct Renderable { char symbol; string color; };
// Simple ECS World
class World {
EntityID nextId = 0;
uno
...Collision Detection
Every game needs collision detection. Circle-vs-circle checks if the distance between centers is less than the sum of radii. AABB-vs-AABB checks if rectangles overlap on both axes. Point-in-AABB checks if a click or projectile is inside a bounding box.
Collision Detection
Circle, AABB, and point collision tests
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
struct Vec2 { float x, y; };
struct Circle {
string name;
Vec2 pos;
float radius;
};
struct AABB { // Axis-Aligned Bounding Box
string name;
Vec2 min, max; // top-left, bottom-right
float width() const { return max.x - min.x; }
float height() const { return max.y - min.y; }
Vec2 center() const { return {(min.x+max.x)/2, (min.y+max.y)/2}; }
};
// Circle vs Circle
bo
...Quick Reference
| Concept | Description |
|---|---|
| Fixed timestep | Physics updates at constant rate (e.g., 60Hz) |
| ECS | Entity = ID, Component = data, System = logic |
| Circle collision | distance < r1 + r2 |
| AABB collision | Overlap on both X and Y axes |
| Spatial partitioning | Grid/quadtree for many objects |
Lesson Complete!
You now understand game loops, ECS architecture, and collision detection — the core of every C++ game engine.
Sign up for free to track which lessons you've completed and get learning reminders.