#bindings #cpp #math #arena #image #rocket-sim #rocketsim

rocketsim_rs

Rust bindings for the RocketSim project

62 releases (28 breaking)

new 0.29.1 May 16, 2024
0.28.3 May 15, 2024
0.26.0 Mar 23, 2024
0.24.2 Dec 27, 2023
0.11.0 Mar 29, 2023

#5 in Simulation

Download history 151/week @ 2024-02-17 40/week @ 2024-02-24 345/week @ 2024-03-02 54/week @ 2024-03-09 2/week @ 2024-03-16 142/week @ 2024-03-23 172/week @ 2024-03-30 12/week @ 2024-04-06 464/week @ 2024-05-04 674/week @ 2024-05-11

1,138 downloads per month
Used in 2 crates

MIT license

1.5MB
33K SLoC

C++ 30K SLoC // 0.1% comments Rust 2.5K SLoC // 0.0% comments

image

Build and test

Rust bindings for the RocketSim project

Basic usage

use rocketsim_rs::{
    math::Vec3,
    sim::{Arena, CarConfig, CarControls, Team},
};
use std::time::Instant;

fn main() {
    // Load in the Rocket League assets from the collision_meshes folder in the current directory
    rocketsim_rs::init(None);

    // Create a new arena with gamemode soccar and a tick rate of 120
    let mut arena = Arena::default_standard();
    println!("Arena tick rate: {}", arena.get_tick_rate());

    let car_id = arena.pin_mut().add_car(Team::Blue, CarConfig::octane());

    println!("Car id: {car_id}");

    {
        // custom initial car state
        let mut car_state = arena.pin_mut().get_car(car_id);

        car_state.pos = Vec3::new(5., 0., 50.);
        car_state.vel = Vec3::new(500., 800., 0.);
        car_state.boost = 100.;

        println!("Created custom car state");

        // Make the car boost
        arena
            .pin_mut()
            .set_car_controls(
                car_id,
                CarControls {
                    boost: true,
                    ..Default::default()
                },
            )
            .unwrap();

        // If car_id can't be found in arena than this will return Err
        arena.pin_mut().set_car(car_id, car_state).unwrap();

        println!("Set car ({car_id}) state");
    }

    {
        let mut ball_state = arena.pin_mut().get_ball();

        ball_state.pos.z = 1050.;
        ball_state.vel = Vec3::new(0., 0., 250.);

        arena.pin_mut().set_ball(ball_state);

        println!("Set ball state");
    }

    let ticks = 1800;
    let curr_time = Instant::now();

    arena.pin_mut().step(ticks);

    println!("Simulated {}s in {}ms", ticks as f32 / 120., curr_time.elapsed().as_millis());

    {
        // get the car state again
        let car_state = arena.pin_mut().get_car(car_id);

        println!("Got new car state");

        // You can debug the whole of the state
        // but it takes up a lot of space in stdout
        // dbg!(&car_state);

        println!("New car location: {}", car_state.pos);
        println!("New car boost: {}", car_state.boost);
    }

    // Cast the ball state position to a glam Vec3A
    #[cfg(feature = "glam")]
    println!("New ball location: {}", arena.pin_mut().get_ball().pos.to_glam());

    #[cfg(not(feature = "glam"))]
    println!("New ball location: {}", arena.pin_mut().get_ball().pos);
}

Benchmarks

Numbers are from a system running Ubuntu 23.10 with a Ryzen 9 5900X and 3600MHz CL18 RAM.

Numbers will vary depending on your system. Only default features are enabled.

  • real_bench:

      Running on 24 threads
      Simulated 2.78 hours in 1.485 seconds
      FPS: 808348.4
    
  • cpu_bench:

    Running on 24 threads
    Simulated 55.56 hours in 0.893 seconds
    FPS: 26883894
    
  • thread_bench (1 thread):

    Simulated 0.58 hours in 3.556 seconds
    FPS: 70312.516
    

Dependencies

~0.6–2.9MB
~54K SLoC