## Under the Hood

Posted in: Electric Shocktopus, Wednesday Update | August 15, 2013 | 16 Comments

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.

-Andy

1. #### By A Random Player

Posted August 15, 2013 at 1:35 pm | Permalink

Wait, so does precomputation mean that you can’t have time-varying charge (like a charge slowly charging a conductor by induction, which wasn’t charged at the beginning)? And Shocktopus can’t charge a conductor? I can see how Shockopus’s effect in the field could be found (superposition principle, as mentioned), but that would certainly affect the conductors.

• #### By TestTubeGames

Posted August 15, 2013 at 1:54 pm | Permalink

That’s true for now, yes. The levels are fixed ahead of time — and Shocktopus is treated as a test charge. His is now treated as ‘too small’ to affect the world.

I might be able to get some induction to work out on the fly — though I’m still not sure. If I just look at an area nearby him (6x6x6 tiles, say) and lower the needed precision, I might be able to get conductors to react to him in real time. Gotta try it out and see how it works.

2. #### By exfret

Posted August 19, 2013 at 11:50 am | Permalink

Hm… I think that gravity is a r^-2 law in a 3 (spatial) dimensional universe, because as the sphere of influence increases outwards, the surface of the sphere, or the ‘spread’ of the gravitons (if gravitons exist, that is) increases by r^2. So, in a one-dimensional universe, you would expect it to increase by r^1, since the circumference of the circle, and thus the ‘spread’ of the gravitons, would increase by r^1. It’s strange that it actually does work out that way with magnetism… Maybe, our Universe is actually a four (spatial) dimensional universe with a r^-3 law, but every point is actually a line that extends into the fourth dimension, so it turns out to be r-2 when you look at only 3 dimensions, and then maybe it goes on: Maybe there’s infinite dimensions, but since every mass extends infinitely outward in every dimension except the three we live in, it feels like a r^-2 law, but in reality, it r^-infinite! Think about that, an r^-infinite world!

• #### By TestTubeGames

Posted August 21, 2013 at 4:37 pm | Permalink

That’s precisely the right reasoning for why the force law works that way in 3D, 2D, etc. That was a very compact, good description of it.

Also your other comment about traveling in 3D to skip past barriers reminded me of a game I’d love to make at some point — Flatland. In that one, this would be an important mechanic.

• #### By A Random Player

Posted August 21, 2013 at 4:59 pm | Permalink

Flatland, as in the novella by Abbott ( http://en.wikipedia.org/wiki/Flatland ) ? That would be cool. (And possibly annoying if in first person.)

• #### By TestTubeGames

Posted August 21, 2013 at 5:04 pm | Permalink

Haha, first person would be great! Just a line of varying color on an otherwise blank screen. I was thinking birds-eye view, of course, but that’d be a funny (very brief) addition to the game.

It’s a great book to read if you’ve never done it before — it’s so old there are free copies online.

• #### By exfret

Posted August 21, 2013 at 8:38 pm | Permalink

I would LOVE a flatland game. Could you have a hyperbolic universe, a spherical universe, a torus universe, a double torus universe, what about a triple torus universe? OH, AND, what about a universe with Klein Bottle geometry? What about having the player have the ability to customize their land’s folds into the third dimension. That would be SO cool. PWEEEEAAASEEEEE!!!!! (Insert Robly’s Puppy Face Here).

• #### By TestTubeGames

Posted August 22, 2013 at 1:55 pm | Permalink

My goodness! Breaking out the puppy face already? At the moment the game is still a figment (with a very old, yet semi-playable prototype), so I wouldn’t start expecting it any time soon. It would be a fun test of some new software I’m working with though. I’m trying to make the new gravity simulator in Unity — which at its core works in 3D. (The sim will still be 2D, mind you, to keep life simple) In Flatland, I would visually have to hide the 3D of course, but the 2D game would match seamlessly with its 3D elements.

• #### By A Random Player

Posted August 22, 2013 at 2:41 pm | Permalink

Wait, Unity3D? Doesn’t that require downloading? I don’t think requiring a download would be a good idea for many people who don’t have Unity already installed (like me). Would there perhaps be a unity-free version?

• #### By TestTubeGames

Posted August 22, 2013 at 4:11 pm | Permalink

Yeah, playing a Unity game requires downloading a free/easy plug-in. In the same way that playing a flash game requires a plug-in. The big difference being more people *already* have flash. Other than that, it shouldn’t be any harder.

But I do share your concerns — I want to set up as few barriers as possible to my games, in general. I figured I’d bring this up in the forums — so we could have an easier format for this discussion… http://testtubegames.com/forums/viewtopic.php?f=1&t=105

• #### By exfret

Posted August 25, 2013 at 6:14 pm | Permalink

I really want to be able to mess around with curved flat spaces. Recently, I have gotten interested in non-Euclidean geometries. I really want to create my own spaces and see how they work.

• #### By exfret

Posted August 25, 2013 at 6:22 pm | Permalink

So let’s see, you have ideas for shocktopus, evolution simulation, circuit game, centrifugal game, quantum game, gravity simulator fixing up, and now flatland. Wow, that’s a lot of options. Could I pwease ask you to do flatland after you finish shocktopus?

• #### By TestTubeGames

Posted August 27, 2013 at 12:41 am | Permalink

I agree, curved flat spaces could make for a neat game. I doubt I’ll be working on it *right* away, but I’ll keep your vote in mind as I figure out what I’ll work on next.

3. #### By exfret

Posted August 19, 2013 at 2:53 pm | Permalink

Oh, and another thing, Shocktopus *can’t* skip past barriers, even if he/she/it did travel in the third dimension. In fact, he/she/it might be traveling through the third dimension right as you play the game. The reason for this is because, as you said, the third dimension is just a whole bunch copies of screen, so if Shocktopus were to travel through the third dimension, then nothing (except his/her/its absolute position, which is both not useful and not displayed) would change. So no matter how much you move through the third dimension, you’ll just come across the same, old, fun level. No skipping across barriers included.

4. #### By exfret

Posted August 25, 2013 at 6:14 pm | Permalink

Edit: Curved 2D spaces

• #### By exfret

Posted August 25, 2013 at 6:16 pm | Permalink

Sorry, meant to place this below my August 25 reply.

### Post a Reply to exfret

You may use these HTML tags and attributes: `<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> `