Skip to main content

snapshot_raycast/
snapshot_raycast.rs

1//! Snapshots, teleports, boost-pad state, and batched raycasts.
2//!
3//! Run with:
4//! ```sh
5//! cargo run -p rocketsim --example snapshot_raycast
6//! ```
7
8use glam::Vec3A;
9use rocketsim::{Arena, CarBodyConfig, GameMode, RaycastQuery, Team, init_from_default};
10
11fn main() {
12    init_from_default(true).unwrap();
13    let mut arena = Arena::new(GameMode::Soccar);
14    let car_idx = arena.add_car(Team::Blue, CarBodyConfig::OCTANE);
15    arena.reset_to_random_kickoff(Some(7));
16
17    // 1. Snapshot everything (cars, ball, pads) for replays/debugging.
18    let snapshot = arena.get_arena_state();
19    println!(
20        "cars: {}, pads: {}",
21        snapshot.num_cars(),
22        snapshot.num_boost_pads()
23    );
24
25    // 2. Teleport the ball above the car and let it drop for a second.
26    let mut ball_state = *arena.get_ball_state();
27    let car_state = arena.get_car_state(car_idx);
28    ball_state.phys.pos = car_state.phys.pos + Vec3A::new(0.0, 0.0, 500.0);
29    ball_state.phys.vel = Vec3A::ZERO;
30    arena.set_ball_state(ball_state);
31
32    for _ in 0..120 {
33        arena.step_tick();
34    }
35    println!("ball fell to: {}", arena.get_ball_state().pos);
36
37    // 3. Boost pads: layout is RLBot/RLGym ordered (Y, then X).
38    println!("pads: {}", arena.num_boost_pads());
39    let first_config = *arena.get_boost_pad_config(0);
40    let first_state = arena.get_boost_pad_state(0);
41    println!(
42        "pad 0 at {} (big: {}), active: {}",
43        first_config.pos,
44        first_config.is_big,
45        first_state.is_active()
46    );
47
48    // 4. Raycasts are SIMD-batched in 4s — send multiples of 4 when possible.
49    // Coordinates are in Unreal units, like the rest of the API.
50    let ball_pos = arena.get_ball_state().pos + Vec3A::new(500.0, 0.0, 0.0);
51    let down = RaycastQuery {
52        from: ball_pos,
53        to: ball_pos - Vec3A::new(0.0, 0.0, 300.0),
54        include_dynamics: false,
55    };
56    let queries = [down; 4];
57    let results = arena.cast_rays(queries);
58    for (i, result) in results.iter().enumerate() {
59        match result {
60            Some(hit) => println!(
61                "ray {i}: hit at {} (t={:.3}, {:?})",
62                hit.hit_point, hit.hit_fraction, hit.user_info
63            ),
64            None => println!("ray {i}: miss"),
65        }
66    }
67
68    // 5. Overlap tests: would something fit here without stepping the sim?
69    // (cars use the hitbox only — a car at rest height clears the floor).
70    let mut high_ball = arena.get_ball_state().phys;
71    high_ball.pos = Vec3A::new(0.0, 0.0, 3000.0);
72    println!(
73        "ball at 3000uu collides: {}",
74        arena.test_collision(high_ball, None, false)
75    );
76    println!(
77        "car at 3000uu collides: {}",
78        arena.test_collision(high_ball, Some(CarBodyConfig::OCTANE), false)
79    );
80    let mut in_wall = arena.get_ball_state().phys;
81    in_wall.pos = Vec3A::new(4096.0, 0.0, 100.0);
82    println!(
83        "ball inside side wall collides: {}",
84        arena.test_collision(in_wall, None, false)
85    );
86
87    // 6. Restore: teleport back to the snapshot's ball state.
88    arena.set_ball_state(snapshot.ball);
89    println!("restored to: {}", arena.get_ball_state().pos);
90}