Tales of the Rampant Coyote

Adventures in Indie Gaming!

2D Game Programming: Why, Y?

Posted by Rampant Coyote on June 13, 2016

UnityCoordinateSystemI have a problem programming in modern game engines in 2D. It’s weird, it’s embarrassing, and it trips me up constantly even when I think I’m being careful. And I finally figured out why.

My problem is Y.

Now, let me preface this by saying I don’t have this problem when doing 3D coding. It’s because 3D is something else entirely. Depending on what coordinate system you use, X and Y might be on a horizontal plane, and Z is up. Or X and Z are horizontal, and Y is up. Something like that. Although this can trip me up from time to time as I switch between different engines and tools (left-hand versus right-hand coordinate systems). But my brain shifts when I’m doing 3D.

xy1When I’m doing 2D, I always think of positive Y as going down. Which doesn’t make sense, if you have ever graphed on an X, Y plane in school. Unless told otherwise, you always graph in that first quadrant, with X going to the right and Y going up on the paper. Right?

You could include all four quadrants and going into negative numbers. But hey, for our purposes, we’re just confining ourselves to positive values. So, naturally, when you are talking about 2D graphics on a screen, if you want to move an object to the right, you increase its X value. If you want to move it up, you increase its Y value. Easy, right?

Yeah, it is. However, I have a weird dyslexia of the vertical axis. When I’m not paying enough attention (and sometimes even if I am), I’ll make assumptions that positive Y is DOWN. I’ll subtract from Y to try to make an object move up (or check what’s above it on a gridded map). This causes some really weird bugs in my code, sometimes, because I’ll do it right in most of the places and then accidentally invert the sign in some obscure corner of the code.

The reason, once I thought about it, was pretty obvious.

This was how the screenspace memory worked on the Commodore 64:

C64ScreenMap

See how that worked? Every row was an increase in Y value. Y got bigger as you went down. It wasn’t just screenspace. This was how you designed character-mapped graphics (and, for that matter, sprites) on the ol’ C-64:

C64CharacterModeProgramming

This wasn’t limited to the old Commodore 64. This was true in the DOS days and on many computers. In fact, you could look back on the trusty ol’ Atary VCS / 2600 from the 1970s to catch a glimpse of the rationale here. From the book Racing the Beam:

televisionscreen

The Atari 2600 shared a problem with my very first computer – a Sinclair ZX80 – in that there was no “memory mapped video” (AKA frame buffer). Instead, for every scanline of the machine – a horizontal line drawn by the electron gun of the television – the game system had to tell it what to draw with every line. The gun’s beam moved left-to-right, top-to-bottom. So naturally, you drew the stuff at the top of the screen first, and at the bottom last.

I suspect that had televisions been design to do that raster scan bottom-to-top, computer graphics hardware would have evolved to match, and we’d never have had this problem. As I recall, this held true even up to the time when I was directly pushing stuff onto the VGA card.

From a mathematical perspective, this is a weird aberration. Hardware does what hardware has to do. I don’t know how modern hardware works. Maybe it’s the same. But even if it does work this way, unless you are writing drivers (and probably not even then), this is all abstracted out. Now we programmers can work in the relative comfort of mathematical purity. Our X,Y axis is neat and orderly, and Y is up.

Alas, old habits die hard. I learned to program on those old computers, and I made games, dangit. Lots and lots of games. The Commodore 64 was my favorite playground growing up. And in the bad ol’ DOS days of the PC, I was still doing this. It’s pretty ingrained and, these days, pretty useless.

The solution, of course, is to spend a lot more time writing 2D games for modern systems. I may be the only game programmer still afflicted with this ancient habit, because working in 2D isn’t my usual thing.


Filed Under: Programming - Comments: 12 Comments to Read



  • Gregory Avery-Weir said,

    I have this problem too! A lot of screen-space coordinate systems use +Y == Down, including Flash/AS3’s and HTML5/Canvas’s. Any time I need to code in a world-space system like Unity, I get confused.

  • Rampant Coyote said,

    I’m so glad to hear it’s not just me!

    I forgot about Flash (but then, I hardly touched it, and that was years ago).

  • Brent Bowman said,

    When I have an issue like this, I will generally make a function or prototype that is named by how I think. Like a function called moveUp(amount) or a function moveDown(amount) that takes care of the signs without me ever having to count or worry about the signs.

  • Darius said,

    I had a lot of trouble with this when I first got started with Unity. But I’ve worked almost exclusively on 2D projects, so I’ve largely gotten over it. Still, it was pretty annoying while it lasted.

  • CdrJameson said,

    Hang on, are there platforms with Y going up?

    I think I vaguely had to work with some MS DIBs years ago that did things in this freaky way, but all my 2D programming has Y going down. I’m sure GameMaker does it this way, along with various mobile things I’ve used.

  • Rampant Coyote said,

    Probably any game engine that has a serious 3D component will default to +Y or +Z going up.

  • Scutterman said,

    As a web programmer, I’m used to CSS having the top-left being 0,0. It makes sense in that case, though, because the bottom of the page often stretches below the bottom of the screen, so starting 0,0 at the bottom-left would just make things confusing with unequal amounts above and below.

  • Rampant Coyote said,

    Dang good point.

    I guess -Y = up makes sense in a lot more contexts than I thought.

    I feel less bad about my old habits. I thought I was just weird.

  • Cuthalion said,

    I definitely think in Y increasing downward, and I’ve never done coding on older systems. (I do 2d for games.) It throws me off occasionally when I have to talk to OpenGL, and it expects Y increasing upward. I try to handle that conversion in the functions I use to send things to OpenGL, so that everywhere else I can think in screen space.

    I think I’ve seen Y increasing downward on graphics programs as well, and it definitely makes sense for documents (like webpages), where things begin at the top.

  • CdrJameson said,

    Just checked with Gimp and Paint.net, and they’re steadfastly +y downwards.

    I guess this kind of weird anomaly is just what you get when you try to force a 3D package into a 2D hole. At least the screen isn’t in X,Z coordinates!

  • Adamantyr said,

    I’m writing vintage games for my old computer and positive Y is down, negative Y is up there as well!

    I have more of an issue that early on I thought X was vertical and Y was horizontal and I occasionally have to remind myself to flip them…

  • Troy Leavitt said,

    I think that the Apple II did this as well with 0,0 being the upper left corner. At least I’m pretty sure that was the case. It’s been a long, long time…

    http://www.cs.columbia.edu/~sedwards/apple2fpga/FPGA-Apple-Lores.jpg

top