raasta 1.0.0

Raasta — navigation and pathfinding engine for AGNOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Steering behaviors — seek, flee, arrive, obstacle avoidance.

use hisab::Vec2;
use serde::{Deserialize, Serialize};

/// A circular obstacle for avoidance steering.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Obstacle {
    pub center: Vec2,
    pub radius: f32,
}

/// A steering behavior that produces a desired velocity/force.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SteerBehavior {
    /// Move toward a target position.
    Seek { target: Vec2 },
    /// Move away from a target position.
    Flee { target: Vec2 },
    /// Move toward a target, slowing down as it approaches.
    Arrive {
        target: Vec2,
        /// Distance at which to start slowing down.
        slow_radius: f32,
    },
}

/// Output of a steering computation.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct SteerOutput {
    /// Desired velocity direction and magnitude.
    pub velocity: Vec2,
}

impl SteerOutput {
    #[cfg_attr(feature = "logging", tracing::instrument)]
    #[must_use]
    pub fn new(vx: f32, vy: f32) -> Self {
        Self {
            velocity: Vec2::new(vx, vy),
        }
    }

    /// Construct from a `Vec2` directly.
    #[cfg_attr(feature = "logging", tracing::instrument)]
    #[must_use]
    pub fn from_vec2(velocity: Vec2) -> Self {
        Self { velocity }
    }

    /// Magnitude of the velocity.
    #[cfg_attr(feature = "logging", tracing::instrument(skip(self)))]
    #[inline]
    #[must_use]
    pub fn speed(&self) -> f32 {
        self.velocity.length()
    }
}

/// Compute steering output for the given behavior.
///
/// - `position`: current agent position
/// - `max_speed`: maximum speed the agent can move
#[cfg_attr(feature = "logging", tracing::instrument)]
#[inline]
#[must_use]
pub fn compute_steer(behavior: &SteerBehavior, position: Vec2, max_speed: f32) -> SteerOutput {
    match behavior {
        SteerBehavior::Seek { target } => {
            let desired = *target - position;
            let len = desired.length();
            if len < f32::EPSILON {
                return SteerOutput::default();
            }
            SteerOutput::from_vec2(desired / len * max_speed)
        }
        SteerBehavior::Flee { target } => {
            let desired = position - *target;
            let len = desired.length();
            if len < f32::EPSILON {
                return SteerOutput::default();
            }
            SteerOutput::from_vec2(desired / len * max_speed)
        }
        SteerBehavior::Arrive {
            target,
            slow_radius,
        } => {
            let desired = *target - position;
            let dist = desired.length();
            if dist < f32::EPSILON {
                return SteerOutput::default();
            }
            let speed = if dist < *slow_radius {
                max_speed * (dist / slow_radius)
            } else {
                max_speed
            };
            SteerOutput::from_vec2(desired / dist * speed)
        }
    }
}

/// Compute pursuit steering — intercept a moving target by predicting its future position.
///
/// `target_pos` and `target_vel` are the target's current position and velocity.
/// `prediction_factor` scales how far ahead to predict (typically 1.0).
#[inline]
#[must_use]
pub fn pursuit(position: Vec2, target_pos: Vec2, target_vel: Vec2, max_speed: f32) -> SteerOutput {
    let to_target = target_pos - position;
    let dist = to_target.length();
    if dist < f32::EPSILON {
        return SteerOutput::default();
    }
    // Predict future position based on distance (further = more prediction)
    let prediction_time = dist / max_speed;
    let predicted = target_pos + target_vel * prediction_time;
    compute_steer(
        &SteerBehavior::Seek { target: predicted },
        position,
        max_speed,
    )
}

/// Compute evasion steering — flee from a moving target's predicted future position.
#[inline]
#[must_use]
pub fn evade(position: Vec2, target_pos: Vec2, target_vel: Vec2, max_speed: f32) -> SteerOutput {
    let to_target = target_pos - position;
    let dist = to_target.length();
    if dist < f32::EPSILON {
        return SteerOutput::default();
    }
    let prediction_time = dist / max_speed;
    let predicted = target_pos + target_vel * prediction_time;
    compute_steer(
        &SteerBehavior::Flee { target: predicted },
        position,
        max_speed,
    )
}

