Composition of the Game World
| Category | Examples | Abstraction in the Engine |
|---|---|---|
| Dynamic objects | Tanks, drones, NPCs | GameObject (GO) |
| Static objects | Houses, hangars, watchtowers | GameObject |
| Environment | Terrain, sky, vegetation | Separate systems (terrain system, TOD) |
| Invisible objects | Air walls, triggers, rules | GameObject |
Conclusion: everything is a GameObject (abbreviated as GO).
Component-Based Architecture
Why Not Deep Inheritance
- Diamond problem of multiple inheritance (is an amphibious tank a “vehicle” or a “boat”?)
- Code bloat and maintenance difficulty
Component-Based Solution
Split “properties” and “behaviors” into independent Components and assemble them as needed:
| Drone Example | Corresponding Component |
|---|---|
| Spatial position | Transform |
| Appearance | Model/Mesh |
| Movement capability | Motor |
| Health | Health |
| AI patrol | AI_Patrol |
| Attack behavior | Combat |
Advantages:
- Composition > inheritance
- Dynamic add/remove at runtime (Unity/UE)
- Data and behavior are decoupled, which helps parallel and batch processing (ECS later)
Making the World Move: Tick Mechanism
- Tick = the engine advances the world by one step at every fixed frame interval (e.g., 33 ms)
- Every Component implements
Tick(float deltaTime):- Motor updates position
- Animation updates bones
- Physics updates colliders
- Combat detects firing
- To improve cache hit rates, modern engines batch Tick by system (first tick all Motors, then all Animations…)
Inter-Object Communication: Event System
| Direct Call (Tight Coupling) | Event Mail (Loose Coupling) |
|---|---|
| Shell explodes → call takeDamage() on each nearby object one by one | Shell explodes → PostEvent(“Damage”, 100) → nearby objects check their mail on the next Tick and lose health |
- Mailbox model = determinism + extensibility
- Implementation details: string hashing / RTTI / reflection (UE Blueprint, Unity SendMessage)
Scene Management: From Brute-Force Traversal to Spatial Acceleration
| Method | Core Idea | Suitable For |
|---|---|---|
| Brute force O(n²) | Traverse all objects every frame | Small demos |
| Uniform grid | Divide the world into cells; check only neighboring cells | Uniformly distributed objects |
| Quadtree / Octree | Recursive subdivision; empty regions are no longer split | 2D/3D sparse worlds |
| BVH | Merge bounding boxes bottom-up | Many dynamic objects; fast updates |
Engines usually ship with multiple structures built in, so products can switch based on the characteristics of the scene.
Advanced Topics
| Problem | Common Strategy |
|---|---|
| Event ordering issue | Introduce a post office concept, rather than having every component trigger or listen to events arbitrarily |
| Tick takes too long | Time compensation / frame skipping / batched processing (deferred processing) |
| Render thread and logic thread synchronization | Logic ticks first → writes a snapshot → render thread consumes it, with an extra 1~2 frames of latency |
| Drawbacks of the component pattern | Query overhead, cache-unfriendly → the advanced course introduces ECS (SOA + System batching) |
| Physics and animation coupling | Animation provides the initial pose → physics simulation → animation blends the interpolated result back, achieving “realistic, satisfying falls” |
| Event debugging | Visualized mail: 3D floating text, frame-by-frame Pause, Log flood |
One-Sentence Summary
The game world = a GameObject container + Component assembly + Tick advancement + Event communication + spatial index acceleration.
Understand these five steps, and you have the “map” for writing an engine by hand.