Monday 15 May 2017

Teach Yourself Game Programming in 21 Days - Day 5

Polygon Engines, ahoy!

Reading this chapter gave me huge nostalgia vibes, as one of my favourite games growing up was Asteroids. I used to play it for hours a day on MAME, trying to beat my high score. I'd like to think I got reasonably competent at it, and it's a major reason why I played Subspace for so long. There's just something I really enjoy about its inertia-based controls.


Anyway. The author notes that polygon-based graphics are very old; they were originally used in specialised hardware (so-called vector monitors) which functioned similarly to an oscilloscope. They could draw polygons reasonably quickly using a single electron beam, rather than the huge array of pixels we associate today with computer displays. This isn't the only way that polygonal graphics are used; of course 3D games throw billions of triangles at us, but there are also plenty of 2D games using them for stylistic reasons. André mentions Out of This World as "one of the most impressive exhibits" of their use.

There's a lot of algebra in this chapter. It's nothing particularly advanced (if you remember your trigonometry) but I remember finding it quite daunting when I was younger. I've always been mathematically inclined, but the notion that I could use maths to make games was oddly novel.


To a modern game developer, used to relying on an existing game engine to draw all their graphics for them, there's an oddly long amount of time devoted to drawing lines. Dredging up your early geometry, you might remember something like $$y = mx + c$$ to calculate where your lines go. While still a useful formula in a game engine setting, we need something that cares about discrete values (i.e. pixels). The code readout to do this is about three pages of the book. That was fun to type! There are much longer ones than that, though...


The first actual program (linedemo) of the chapter duplicates that old bouncing-line-with-trails screensaver that all the computers at my school had set by default. Being the weirdo that I was, I wrote a snow falling thing in QuickBasic and replaced as many of them as I could.

After this comes a whole bunch of functions for creating, translating, rotating and scaling polygons. Dealing with such a simple mathematical object is easy; the least accessible part is the rotation, which uses cos and sin to move each vertex around a hypothetical 'origin' of the polygon. The real wrist-breaker was clipping a polygon to fit inside a display rectangle. Here's a sample:


Thanks, whoever lost the companion CD. I really wanted to type that out again.

Still, my efforts were rewarded with a passable copy of Asteroids's graphics in the boringly-titled rockdemo. If anybody is following along, I'd encourage you to grab the code from this chapter and run it, because it made me smile to see.

There's not much more to this chapter, dominated as it is with giant code listings. There are a couple of exercises that centre around making rockdemo better; making the ship fire and adding a Gravitar-esque black hole. If nothing else, I might do these ones.

If people want it, I could easily provide all the code compiled to run inside DOSBox (including any exercises I can be bothered to do).

No comments:

Post a Comment