📘 Ian Millington "AI for Games" 3rd Ed.

BAB 3: MOVEMENT

Perilaku Gerakan dalam Game AI

"One of the most fundamental requirements of game AI is to move characters around sensibly." — Millington, 2019

Deskripsi: Bab ini membahas algoritma pergerakan untuk karakter dalam game AI. Millington membagi menjadi dua pendekatan utama: kinematic movement (tanpa akselerasi) dan steering behaviors (dengan akselerasi). Dasar dari semua perilaku gerakan adalah bagaimana karakter bergerak dari posisi saat ini ke target dengan cara yang realistis dan efisien.

Sumber: Millington, Chapter 3, p. 47-49

3.1 Dasar Movement Algorithms

Statics vs Kinematics

Static: posisi + orientasi, kecepatan nol.

Kinematic: posisi + orientasi + kecepatan + rotasi.

Rumus Kinematic:
p' = p + v·Δt
θ' = θ + ω·Δt

Deskripsi: "Kinematic movement algorithms use only position and orientation data, ignoring acceleration. They're simpler but less realistic. The character's velocity is constant, and orientation changes at a constant rate." — Millington, p. 50. Kinematic movement cocok untuk NPC sederhana atau ketika performa menjadi prioritas utama.

Sumber: Millington, Section 3.1, p. 50-51

3.2 Kinematic Movement Algorithms

Kinematic Seek

// Pseudocode Kinematic Seek velocity = target.position - character.position velocity.normalize() velocity *= maxSpeed orientation = atan2(-velocity.x, velocity.z)

Deskripsi: "Kinematic seek sets the character's velocity to point towards the target at maximum speed. The orientation is then set to match the direction of movement." — Millington, p. 53. Algoritma ini sangat sederhana namun menghasilkan gerakan yang kaku karena tidak ada perlambatan saat mendekati target.

Sumber: Millington, Section 3.2.1, p. 53-54

3.2 Kinematic Wander

Mengacak orientasi setiap frame, lalu bergerak maju.

orientation += randomBinomial() * maxRotation velocity = orientation.asVector() * maxSpeed

Deskripsi: "The kinematic wander behavior is the simplest way to achieve random-looking movement. It changes the character's orientation randomly each frame, then moves forward at maximum speed." — Millington, p. 55. Meskipun sederhana, hasilnya sering terlihat tidak alami karena perubahan arah yang terlalu tiba-tiba.

Sumber: Millington, Section 3.2.3, p. 55-56

3.3 Steering Behaviors

"Steering behaviors calculate a force (linear acceleration) and angular acceleration to apply to a character."

Linear acceleration: mengubah kecepatan

Angular acceleration: mengubah rotasi

Deskripsi: "Steering behaviors are the foundation of more realistic movement. Instead of setting velocity directly, they apply accelerations that change velocity over time, producing smooth, natural-looking motion." — Millington, p. 57. Pendekatan ini memungkinkan kombinasi multiple behaviors dan reaksi yang lebih halus terhadap lingkungan.

Sumber: Millington, Section 3.3, p. 57-59

3.3.3 Seek dan Flee

// Steering Seek direction = target.position - character.position steering.linear = direction.normalized * maxAcceleration // Steering Flee steering.linear = (character.position - target.position).normalized * maxAcceleration

Deskripsi: "Seek accelerates the character toward the target, while Flee accelerates away from it. These are the simplest steering behaviors and form the basis for more complex behaviors." — Millington, p. 60. Seek akan menyebabkan karakter terus bergerak meskipun sudah melewati target (overshoot), berbeda dengan Arrive yang melambat.

Sumber: Millington, Section 3.3.3, p. 60-61

3.3.4 Arrive

Millington: "Arrive slows down as it approaches the target, coming to a gentle stop."

distance = |target - position| if distance < targetRadius: stop targetSpeed = maxSpeed * (distance / slowRadius) targetVelocity = direction * targetSpeed acceleration = (targetVelocity - velocity) / timeToTarget

Deskripsi: "The Arrive behavior is a modification of Seek that brings the character to a gentle stop at the target. It uses two radii: the slow radius (where deceleration begins) and the target radius (where the character is close enough to stop)." — Millington, p. 62. Parameter timeToTarget mengontrol seberapa cepat perlambatan terjadi.

Sumber: Millington, Section 3.3.4, p. 62-63

3.3.5 Align

Versi angular dari Arrive. Menyamakan orientasi dengan target.

rotation = target.orientation - character.orientation rotation = mapToRange(rotation) rotationSize = |rotation| if rotationSize < targetRadius: stop targetRotation = maxRotation * (rotationSize / slowRadius) angularAccel = (targetRotation - character.rotation) / timeToTarget

