One of the fun math challenges we have run into during development is for controlling items in space. I wanted all the items to use real physics modeling to move from place to place. Physical simulation is pretty standard, but control systems are more complex as they don’t just simulate, they have to generate/calculate a solution to a problem. Given an item with position and velocity, I need it to move over time to a new position and have a resulting velocity relative to some other item. I couldn’t find an existing algorithm to do that. There were some related things like docking spaceships, but that was much more complex than I needed. After some dead ends, I realized that modeling it with separate acceleration/deceleration phases, I could create an overall system of equations to solve. With some old school algebra and some extra trickiness to deal with the fact that we use discrete timesteps… and after a good bit of testing and tweaking, we got it to work very well.
One of the fun things I did in that “good bit of testing” was create a random tester. It would iterate through thousands of tests. Each iteration would generate random values for position, relative velocity, target position, maximum acceleration, and timestep. Then it would run through the control calculation and update the physics for each step. Within a maximum number of iterations, it would confirm that it arrived at the target position and velocity. Trying to come up with a representative set of tests for this manually would have taken a great deal more effort. We did start with some basic manual tests to make sure the very basics were working first, but this random tester gave us the confidence that the control algorithm would work correctly. And since all the more complex control algorithms such as formations will use this one, it was very important to make sure it was right when we’re troubleshooting the other ones.