I've got a physics sim program that I'm working on that involves quite a bit of matrix multiplication, with generic sizing for some of the inputs. I'm hoping to add some scripting capability for programming new behavior at runtime but I'm running into issues with the way I've implemented generic physics objects.
The primary struct is (reduced here):
struct Object<const U: usize> {
u: na::SMatrix<f64, 6, U>,
// Other properties and fields
}
The setups are not known at compile time, and initialization is currently done through loading JSON files. The only way I could figure out how to do this was fairly ugly and basically boiled down to:
match u_from_json {
1 => Object::<1>::new(),
2 => Object::<2>::new(),
3 => Object::<3>::new(),
4 => Object::<4>::new(),
// Etc
}
Which works, but is not pretty in the slightest.
I'm trying to dramatically expand the capability of the program now, and I'd like to be able to implement custom runtime behavior through some kind of scripting language. For example, collision between two types of objects could produce some kind of control flow (stop the run, for example) or delete the colliding objects and produce a load of shrapnel instead. Or custom behavior for certain objects, such as something like:
Vehicle.run_guidance_script()
which would produce a new goal position based on whatever behavior I script in at runtime.
I've been trying to implement this through RHAI, and start by initialization, but I'm getting stuck with the pairing structs with generic parameters to the scripting language. While I'm fine to write a quick function to do the above matching on initialization, I'd really rather not write parallel APIs for each U
value. Surely there's a clean way to do this, right?
Any direction would be appreciated. I'd be ok with a refactor if necessary, but currently the simulation is extremely fast and I prefer not to make everything dynamic length if possible.