Building a Fighting Game: Architecture, AI, Hit Detection, and Frame Data
This article provides a practical technical roadmap for developing fighting games, covering the critical decisions that determine feel and performance. It compares hierarchical state machines and ECS for character architecture, details essential frame data fields and formula-based advantage calculation, explains how to design believable AI with tunable reaction delays and error rates, and outlines a deterministic hit detection pipeline. The goal is to help developers make informed choices early to avoid costly refactors later.
The feel of a fighting game depends heavily on frame data, input buffering, cancel windows, and the precise timing of hit detection. Animation and audio presentation also shape player perception, and the same logic can feel very different on devices with varying input latency. Before starting development, three key questions need answers: What structure should the combat entities use? How do you make AI challenging yet believable? Should versus mode be local same-screen or networked? Below, we walk through the development sequence.
Step One: Architecture — State Machine or ECS?
Both approaches can produce a solid fighting game. The choice depends on character count, mechanic complexity, and how often you plan to extend the roster.
A Finite State Machine (FSM) offers intuitive states and straightforward debugging, making it a good fit for early builds with a small roster. Each character has a set of states—idle, walk, normal attack, hitstun, knockdown—connected by transition conditions. As shared mechanics grow, however, the transition graph can expand rapidly. A rule like “cancellable on hit” may need to be repeated across dozens of transitions.
ECS (Entity-Component-System) decomposes movement, actions, hit reactions, and resources into components processed by systems. New characters are created by combining components and configuring frame data tables, without modifying existing system code. The trade-off is upfront investment in building the component/system framework, plus the need for system-level logging and visualization tools during debugging.
| Dimension | Hierarchical FSM + Data Tables | ECS |
|---|---|---|
| Ideal roster size | Initial builds, up to ~8 characters | 15+ characters with ongoing additions |
| Extension method | Add new states and transitions | Combine components, configure frame tables |
| Cost of shared mechanics | Rises as mechanics multiply | Lower, via component reuse |
| Upfront investment | Low | High, framework required |
| Debugging approach | State graphs are directly viewable | Requires component inspectors and system logs |
The following thresholds are rough guidelines; assess based on your team’s size and roadmap:
- Up to 8 characters, low mechanic complexity (standard normals, specials, throws), team of 1–3 developers: choose hierarchical FSM + data tables.
- 15+ characters, or multiple characters sharing mechanisms like counters, special hit reactions, or summons, with a long-term update plan: choose ECS.
- In-between: start with the FSM approach, but write state transitions as data tables and extract shared mechanics into independent modules. This leaves room for migration later.
A suggested migration path:
- Move frame data from code into tables, separating data from logic.
- Refactor shared logic into standalone functions, one per mechanic.
- Split components by position, animation, hit reaction, and resources; migrate characters one at a time.
- After each migration, replay identical input sequences and compare state frame-by-frame.
The most common issue during migration is inconsistency between old and new implementations. Fix the random seed and compute a hash of key state values per frame to quickly identify where the divergence occurs.
Two: Characters and Frame Data
Frame data is the design language of fighting games. Each move should define at minimum:
- Startup: frames from input until the hitbox becomes active.
- Active: frames the hitbox remains active.
- Recovery: frames from active end until the character can act again.
- Hitstun / Blockstun: frames the opponent is unable to act after being hit or blocking.
- Cancel window: which actions can cancel this move, and the starting frame for the cancel.
Advantage on hit can be calculated as:
Advantage = Opponent’s hitstun/blockstun frames − Attacker’s remaining recovery frames
A positive result means the attacker acts before the defender; a negative result means the opposite. Use this value to verify combos behave as intended, rather than tuning by feel.
Input buffering determines whether a “close enough” input still registers. Start with 3–6 frames and adjust based on real player testing. Too long a buffer causes unintentional triggers; too short makes inputs feel dropped.
Character development checklist:
- Every move has complete frame data; startup and active frames are each at least 1.
- Cancel window start/end frames match the move’s animation.
- Hitboxes are visible in frame-by-frame playback and correspond to specific frames.
- New character values are reasonable compared to similar characters—no obvious overpowered or unplayable matchups.
Three: AI Design and Fairness
AI should be challenging without feeling like it “reads” the player. The core principle: AI may only read information visible on screen—position, animation state, health, and inputs that have already occurred. It must not access inputs the player has not yet pressed.
AI reaction must have latency, and that latency should come from a random distribution, not a fixed value. Human visual reaction time is roughly 200 ms—about 12 frames in a 60 fps game. Reactions to known startup animations can be faster, so reference values are:
- Easy: 20–30 frames reaction delay.
- Normal: 14–20 frames.
- Hard: 10–14 frames; do not go below 10 frames (~167 ms).
These are starting points, not final values—calibrate with playtest data.
Add two more controllable error types beyond reaction delay:
- Error rate: a chance per decision to pick a wrong move or drop a combo, typically 5%–15%.
- Decision frequency: make decisions every 2–4 frames rather than every frame.
Errors must appear at the decision level only—do not alter hit detection or damage values. Random damage changes break fairness and hurt result reproducibility.
A single parameter table can manage difficulty tiers:
| Parameter | Easy | Normal | Hard |
|---|---|---|---|
| Reaction delay (frames) | 20–30 | 14–20 | 10–14 |
| Error rate | ~15% | ~8% | ~5% |
| Decision interval (frames) | 4 | 3 | 2–3 |
| Combo completion cap | per playtest | per playtest | per playtest |
Fairness verification steps:
- Code review: confirm the AI decision module has no API to access future inputs.
- Block all future input information from the AI, then replay the same sequence; AI behavior should be identical.
- Record actual AI reaction frames and compare against the configured distribution; deviations should stay within the set range.
- Run a blind human test and survey whether players felt cheated. If the rate is too high, adjust reaction delay first, not numeric advantages.
Four: Combat Hit Detection
The hit detection order must be fixed and documented. A common sequence:
Input collection → State update → Movement → Collision detection → Damage resolution → Hitstun and pushback → Next frame
If this order changes, the same inputs can produce different results, affecting both network sync and replays.
Rules that need to be agreed in advance:
- Same-frame mutual hits: when both sides land a hit on the same frame, resolve by attack priority or apply damage to both. Either is acceptable, but it must be specified.
- Throws vs. strike moves: can a throw be beaten by a strike move? On which exact frames does the counter-hit window begin? Document precise frame numbers.
- Hitstop: frames of freeze on both characters at the moment of impact. Reference range: 2–12 frames. Hitstop affects both feel and combo rhythm, so test via replay comparison before changing.
- Hitbox interpolation: rendering may interpolate, but hit detection must use discrete integer-frame data.
Test cases should cover these edge conditions:
- Same-frame mutual hits resolve per the documented rule.
- Throw-vs-strike priority resolves correctly on the first active frame.
- Cancel window works on both its first and last frame.
- Maximum damage and maximum combo length combos do not produce anomalies.
Each test case should document the expected result and include a frame-accurate replay for verification.
