1
\$\begingroup\$

I am going to work on my next game project, however there are some issues I want to correct since my last one. Here is one of them:

This is a problem I think I can already solve, but I'd like to hear any criticisms on said solution, and perhaps hear other solutions that may exist.

The problem:

I have a window 800W by 600H, and my player's position is stored as pixels down, and pixels right. Lets say this player can run to the edge of the screen. This works fine until someone say, changes the size of the window, thus the player can run further out, where he should not be. In one of my first game attempts (a TD), enemies were dependent on the pixel position of game objects (Tiles), and thus by resizing the window, they ended up wandering off path.

A solution:

Represent the world with boundaries placed at 0 and 1 (or any other numerical pairing), and place entities as a percentage across. e.g. Player.positionX = 0.5, would mean that the player is half way across the map. Perhaps instead of 0, 1, it was 0, 1000 instead? With floating/double error in mind, what choice might I make?

So I think that would work fine in most cases, do you think so? What is a typical approach? What other approaches exist? How might platform/game type affect this choice? What if for some reason, the world size were to change dynamically? (Odd, but I'll throw it in there)

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Letterbox and scale.

Percentages will still break your game. Think about different aspect ratios. The window might be 3:4, 16:10, etc. If you depend on pixel locations, these ratios between player and enemy locations will end up differing.

Instead, just pick a size of your world completely independent of pixels. Scale your graphics up or down if the resolution is different than your target. Strongly recommend to players to use even multiples of your graphics' intended resolution so that the game still looks smooth. Do not use on-screen pixels as any kind of meaningful unit of measurement!

For different aspect ratios, use letter boxing (black bars, either vertically or horizontally, to fill up the "excess space" from the resolution). That is, if the resolution's ratio is wider than your target, add vertical black bars on the side of the screen.

You can also just stretch and squash the pixels. If the player makes the window twice as wide, the game will look wide, but all your in-game units will be unchanged. The player might look like it's moving twice as fast horizontally but all the mechanics will be unchanged; the only difference is visual. This is not an ideal situation, but it works.

Ideally your game design just shouldn't care about these questions of resolution or aspect ratios. All of the interesting action should take place in a narrow aspect ratio, players with wider displays get no unfair advantages, etc. This is similar to what movies do. They are made in very wide formats like 2.39:1 but the cinematographer and director work to make sure that all the interesting things in the scene can be kept in after cropping down to the 4:3 ratio still common on older TVs (and eventually will target at minimum the 16:9 common to HDT/FHDT sets).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ah, I see what you mean. This solves a few other problems/questions that I had concerned my self with in previous games. Now by separating the screen and world, it doesn't matter whether I choose between percent across, or units across, I choose whats best for the situation, correct? \$\endgroup\$
    – Henry Tupo
    Commented Sep 14, 2013 at 18:05
  • \$\begingroup\$ Yup. The simulation of your world is independent of the visualization of it, so don't tie them together at a game mechanics level. \$\endgroup\$ Commented Sep 14, 2013 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .