November 7th, 2007

Anatomy of a Game – Part 2

Beginner Game Tutorials, by nazeeh.

In this post, let’s talk about the difference between 2D and 3D games!

Games can be either two dimensional (2D) or three dimensional (3D). Before we continue talking about the two types and how they differ, let’s talk about how they are similar.

Remember, games are nothing but elaborate illusions. Your monitor is capable of displaying only 2D images. That’s why your resolution is something in the form of width x height (1600×1200, for example). You don’t see a monitor that is advertised as 1600×1200×800! So how does it make sense that we can have 3D games on a 2D display surface? It’s really nothing but an optical trick that artists have been using for ages. You can draw a 2D image and put depth in it by how you size things relative to each other as well as how you draw the lighting and shadows in it. It’s just an illusion. Here’s a perfect example of such an illusion:

The image looks like it’s 3D when it’s 2D but with amazing usage of lighting, perspective, shadows, etc. This is really what 3D gaming is about, how to draw such 2D images in realtime in an easy way. Lots of math!

We’ll discuss 2D games first since they are easier to understand than 3D ones. In a 2D game, you have a canvas (your screen or game window) that has a height and a width. We are going to assume that your game is running full screen at a resolution of 1600×1200. That would mean that the coordinates of the upper left corner of the screen are 0,0, and those of the lower right corner are 1600,1200. You can express any point on the screen as an x,y pair of positive values.

Programming for 2D is really pretty easy. You start by positioning your objects on the X,Y grid, and then you move them around. So you can start by putting a paddle for the game Pong at the coordinates 20,20 and then moving it around the screen by increasing and decreasing the Y value to make it go up and down. So for the first frame, you put it at 20,20, and then in the next frame, you position it at 20,21, and so on. As the game draws itself frame by frame, your paddle will appear to move down when actually it’s you who are positioning it at a slightly lower point every frame. You can do the same thing with the X value to make it move sideways.

So what do you do for 3D then? Well, in principle, it’s kind of the same at the core, but needs different considerations than 2D. The first thing is that you specify a location on the screen by three values, the 2D X and Y values and a new value called Z that we’ll use for depth.

clip_image002

The Z axis goes through the screen. This means that you can specify points in this 3D space that are at different depths from each other. So in essence, an object can be placed way behind another one and appear to be smaller in size because it’s further away. What happens is that you specify objects in this 3D space with different depths. Then when the graphics card draws the final image that appears on your screen, it will apply all kinds of mathematical operations to produce a 3D looking image that is really 2D. But for all intents and purposes, you are working in a 3D space and have to account for that in everything you do. That means when you want to know whether one object hits another, you have to consider that they may be at different depths (that is, one is behind the other).

Whether your space is 2D or 3D, you’ll find that a lot of the principles remain the same. You still have to specify positions of objects on the screen by giving them coordinates. Moving objects around is an act in manipulating these coordinates to position them in another place. 2D is simpler because you don’t have to worry about a whole other axis, but moving to 3D doesn’t complicate life that much once you get it.

Move on to part 3

Thanks for reading! I'd love to hear your thoughts, feel free to leave a comment below. Don't forget to subscribe to my RSS Feed!
Share and Enjoy:
  • Print
  • Digg
  • Twitter
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Back Top

Anatomy of a Game – Part 1 Anatomy of a Game – Part 3

Responses to “Anatomy of a Game – Part 2”

  1. Hey,
    That was probably the simplest I’ve heard the whole 2d 3d thing explained also the flow diagram of the game process really helped me visualize how the different sections of the code work together.
    Thumbs up and on to chapter 3

  2. Hei you just removed all the problems before my eyes and now I have a clear idea how 2d and 3d works in a 2d monitor. thanks

    Mehbube Arman at July 9, 2010 4:20 am
  3. Great, simply explained and very clear. its brilliant, thanks!!!

Leave a Reply

Back Top