Dev Log: Evolution of the Match Engine
Building a 2D top-down match engine from scratch has been frustrating. What started as a chaotic group of players chasing a ball and sometimes chasing each other, has evolved into a semi-intelligent, tactical, physics-based simulation of 5-aside drunken pub football. Below is a summary of how the engine was developed and the key features that brought it to life.
Phase 1: Basic Physics
First thing we started with was the ball, it's usually kind of important in football
- Ball Physics & Friction: Kicking the ball gives it an initial velocity, which then decays over time due to ground friction, creating a natural roll and deceleration.
- AI behaviour: Our initial AI was very simple with every player chasing after the ball like a mass of 5-year-olds.
- Dribbling & Shooting: Once a player gets close enough to the ball, they move with it directly towards the opponent's goal. Once close enough to the goal, they attempt a shot and the engine detects if the ball crosses the goal line between the posts.
Phase 2: Introducing Teamplay & Basic Defending
Next, we had to make the players act like a team instead of a swarm of individuals:
- Passing Mechanics: To implement passing, players scan the pitch and identify open teammates, and pass. This was later refined to check if a teammate is free (not too close to an opponent) and prioritise forward passes. If a good forward pass can't be found, players then continue to dribble or shoot.
- Defensive Positioning: Defenders were programmed to hold a defensive line, placing their bodies between the ball carrier and their own goal. Though being a simulation of a pub team, players will occasionally run in the wrong direction towards the lure of a beverage.
- Shielding & Tackle Cooldowns: To prevent endless tackle-spamming, we added a cooldown. When a player wins a tackle or a tackle fails, there is a short window before the next tackle can be attempted, giving the ball carrier space to react.
Phase 3: Predictive Passing & Evasion
To make build-ups look fluid and professional, we added advanced predictive math:
- Predictive Passing (Leading the Receiver): Instead of passing directly to where a teammate *is*, the passing AI calculates the teammate's future position based on their current velocity. It aims the ball to that future spot, letting them run onto the ball for intelligent through-balls. Unfortunately, at the pub level, none of their team mates are able to read this level of incisive passing.
- Evasive Dribbling: If a player with the ball is under immediate defensive pressure, they will attempt to sidestep or dribble away from the challenger rather than running backwards. Though I've observed that under pressure they most likely proceed to run to the side of the pitch for mid game refreshments.
- Human Error & Distance Accuracy: We added a randomized "passing error" to keep the game realistic. Short passes are highly accurate, but long, cross-field passes have a higher chance of going slightly wayward.
- Variable Pass Power: We removed static pass power. The power is now dynamically calculated based on the distance to the teammate—delivering soft, gentle lay-offs or paced, long through-balls.
Phase 4: Tactical Defending & Zonal Shape
With the attack slowly taking shape, the next step was to improve the defending:
- Intelligent Marking: Defenders actively identify the most immediate threat (the opponent closest to the goal) and position themselves between them and the goal, cutting off direct paths. Unless intoxicated, then the defender will proceed to take a random path, usually away from the ball.
- Passing Lane Interception: Defenders check the ball carrier's open lanes and attempt to move to block obvious passing paths.
- Proactive Aggression: When the ball enters the defensive third, defenders abandon their zones to aggressively press the ball carrier and force turnovers. Though the game is so fast paced this is almost unnoticable and often the striker has made the shot before it happens.
- Zonal Positioning: When the ball is not an immediate threat, defenders return to hold their solid zonal shapes relative to their home positions.
Phase 5: Physics Realism & Positioning
Finally, we resolved player spacing and zonal positioning:
- Player Collision Physics: The engine checks distances between all players. If two overlap, a "push-back" force pushes them apart along the collision axis. Players can now bump into, shield, and block each other, without standing on top of each other.
- Zonal Positioning: To prevent player clumping together, we introduced a "leash" to tether players to their defined zones. While they can engage in play, if they drift too far, their primary instinct is to return, maintaining formation spacing. Though this occasionally introduces a "feature" where players run away from the scary ball.
- Shooter's Mentality: Attacking players are now "highly" decisive inside the box. If they see a clear goal opportunity, the probability of them taking the shot is significantly increased (though the probability the hit the ball right at the keeper is near 1).