Sign in to follow this  
Xeon06

2D Camera?

Recommended Posts

Xeon06    122
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 this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by Xeon06
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.
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 this post


Link to post
Share on other sites
kiwibonga    183
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 this post


Link to post
Share on other sites
Xeon06    122
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this