/// Compute wander steering — natural-looking random movement.
///
/// Projects a circle of `wander_radius` at `wander_distance` ahead of the
/// agent, then displaces the target by `wander_angle` radians on that circle.
///
/// Call with a varying `wander_angle` each tick (e.g., previous angle ± small random delta)
/// for smooth wandering.
#[inline]
#[must_use]
pub fn wander(
    position: Vec2,
    velocity: Vec2,
    max_speed: f32,
    wander_distance: f32,
    wander_radius: f32,
    wander_angle: f32,
) -> SteerOutput {
    let speed = velocity.length();
    let forward = if speed > f32::EPSILON {
        velocity / speed
    } else {
        Vec2::new(1.0, 0.0)
    };

    let circle_center = position + forward * wander_distance;
    let displacement = Vec2::new(wander_angle.cos(), wander_angle.sin()) * wander_radius;
    let target = circle_center + displacement;

    compute_steer(&SteerBehavior::Seek { target }, position, max_speed)
}

/// Compute separation steering — steer away from nearby neighbors.
///
/// Returns a force that pushes the agent away from neighbors within `radius`.
/// Closer neighbors produce stronger repulsion.
#[inline]
#[must_use]
pub fn separation(position: Vec2, neighbors: &[Vec2], radius: f32, max_force: f32) -> SteerOutput {
    let mut force = Vec2::ZERO;
    let mut count = 0;

    for &neighbor in neighbors {
        let diff = position - neighbor;
        let dist = diff.length();
        if dist > f32::EPSILON && dist < radius {
            // Weight inversely by distance
            force += diff / (dist * dist);
            count += 1;
        }
    }

    if count > 0 {
        force /= count as f32;
        let len = force.length();
        if len > f32::EPSILON {
            force = force / len * max_force;
        }
    }

    SteerOutput::from_vec2(force)
}

/// Compute alignment steering — steer toward the average heading of neighbors.
#[inline]
#[must_use]
pub fn alignment(velocity: Vec2, neighbor_velocities: &[Vec2], max_force: f32) -> SteerOutput {
    if neighbor_velocities.is_empty() {
        return SteerOutput::default();
    }

    let avg: Vec2 =
        neighbor_velocities.iter().copied().sum::<Vec2>() / neighbor_velocities.len() as f32;
    let desired = avg - velocity;
    let len = desired.length();
    if len < f32::EPSILON {
        return SteerOutput::default();
    }
    SteerOutput::from_vec2(desired / len * max_force)
}

/// Compute cohesion steering — steer toward the center of mass of neighbors.
#[inline]
#[must_use]
pub fn cohesion(position: Vec2, neighbors: &[Vec2], max_speed: f32) -> SteerOutput {
    if neighbors.is_empty() {
        return SteerOutput::default();
    }

    let center: Vec2 = neighbors.iter().copied().sum::<Vec2>() / neighbors.len() as f32;
    compute_steer(&SteerBehavior::Seek { target: center }, position, max_speed)
}

