Today, we released our third and most (mathematically) sophisticated simulation yet, Magnetic Fields.
The Magnetic Fields simulation helps you better understand and teach how currents in a wire induce a magnetic field. Magnetic fields are an abstract concept to many and hard to visualize in the real world. In VR, we don’t have any of those limitations. No awkward hand rules or “o” or “x” needed.
In this post, I want to give you some more development background into how we built this simulation. It wasn’t straight-forward, but a highly interesting problem to solve.
Here’s a short video of how it looks (pro tip: it looks much cooler in VR):
"Gut Ding will Weile haben" - a German saying that means great things take their time. Today we release our Magnetic Fields simulation 🎉, which took a while to make. But it's great, we think. Update your app now to give it a whirl! pic.twitter.com/zAoCwWQVHu— Kosmos School 👾 (@kosmosschool) November 13, 2019
In principle, you have two conductors that you can connect to a power source and either run DC or AC current through them. Once you hit the power button, you see the magnetic fields forming. You can also move the conductors closer or farther away from each other to see how the magnetic field adapts. Of course, you can use the slider to change the magnitude of the current, too.
We’re calculating the magnetic field lines dynamically and in real-time, which allows us maximum flexibility and extensibility in this simulation. I want to briefly share how we do that.
First, we calculate the magnetic field at a fixed distance from the center of the conductor (let’s say 0.1m). We use the Biot-Savart Law. We are working in three dimensions, so we get a vector with three components. If there are two conductors, we calculate both magnetic fields and add them.
Second, we now need to find the next point in the magnetic field line. After that the next and so on until we’re back at the starting point. Doing this analytically would light our headsets on fire and probably take decades, so we’re approximating and doing it numerically. We use the Runge-Kutta method. If you’re not familar with the Runge-Kutta methods, they’re a family of iterative methods and we use the classical one, called RK4. For every point on the magnetic field we need, we approximate it with RK4. Once we’ve got a point, we take that as a starting point and find the next point.
However, RK4 just gives a point close to the actual point on the magnetic field line (that’s the trade-off). In pricinple, the smaller you make the step-size, the more accurate your calculation is, but the more calculations you need to get back to your initial starting point. So, it took some experimenting to find a good value.
In theory, mathematically, this approach worked and we were able to generate dynamic magnetic field lines based on the magnitude of current and different distances of two conductors, it felt great! Here comes the but.
From a software engineering point, this implementation wasn’t ideal.
We picked a small step-size, so, to create the magnetic field lines, it has to do a large number of matrix calculations. To understand why this is a problem, you need to know that in game development, we tell the computer what to calculate each frame. Per second, there are usually around 60-72 frames (on the Oculus Quest). If the computer needs to calculate something that takes longer than 1⁄60 of a second, it can display less frames and you see frames stuttering or lagging. Not ideal.
The way we initially implemented it was that we ran all calculations in the first frame. While this worked well on our computers, it took several seconds to complete on our Oculus Quest headsets.
To solve this problem, we decided to evenly distribute the calculations and drawing of the magnetic fields among frames. Not only did this work nicely, it also had a cool side effect: The user can watch the magnetic field building up!
And that’s how we make magnetic fields :-)
If you’re interested in learning more, contact us at email@example.com or on Twitter.
You can download the Kosmos School app for your Oculus Quest on SideQuest.