The Mathematics Behind Open-World Game AI: A GTA 6-Style Breakdown (with Python)
Disclaimer: Rockstar Games has not published the internal AI code or formulas of GTA 6. This post explains the standard, publicly documented mathematical techniques that open-world games like GTA use for NPCs, police, and traffic, and implements each one in simple Python. Treat it as an educational model, not leaked source code.
- Pathfinding with A* (how NPCs find routes)
- Steering behaviors (how NPCs move smoothly)
- Utility AI (how NPCs decide what to do)
- Perception model (how NPCs notice you)
- Wanted-level "heat" model (how police escalation works)
- Intelligent Driver Model (how traffic behaves)
1. Pathfinding: A* Algorithm
A* finds the cheapest path on a map by scoring each tile:
f(n) = g(n) + h(n)
- g(n) = actual cost travelled from the start to node n
- h(n) = heuristic estimate from n to the goal, e.g. Euclidean distance: h = √((x1−x2)² + (y1−y2)²)
import heapq
import math
def heuristic(a, b):
return math.hypot(a[0] - b[0], a[1] - b[1])
def astar(grid, start, goal):
"""grid: 2D list, 0 = walkable road/sidewalk, 1 = blocked (building)."""
rows, cols = len(grid), len(grid[0])
open_set = [(heuristic(start, goal), 0.0, start)]
came_from = {}
g_cost = {start: 0.0}
moves = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
while open_set:
f, cost, current = heapq.heappop(open_set)
if current == goal:
path = [current]
while current in came_from:
current = came_from[current]
path.append(current)
return path[::-1]
if cost > g_cost[current]:
continue
for dx, dy in moves:
nx, ny = current[0] + dx, current[1] + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 0:
new_g = cost + math.hypot(dx, dy)
if new_g < g_cost.get((nx, ny), float("inf")):
g_cost[(nx, ny)] = new_g
came_from[(nx, ny)] = current
f_score = new_g + heuristic((nx, ny), goal)
heapq.heappush(open_set, (f_score, new_g, (nx, ny)))
return None
2. Steering Behaviors (Smooth Movement)
Instead of teleporting along a path, NPCs apply a steering force. For the arrive behavior:
desired_speed = vmax × min(1, d / rslow)
steering = desired_velocity − current_velocity
Here d is the distance to the target and rslow is the radius where the NPC begins slowing down.
import math
def arrive(pos, vel, target, max_speed=5.0, slow_radius=10.0, max_force=2.0):
dx, dy = target[0] - pos[0], target[1] - pos[1]
dist = math.hypot(dx, dy)
if dist < 1e-6:
return (-vel[0], -vel[1])
speed = max_speed * min(1.0, dist / slow_radius)
desired = (dx / dist * speed, dy / dist * speed)
steer = (desired[0] - vel[0], desired[1] - vel[1])
mag = math.hypot(*steer)
if mag > max_force: # clamp the force
steer = (steer[0] / mag * max_force, steer[1] / mag * max_force)
return steer
3. Utility AI (Decision Making)
Each action (flee, fight, wander, call police) gets a score from several considerations, each passed through a response curve:
U(action) = Σ wi · ci(xi)
c(x) = 1 / (1 + e−k(x − m)) (sigmoid curve)
To keep NPCs from being predictable, pick an action with a softmax probability: Pj = eUj/T / Σ eUk/T, where T is the "randomness" temperature.
import math
import random
def sigmoid(x, k=10.0, m=0.5):
return 1.0 / (1.0 + math.exp(-k * (x - m)))
def utility_scores(state):
"""state values are normalized to 0..1."""
fear = sigmoid(state["threat"]) * 0.6 + sigmoid(1 - state["health"]) * 0.4
fight = sigmoid(state["aggression"]) * 0.5 + sigmoid(state["health"]) * 0.3 \
+ sigmoid(1 - state["threat"]) * 0.2
wander = 0.2 + 0.3 * (1 - state["threat"])
return {"flee": fear, "fight": fight, "wander": wander}
def softmax_choice(scores, temperature=0.15):
exps = {a: math.exp(s / temperature) for a, s in scores.items()}
total = sum(exps.values())
r, acc = random.random(), 0.0
for action, e in exps.items():
acc += e / total
if r <= acc:
return action
return action
4. Perception Model (How NPCs Notice You)
Detection depends on distance, viewing angle (field of view), and visibility (night, cover). Awareness builds up over time:
p = (1 − d / R) × V (if the player is inside the FOV cone and d < R)
dA/dt = k · p − λ · A
A is awareness (0 to 1), R is the sight range, V is a visibility factor, k the gain and λ the forgetting rate. When A reaches 1, the NPC "spots" the player.
import math
def detection_probability(npc_pos, npc_facing_deg, player_pos,
sight_range=40.0, fov_deg=120.0, visibility=1.0):
dx, dy = player_pos[0] - npc_pos[0], player_pos[1] - npc_pos[1]
dist = math.hypot(dx, dy)
if dist >= sight_range:
return 0.0
angle_to_player = math.degrees(math.atan2(dy, dx))
diff = (angle_to_player - npc_facing_deg + 180) % 360 - 180
if abs(diff) > fov_deg / 2:
return 0.0
return (1 - dist / sight_range) * visibility
def update_awareness(awareness, p, dt, gain=1.5, decay=0.3):
awareness += (gain * p - decay * awareness) * dt
return max(0.0, min(1.0, awareness))
5. Wanted Level: Heat with Exponential Decay
Crimes add "heat". When no police can see you, heat cools down exponentially:
H(t + Δt) = H(t) · e−λΔt + Σ crimeweights
The star rating is found by comparing H against thresholds.
import math
import bisect
CRIME_WEIGHTS = {"speeding": 5, "assault": 20, "car_theft": 30, "shooting_cop": 60}
STAR_THRESHOLDS = [10, 30, 60, 100, 150] # heat needed for stars 1..5
class WantedSystem:
def __init__(self, decay_rate=0.05):
self.heat = 0.0
self.decay_rate = decay_rate
def commit(self, crime):
self.heat += CRIME_WEIGHTS[crime]
def update(self, dt, seen_by_police):
if not seen_by_police:
self.heat *= math.exp(-self.decay_rate * dt)
@property
def stars(self):
return bisect.bisect_right(STAR_THRESHOLDS, self.heat)
6. Traffic: Intelligent Driver Model (IDM)
Car-following behavior in realistic traffic is commonly modelled with the IDM acceleration formula:
a = amax · [ 1 − (v / v0)4 − (s* / s)2 ]
s* = s0 + v · T + (v · Δv) / (2 · √(amax · b))
- v = current speed, v0 = desired speed, s = gap to the car ahead
- s0 = minimum gap, T = time headway, b = comfortable braking
- Δv = your speed minus the leader's speed
import math
def idm_acceleration(v, gap, dv, v0=15.0, a_max=1.5, b=2.0, s0=2.0, T=1.5):
s_star = s0 + v * T + (v * dv) / (2 * math.sqrt(a_max * b))
return a_max * (1 - (v / v0) ** 4 - (s_star / max(gap, 0.1)) ** 2)
Putting It All Together: Demo
# Assumes all the functions/classes above are defined in the same file.
if __name__ == "__main__":
# 1. Pathfinding
city = [[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,1,0],
[1,1,0,0,0],
[0,0,0,1,0]]
print("Path:", astar(city, (0, 0), (4, 4)))
# 2. Steering
print("Steering force:", arrive((0, 0), (0, 0), (30, 40)))
# 3. Utility AI
state = {"threat": 0.8, "health": 0.4, "aggression": 0.3}
scores = utility_scores(state)
print("Utility scores:", scores, "-> chosen:", softmax_choice(scores))
# 4. Perception
p = detection_probability((0, 0), 0, (20, 5))
print("Detection probability:", round(p, 2))
# 5. Wanted level
w = WantedSystem()
w.commit("car_theft"); w.commit("assault")
print("Stars:", w.stars)
w.update(dt=30, seen_by_police=False)
print("Stars after hiding 30s:", w.stars)
# 6. Traffic
print("IDM acceleration:", round(idm_acceleration(v=12, gap=20, dv=3), 3))
Final Thoughts
Modern open-world games combine these ideas (plus behavior trees, navigation meshes, and machine-learning-assisted animation) to make cities feel alive. Experiment with the weights and constants above: changing a single number, like the heat decay rate or the softmax temperature, noticeably changes how the world behaves.
Educational content. Not affiliated with Rockstar Games. Grand Theft Auto is a trademark of Take-Two Interactive.Use AI

0 Comments