/// Compute an avoidance steering force away from nearby obstacles.
///
/// Casts a ray from `position` along `velocity` up to `look_ahead` distance.
/// If any obstacle intersects that ray, returns a lateral steering force that
/// pushes the agent away from the nearest one. The force magnitude scales
/// with `max_force` and is stronger for closer obstacles.
///
/// Returns zero if no obstacles are within the look-ahead cone.
///
/// - `position`: current agent position
/// - `velocity`: current agent velocity (direction of travel)
/// - `obstacles`: slice of circular obstacles to avoid
/// - `look_ahead`: how far ahead to check for obstacles
/// - `max_force`: maximum avoidance force magnitude
#[inline]
#[must_use]
pub fn avoid_obstacles(
    position: Vec2,
    velocity: Vec2,
    obstacles: &[Obstacle],
    look_ahead: f32,
    max_force: f32,
) -> SteerOutput {
    let speed = velocity.length();
    if speed < f32::EPSILON {
        return SteerOutput::default();
    }

    let forward = velocity / speed;
    // Lateral (perpendicular) direction — always turn the same way for consistency
    let lateral = Vec2::new(-forward.y, forward.x);

    let mut nearest_t = f32::INFINITY;
    let mut nearest_lateral_offset = 0.0f32;
    let mut nearest_dist_sq = f32::INFINITY;

    for obs in obstacles {
        let to_obs = obs.center - position;
        // Project obstacle center onto forward axis
        let forward_dot = to_obs.dot(forward);

        // Behind us or beyond look-ahead — skip
        if forward_dot < -obs.radius || forward_dot > look_ahead + obs.radius {
            continue;
        }

        // Lateral distance from the ray to obstacle center
        let lateral_dot = to_obs.dot(lateral);
        let overlap = obs.radius - lateral_dot.abs();

        // No intersection with the ray corridor
        if overlap < 0.0 {
            continue;
        }

        // Track the nearest obstacle by forward projection
        let dist_sq = to_obs.length_squared();
        if forward_dot < nearest_t || (forward_dot == nearest_t && dist_sq < nearest_dist_sq) {
            nearest_t = forward_dot;
            nearest_dist_sq = dist_sq;
            // Steer away from whichever side the obstacle is on
            nearest_lateral_offset = if lateral_dot >= 0.0 { -1.0 } else { 1.0 };
        }
    }

    if nearest_t == f32::INFINITY {
        return SteerOutput::default();
    }

    // Force scales inversely with distance — closer obstacles get stronger avoidance
    let urgency = 1.0 - (nearest_t / (look_ahead + f32::EPSILON)).clamp(0.0, 1.0);
    let force = lateral * nearest_lateral_offset * max_force * urgency;
    SteerOutput::from_vec2(force)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn seek_toward_target() {
        let out = compute_steer(
            &SteerBehavior::Seek {
                target: Vec2::new(10.0, 0.0),
            },
            Vec2::ZERO,
            5.0,
        );
        assert!((out.velocity.x - 5.0).abs() < 0.01);
        assert!(out.velocity.y.abs() < 0.01);
    }

    #[test]
    fn flee_from_target() {
        let out = compute_steer(
            &SteerBehavior::Flee {
                target: Vec2::new(10.0, 0.0),
            },
            Vec2::ZERO,
            5.0,
        );
        assert!((out.velocity.x - (-5.0)).abs() < 0.01);
    }

    #[test]
    fn arrive_full_speed() {
        let out = compute_steer(
            &SteerBehavior::Arrive {
                target: Vec2::new(100.0, 0.0),
                slow_radius: 10.0,
            },
            Vec2::ZERO,
            5.0,
        );
        assert!((out.speed() - 5.0).abs() < 0.01);
    }

    #[test]
    fn arrive_slow_down() {
        let out = compute_steer(
            &SteerBehavior::Arrive {
                target: Vec2::new(5.0, 0.0),
                slow_radius: 10.0,
            },
            Vec2::ZERO,
            10.0,
        );
        // At distance 5 with slow_radius 10, speed should be 5.0 (half)
        assert!((out.speed() - 5.0).abs() < 0.01);
    }

    #[test]
    fn arrive_at_target() {
        let out = compute_steer(
            &SteerBehavior::Arrive {
                target: Vec2::new(5.0, 5.0),
                slow_radius: 10.0,
            },
            Vec2::new(5.0, 5.0),
            10.0,
        );
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn seek_at_target() {
        let out = compute_steer(&SteerBehavior::Seek { target: Vec2::ZERO }, Vec2::ZERO, 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn flee_at_target() {
        let out = compute_steer(&SteerBehavior::Flee { target: Vec2::ZERO }, Vec2::ZERO, 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn steer_output_speed() {
        let out = SteerOutput::new(3.0, 4.0);
        assert!((out.speed() - 5.0).abs() < 0.01);
    }

    #[test]
    fn seek_diagonal() {
        let out = compute_steer(
            &SteerBehavior::Seek {
                target: Vec2::new(10.0, 10.0),
            },
            Vec2::ZERO,
            1.0,
        );
        // Speed should be max_speed
        assert!((out.speed() - 1.0).abs() < 0.01);
        // Direction should be ~45 degrees (equal x and y)
        assert!((out.velocity.x - out.velocity.y).abs() < 0.01);
    }

    #[test]
    fn flee_negative_coords() {
        let out = compute_steer(
            &SteerBehavior::Flee {
                target: Vec2::new(-10.0, -10.0),
            },
            Vec2::ZERO,
            5.0,
        );
        // Should flee in positive direction
        assert!(out.velocity.x > 0.0);
        assert!(out.velocity.y > 0.0);
        assert!((out.speed() - 5.0).abs() < 0.01);
    }

    #[test]
    fn arrive_at_slow_radius_boundary() {
        let out = compute_steer(
            &SteerBehavior::Arrive {
                target: Vec2::new(10.0, 0.0),
                slow_radius: 10.0,
            },
            Vec2::ZERO,
            10.0,
        );
        // Exactly at slow_radius distance — speed should equal max_speed
        assert!((out.speed() - 10.0).abs() < 0.01);
    }

    #[test]
    fn steer_output_zero_speed() {
        let out = SteerOutput::default();
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn steer_serde_roundtrip() {
        let b = SteerBehavior::Arrive {
            target: Vec2::new(1.0, 2.0),
            slow_radius: 5.0,
        };
        let json = serde_json::to_string(&b).unwrap();
        let deserialized: SteerBehavior = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, b);
    }

    // --- Obstacle avoidance tests ---

    #[test]
    fn avoid_no_obstacles() {
        let out = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[], 10.0, 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn avoid_obstacle_ahead() {
        let obs = Obstacle {
            center: Vec2::new(5.0, 0.5),
            radius: 1.0,
        };
        let out = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[obs], 10.0, 5.0);
        // Should produce a lateral force (non-zero y, ~zero x contribution)
        assert!(out.speed() > 0.0);
        // Obstacle is slightly above the ray — should steer downward (negative y)
        assert!(out.velocity.y < 0.0);
    }

    #[test]
    fn avoid_obstacle_behind() {
        let obs = Obstacle {
            center: Vec2::new(-5.0, 0.0),
            radius: 1.0,
        };
        let out = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[obs], 10.0, 5.0);
        // Obstacle is behind — no avoidance
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn avoid_obstacle_far_lateral() {
        let obs = Obstacle {
            center: Vec2::new(5.0, 10.0),
            radius: 1.0,
        };
        let out = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[obs], 10.0, 5.0);
        // Obstacle is far to the side — no intersection
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn avoid_nearer_obstacle_preferred() {
        let near = Obstacle {
            center: Vec2::new(3.0, 0.5),
            radius: 1.0,
        };
        let far = Obstacle {
            center: Vec2::new(8.0, 0.5),
            radius: 1.0,
        };
        let out_both = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[far, near], 10.0, 5.0);
        let out_near = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[near], 10.0, 5.0);
        // Should react to the nearer obstacle — same direction
        assert!((out_both.velocity.y - out_near.velocity.y).abs() < 0.01);
    }

    #[test]
    fn avoid_zero_velocity() {
        let obs = Obstacle {
            center: Vec2::new(5.0, 0.0),
            radius: 1.0,
        };
        let out = avoid_obstacles(Vec2::ZERO, Vec2::ZERO, &[obs], 10.0, 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn avoid_urgency_scales_with_distance() {
        let close = Obstacle {
            center: Vec2::new(2.0, 0.5),
            radius: 1.0,
        };
        let far = Obstacle {
            center: Vec2::new(9.0, 0.5),
            radius: 1.0,
        };
        let out_close = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[close], 10.0, 5.0);
        let out_far = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[far], 10.0, 5.0);
        // Closer obstacle should produce stronger force
        assert!(out_close.speed() > out_far.speed());
    }

    #[test]
    fn avoid_beyond_look_ahead() {
        let obs = Obstacle {
            center: Vec2::new(15.0, 0.0),
            radius: 1.0,
        };
        let out = avoid_obstacles(Vec2::ZERO, Vec2::new(1.0, 0.0), &[obs], 10.0, 5.0);
        // Beyond look-ahead — no avoidance
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn obstacle_serde_roundtrip() {
        let obs = Obstacle {
            center: Vec2::new(1.0, 2.0),
            radius: 3.0,
        };
        let json = serde_json::to_string(&obs).unwrap();
        let deserialized: Obstacle = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, obs);
    }

    // --- Pursuit / Evade / Wander / Flocking tests ---

    #[test]
    fn pursuit_intercepts() {
        // Target moving right, agent behind and below
        let out = pursuit(
            Vec2::new(0.0, -5.0),
            Vec2::new(5.0, 0.0),
            Vec2::new(1.0, 0.0),
            5.0,
        );
        // Should aim ahead of target (positive x component)
        assert!(out.velocity.x > 0.0);
        assert!(out.speed() > 0.0);
    }

    #[test]
    fn evade_flees_predicted() {
        let out = evade(
            Vec2::ZERO,
            Vec2::new(5.0, 0.0),
            Vec2::new(-1.0, 0.0), // target approaching
            5.0,
        );
        // Should flee from predicted position (which is closer)
        assert!(out.velocity.x < 0.0);
    }

    #[test]
    fn wander_produces_movement() {
        let out = wander(
            Vec2::ZERO,
            Vec2::new(1.0, 0.0),
            5.0,
            2.0, // distance
            1.0, // radius
            0.5, // angle
        );
        assert!(out.speed() > 0.0);
    }

    #[test]
    fn wander_stationary_agent() {
        let out = wander(Vec2::ZERO, Vec2::ZERO, 5.0, 2.0, 1.0, 0.0);
        // Uses default forward direction, should still produce movement
        assert!(out.speed() > 0.0);
    }

    #[test]
    fn separation_pushes_apart() {
        let neighbors = vec![Vec2::new(1.0, 0.0), Vec2::new(-1.0, 0.0)];
        let out = separation(Vec2::ZERO, &neighbors, 5.0, 10.0);
        // Equal neighbors on both sides — should roughly cancel, low force
        assert!(out.speed() < 1.0);
    }

    #[test]
    fn separation_one_neighbor() {
        let neighbors = vec![Vec2::new(1.0, 0.0)];
        let out = separation(Vec2::ZERO, &neighbors, 5.0, 10.0);
        // Should push away from neighbor (negative x)
        assert!(out.velocity.x < 0.0);
    }

    #[test]
    fn separation_no_neighbors() {
        let out = separation(Vec2::ZERO, &[], 5.0, 10.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn separation_out_of_range() {
        let neighbors = vec![Vec2::new(100.0, 0.0)];
        let out = separation(Vec2::ZERO, &neighbors, 5.0, 10.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn alignment_matches_heading() {
        let neighbor_vels = vec![Vec2::new(3.0, 0.0), Vec2::new(3.0, 0.0)];
        let out = alignment(Vec2::new(1.0, 0.0), &neighbor_vels, 5.0);
        // Should steer to match neighbors (increase x velocity)
        assert!(out.velocity.x > 0.0);
    }

    #[test]
    fn alignment_no_neighbors() {
        let out = alignment(Vec2::new(1.0, 0.0), &[], 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn cohesion_toward_center() {
        let neighbors = vec![Vec2::new(10.0, 0.0), Vec2::new(10.0, 10.0)];
        let out = cohesion(Vec2::ZERO, &neighbors, 5.0);
        // Should steer toward center of mass (positive x and y)
        assert!(out.velocity.x > 0.0);
        assert!(out.velocity.y > 0.0);
    }

    #[test]
    fn cohesion_no_neighbors() {
        let out = cohesion(Vec2::ZERO, &[], 5.0);
        assert!(out.speed() < f32::EPSILON);
    }

    #[test]
    fn steer_output_serde_roundtrip() {
        let out = SteerOutput::new(3.0, 4.0);
        let json = serde_json::to_string(&out).unwrap();
        let deserialized: SteerOutput = serde_json::from_str(&json).unwrap();
        assert!((deserialized.speed() - 5.0).abs() < 0.01);
    }
}