# 2D Camera?

This topic is 3744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, I am developing a small 2D game, an asteroids clone. I want the space where the ship can be flown bigger than the screen, and so I need to implement some kind of top-down 2D camera. However I can't wrap my head around how I would do that. I can do most graphical and mathematical operations. Does anyone have some pseudo-code or even a theoretical algorithm? Thanks.

##### Share on other sites
Quote:
 Original post by Xeon06Hey all,I am developing a small 2D game, an asteroids clone. I want the space where the ship can be flown bigger than the screen, and so I need to implement some kind of top-down 2D camera. However I can't wrap my head around how I would do that. I can do most graphical and mathematical operations. Does anyone have some pseudo-code or even a theoretical algorithm? Thanks.
To keep the ship at the center of the screen, just translate by the negative of the ship's position. For example, in OpenGL:
glMatriMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(-ship.pos.x, -ship.pos,y, 0.0f);
Or are you looking for something more sophisticated than that?

##### Share on other sites
You'll want to keep two sets of coordinates: world coordinates for the objects' current locations, and "scroll" coordinates for the camera. This way, object_world_coordinates - camera_scroll_coordinates = object's screen coordinates.

Then to follow the ship, you just have to test if it's close to the screen edges, and scroll accordingly.

To sugarcoat it, you can make the camera's speed proportional to the object's distance from the edge of the screen, that way the scrolling will be smooth.

##### Share on other sites
Jyk, thanks but I don't have any direct access to openGl like that. (I'm coding a game in D using the Arc game library... :P).

Kiwibonga, thanks thats a start. However will the game crash when I draw something outside the boundaries? I could do a simple check, but what about sprites or primitives that will be half outside half inside the camera's view?

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002314
×