#1: Game programming with a camera object

by Peter Stuifzand

When creating a game there's always the problem of putting the object in the right place on the screen. A camera object will help you with this.

Introduction

A camera is an object that can calculate the position of objects on the screen. It simplifies all the code written for this. The easiest place to use a camera is in a 2d game. It doesn't if it's top down or side scrolling because you always have to calculate the screen position of objects in the gameworld.

The basic part of the camera object's code is the calculation of the screen coordinates of a object in gameworld. In a 2d game an object in gameworld has two dimensions. Let's call these x and y.

struct game_object {
    int x, y;
};

If this object is placed on the map it could be on e.g. { 40, 20 }. If the camera is on {10, 6}, then the object should be drawn on { 30, 14 }. If the camera moves closer to the object, this object should be drawn closer to the corner of the screen.

The camera object

The camera object is an object that 'knows' about the screen and the worldmap. It can translate gameworld coordinates to screen coordinates. Gameword coordinates can be all kinds of coordinates; these are different for different games.

struct camera {
    int x, y;
    int width, height;
};

A camera object is composed of an x and y coordinate and has a width and a height. The x and y coordinatie of camera is the starting point of the camera. The end point of the camera is x + width and y + height.

If an object's x and y are between those the camera, the object will be on visible on the screen. It can be used to know which objects should be shown on screen.

A simple function that is used together with the camera is translate_to_screen. This function can translate the coordinates of an object in the gameworld to an coordinate on the screen.

void translate_to_screen(camera* cam, game_object* obj, point* pt)
{
    pt->x = obj->x - cam->x;
    pt->y = obj->y - cam->y;
}

This code shows how to translate from a gameworld object to a screen point with a camera.

But where's the camera?

You could say that this is all very nice, and it is, but how does the camera get its coordinate.

  • Static, the camera stays at the same place, but moves based on mouse movement.
  • Object based, the camera moved stays at the same place in relation to the an object e.g. the player.
  • Function based, the camera is moved according to a mathematical formula.
  • A combination of the above, the camera is moved based on player movement, but shocks when the player is hit.

Not all camera types are usefull for every game, or in every part of a game, but these all have a place.

Static

This camera type is used by strategy games, where you can move the camera by moving the mouse to a side of the screen or by pushing the arrow keys.

The best way to move this camera is to use similar code to what is used to move te game object. If the mouse at the top of the screen, set a variable in the camera to up. Now the engine can move it up, by calculating the time since the last frame. But this is complex enough for another article.

Object based

An object based camera is also a simple camera. There are at least two ways to implement to implement this object. If you use c++, the simplest way is to use a class like the one below.

class ObjectBasedCamera {
    // Construction
public:
    ObjectBasedCamera(int sw, int sh, game_object const* obj)
        : width(sw), height(sh), object(obj)
    {
    }

    // Coordinates
private:
    int get_x() const {
        return object->x - screen_w / 2;
    }

    int get_y() const {
        return object->y - screen_h / 2;
    }

    // Translation
public:
    void translate_to_screen(game_object const* obj, point* scr_pt) const {
        scr_pt->x = obj->x - get_X();
        scr_pt->y = obj->y - get_Y();
    }

   // Private Data
private:
   int screen_w;
   int screen_h;

   game_object const* object;
};

As you can see a class like this can let the camera be centered on any object. The camera will follow the selected object nicely.

The center of the camera is calculated by subtracting the mid point of the screen from the followed object. If your screen is { 640, 480 }. The middle will be on { 320, 240 } and by substracting this, the object will be in the middle of the screen.

If the object has a width itself, you can subtract one-half of the objects width and height.

This camera can be used by calculating the screen coordinates of every object with the translate_to_screen method. If the object is on screen, then it should be drawn.

If you use the object based camera on another object than the player, you have a chasecam.

Function based

An example of the function based camera is visible in a level of Mario Bros. where the camera moves to the right and the where the player (Mario) should stay ahead of the left side of the screen.

The x coordinate of the screen can then be camera will be a function of the time spend in the level.

f(time_spend) = speed * time_spend

Of course function based cameras can be based on all kinds of variables this is just one example.

A combination of camera types

By using a combination of camera types all kinds of effect can be created. For example you can have a object based camera that follows the player and a function based camera that shakes whenever a player is hit. The function of the shaking camera can be based on the time since the last frame and a sin function that uses the direction of the hit.

Conclusion

Games can be a lot simpler by using a camera object instead of some code in some function. You can also create lots of different effects without changing any code in the rest of the game.

Welcome

My name is Peter Stuifzand. You're reading my personal website.

Profiles

Peter Stuifzand
peter@peterstuifzand.nl
Zwolle, The Netherlands