# Quickly update, clone, and iterate ball simulation

What the program does: I have made a program that simulates a ball in an area made of triangles and many optimizations because it's for high-performance ball path simulation (technically ball path prediction).

Vec3 and Ball definitions:

#[derive(Clone, Copy, Debug)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct Ball {
pub time: f32,
pub location: Vec3,
pub velocity: Vec3,
pub angular_velocity: Vec3,
pub moi: f32,
}


How it works right now: I have a function that takes all of the ball info/game info as well as the desired time to simulate the ball for. It converts the time into the number of game ticks that need to be run to actually simulate the ball for that time. It runs a loop for that number of ticks. For each tick, it calls another function, step(game: &mut Game, dt: f32). This runs all of the physics that would advance the game that one tick (CPU only, no graphical output whatsoever or GPU involvement.) I then throw the ball data into a Box and throw that into a Vec. This is so, when the Vec is returned, I basically have the predicted path of the ball, from game tick to game tick.

The function:

pub fn get_ball_prediction_struct_for_time(game: &mut Game, time: &f32) -> BallPrediction {
let num_slices = (time / Ball::SIMULATION_DT).round() as usize;
let mut slices = Vec::with_capacity(num_slices);

for _ in 0..num_slices {
Ball::step(game, Ball::SIMULATION_DT);
slices.push(Box::new(game.ball));
}

BallPrediction {
slices,
num_slices,
}
}


My question: Is putting the ball into a box and putting that into a Vec the best, most efficient way to do this? Is it possible for me to rather clone the balls into the stack (I believe Box stores it on the heap?) but then Vec stores stuff on the heap anyway, so does that even matter? How can I minimize moving all the data around?

I'm also new-er to Rust, so insight into Rust and its conventions is appreciated! I can share as might insight to the program as needed, this is an open-source hobby project of mine.

• There isn't enough information here to determine “most efficient”. We'll need to see the definition of Ball at least; preferably a complete runnable program. It's very likely that you should not be trying to “minimize moving”, and the Box is doing you no good, but more details of your data will help determine that. (And in the end, the only way to be sure about performance is to benchmark, not to read the code.) Nov 12, 2021 at 19:35
• @KevinReid Sure! Here's the github repo with tests - github.com/VirxEC/rl_ball_sym - it's also a crate on crates.io. The closest test to that is fast_predict_soccar, where you can really just swap out the functions (line 362) and put in a value for time. Nov 12, 2021 at 19:46
• I also added type definitions for Ball (and Vec3 in that.) Nov 12, 2021 at 19:48
• I actually just added a test called fast_predict_custom_soccar, it's in the repo for anyone to run. Nov 12, 2021 at 20:02