Deskripsi: "Align tries to match the orientation of the character with that of the target. It's the rotational equivalent of Arrive, using the same deceleration logic to avoid overshooting." — Millington, p. 64. Fungsi mapToRange penting untuk memastikan rotasi mengambil jalur terpendek (tidak memutar 270° jika bisa 90°).

Sumber: Millington, Section 3.3.5, p. 64-65

3.3.6 Velocity Matching

Menyamakan kecepatan dengan target. Digunakan dalam flocking.

steering.linear = (target.velocity - character.velocity) / timeToTarget

Deskripsi: "Velocity matching produces an acceleration that attempts to match the character's velocity with that of the target. This is essential for group behaviors like flocking, where characters need to move together at similar speeds." — Millington, p. 66. Berbeda dengan Seek yang fokus pada posisi, velocity matching hanya peduli pada kecepatan.

Sumber: Millington, Section 3.3.6, p. 66-67

3.3.8 Pursue dan Evade

"Pursue predicts where the target will be in the future and seeks to that predicted position."

prediction = target.position + target.velocity * predictionTime Seek(prediction)

Deskripsi: "Pursue predicts the target's future position based on its current velocity and seeks to that predicted point. This produces more intelligent chasing than simple Seek, especially when the target is moving." — Millington, p. 69. Prediction time biasanya dihitung berdasarkan jarak atau kecepatan relatif.

Sumber: Millington, Section 3.3.8, p. 69-70

3.3.9 Face & 3.3.10 Look Where You're Going

Face: berputar menghadap target. Look: menghadap arah gerak.

Deskripsi: "Face is a delegated behavior that calculates the direction to a target and then uses Align to face that direction. Look Where You're Going faces the character in the direction it is moving, which is useful for vehicles or characters that should always face forward." — Millington, p. 71-72. Face berguna untuk karakter yang perlu "melihat" target seperti dalam dialog atau pertempuran.

Sumber: Millington, Section 3.3.9-10, p. 71-73

3.3.11 Wander (Steering)

Versi steering: lingkaran di depan karakter, target acak di lingkaran.

wanderOrientation += randomBinomial() * wanderRate target = circleCenter + wanderRadius * targetOrientation.asVector()

Deskripsi: "The steering wander behavior solves the jerky movement of kinematic wander by constraining the target to a circle in front of the character. The target moves randomly on this circle, and the character seeks that target, producing smooth changes in direction." — Millington, p. 74. Ini adalah implementasi wander yang digunakan dalam demo boids Craig Reynolds.

Sumber: Millington, Section 3.3.11, p. 74-75; Reynolds, "Steering Behaviors for Autonomous Characters", 1999

3.3.12 Path Following

Mengikuti path dengan memproyeksikan posisi ke path.

Deskripsi: "Path Following creates a steering force that keeps the character moving along a path. The algorithm finds the nearest point on the path, projects ahead by a fixed distance, and seeks to that projected point. This naturally handles curves and corners." — Millington, p. 76. Path dapat berupa garis lurus atau kurva spline.

Sumber: Millington, Section 3.3.12, p. 76-78

3.3.13 Separation

"Separation produces a force to move a character away from others that are too close."

Deskripsi: "Separation is a group behavior that keeps characters from getting too close to each other. The force is inversely proportional to the distance, becoming very strong when characters are near each other. This is one of the three rules in Reynolds' flocking algorithm." — Millington, p. 79. Separation sering dikombinasikan dengan cohesion dan alignment untuk membentuk flocking.

Sumber: Millington, Section 3.3.13, p. 79-80; Reynolds, "Flocking", 1987

3.3.14 Collision Avoidance

Memprediksi tabrakan berdasarkan kecepatan relatif.

Deskripsi: "Collision avoidance predicts the future position of potential collisions based on current velocities. It calculates the time to closest approach and if that time is small enough and the distance at that time is below a threshold, it generates a force to avoid the collision." — Millington, p. 82. Berbeda dengan separation yang bereaksi terhadap jarak saat ini, collision avoidance bersifat prediktif.

Sumber: Millington, Section 3.3.14, p. 82-84

3.4.2 Weighted Blending

"Behaviors are combined by taking a weighted sum of their accelerations."

totalLinear = Σ (weight_i * steering_i.linear) totalAngular = Σ (weight_i * steering_i.angular)

Deskripsi: "Weighted blending is the simplest way to combine multiple steering behaviors. Each behavior contributes a force multiplied by its weight, and the results are summed. This is how flocking combines separation, cohesion, and alignment." — Millington, p. 86. Bobot bisa diatur secara manual atau dihasilkan melalui pembelajaran mesin (walaupun hasilnya sering kurang memuaskan).

