Under the Hood
In the in-development game (ahem, ~~The Electric Shocktopus~~), the title character can charge himself and fly across electric fields. But how do those electric fields get there in the first place? Well, since it is a true simulation, we (I? The computer?) have to compute them.
The Simulation: Part 1
Electric Fields are an easy matter, right? Say we’ve got a positive charge in the middle of a level. To find out the electric field around it, we turn to Coulomb’s Law, something we all learned in high school.
The field points away from the electric charge, and the strength drops off as the distance squared. Simple.
If we have multiple charges, thanks to the superposition principle, we just do this for each charge — then add it all up. Straightforward, simple, perfect.
But that’s not the end of the story.
The Simulation: Part 2
Because in the game, I want there to be conductors. And these blocks of metal affect the electric field. The conductor, you see, is filled with positive and negative charges. Put another positive charge next to it, and the negatives in the conductor crowd toward it. Unfortunately, calculating these effects is no easy matter. Only in very symmetric and special situations can you even write an equation for this.
Enter the poorly named ‘Relaxation Methods.’ Since we’re on a computer, this is a way to numerically figuring out what the fields should be. And we’ve gotta crunch a lot of numbers to do it. Basically, you turn the screen into a grid (which is easy, since the game is already built on a grid). Then you take a guess at some initial values of the field, the ‘rough draft’ so-to-speak, which can be completely wrong for all we care. The strength in this method comes from revisions. You take the grid of bad values, and go through, do the number crunching, and make them slightly better. And then you repeat – a lot. Fifty times, a few hundred times, something in that ballpark. And eventually you get ‘close enough’ and stop.
Turns out, this isn’t too bad. On the 2D grid of the screen, the game can generally figure out the electric fields in about a fifth of a second. And since this is a 2D game, that’s all we need. The third dimension is irrelevant, right?
The Simulation: Part 3
Wrong. It turns out, that third dimension affects us whether we like it or not. Even if we think we’re dealing with a 2D grid, the third dimension is assumed. And that third dimension is very boring — nothing changes as you move along it. It’s just a whole line of carbon-copies of the screen. It’s as if every point charge we place extended in that direction and made a line of charges. And a line of charges gives a very different field from a point charge:
The force drops off more slowly, so stays stronger even further away (it falls off as the distance, not the distance squared). As you can tell by the face that the Shocktopus is being repelled back into a wall. So what we think of as ‘point’ charges in this method are not point charges at all.
The solution? Do the Relaxation Method on a 3D grid. It’s a whole lot more calculations (instead of 50×50 points, we have 50x50x50 points), and takes a few seconds to compute the fields. But, for all that, we can finally have point charges and conductors coexisting in the game.
Moral of the story? Nothing is as easy as you’d expect. The 2D game is actually a 3D simulation. Shocktopus *could* move in the third dimension to skip past barriers, he just chooses not to. He likes the challenge.