Sumber: Millington, Section 3.4.2, p. 86-88

3.4.3 Priorities

"Groups are arranged in priority order. If the first group produces a significant result, it is used; otherwise, the next group is tried."

Deskripsi: "Priority-based steering arranges behaviors in groups with decreasing priority. If a higher-priority group produces a significant acceleration (above a threshold), lower groups are ignored. This ensures critical behaviors like collision avoidance always take precedence." — Millington, p. 90. Nilai epsilon menentukan seberapa besar akselerasi dianggap signifikan.

Sumber: Millington, Section 3.4.3, p. 90-92

3.4.5 Steering Pipeline

Target → Decomposer → Constraint → Actuator

Deskripsi: "The steering pipeline is a more sophisticated architecture for combining behaviors. It consists of four stages: targeters (generate movement goals), decomposers (break goals into sub-goals), constraints (modify goals to avoid obstacles), and actuators (convert goals into movement requests)." — Millington, p. 94. Pipeline ini mirip dengan arsitektur dalam robotika.

Sumber: Millington, Section 3.4.5, p. 94-98

3.5.3 The Firing Solution

Persamaan gerak proyektil tanpa drag:

p(t) = p0 + u·s_m·t + ½g·t²

Deskripsi: "The firing solution calculates the launch angle needed to hit a target with a projectile under gravity. The equation accounts for muzzle velocity and gravitational acceleration. For moving targets, the target's future position must be predicted." — Millington, p. 101. Dalam game, nilai g sering dibesarkan (misalnya 18-20 m/s²) untuk memberikan feel yang lebih baik.

Sumber: Millington, Section 3.5.3, p. 101-104

3.5.5 Iterative Targeting

Untuk drag: gunakan binary search untuk menemukan sudut tembak.

Deskripsi: "When drag is involved, the trajectory is no longer a simple parabola. Iterative targeting uses simulation and binary search to find the correct firing angle. It guesses an angle, simulates the flight, and adjusts until the projectile lands within an acceptable radius of the target." — Millington, p. 106. Metode ini membutuhkan physics engine yang bisa menjalankan simulasi terisolasi.

Sumber: Millington, Section 3.5.5, p. 106-108

3.6 Jumping

"Jump points are locations in the game where a character can jump between platforms."

Deskripsi: "Jumping requires careful coordination: the character must approach the jump point with the correct velocity and direction, then execute the jump at the right moment. The trajectory is predetermined by physics, so the AI must ensure the character is moving fast enough and in the right direction to land safely." — Millington, p. 110. Jump points sering ditempatkan manual oleh designer untuk menjamin keberhasilan.

Sumber: Millington, Section 3.6, p. 110-114

3.7.1 Fixed Formations

"Slot positions are defined relative to the leader's position and orientation."

Deskripsi: "In fixed formations, slot positions are defined relative to a leader character. The formation moves as a whole, and each character tries to stay in its assigned slot. This is simple to implement but can cause problems when the leader moves through narrow spaces." — Millington, p. 115. Formasi tetap sering digunakan dalam game strategi real-time.

Sumber: Millington, Section 3.7.1, p. 115-117

3.7.3 Emergent Formations

"Each character uses steering behaviors to stay in formation, reacting to neighbors."

Deskripsi: "Emergent formations arise from local rules rather than global positions. Each character uses steering behaviors (like separation and cohesion) to maintain formation with nearby characters. This is more flexible and handles obstacles better, but it's harder to guarantee a specific shape." — Millington, p. 119. Contoh terkenal adalah formasi V pada burung yang muncul secara alami.

Sumber: Millington, Section 3.7.3, p. 119-121

3.8 Motor Control

"Actuators convert steering requests into actual motion, respecting physical constraints."

Deskripsi: "Motor control bridges the gap between steering requests and what the character can actually do. For human characters, this might mean turning in place before moving. For vehicles, it involves acceleration, braking, and turning constraints based on physics." — Millington, p. 123. Heuristik khusus digunakan untuk berbagai jenis karakter (manusia, mobil, tank).

Sumber: Millington, Section 3.8, p. 123-127

Ringkasan Bab 3

Mencakup:

Deskripsi: "Movement is the most visible aspect of game AI. Well-implemented movement behaviors create the illusion of intelligence and make game worlds feel alive. The algorithms in this chapter form the foundation for everything from simple patrolling NPCs to complex squad tactics in strategy games." — Millington, p. 128.

Sumber: Millington, Chapter 3, p. 47-128