Advertisement Jump to content

C++ Is it standard for implementing a 2D Camera by moving everything but the player (or focus point)?

Recommended Posts

So in the few different tutorials that I have seen for using C++ / SDL, the implementation of the camera does not effect how the player is rendered but how the the rest of the world is rendered. Instead of changing the position / offset of where the player is rendered, you change the position / offset of where the map and other entities are renderer.

Out of curiosity, is this the standard (or maybe only) way of doing things when working with lower level code like C++ / SDL?

While it makes logical sense to me, my experience in game dev has always been high level abstractions (game engines like Unity or even libraries like Love) so it just feels wrong but maybe all of those engines / tools do it the same way and the abstraction they provide just hides that fact.

Share this post

Link to post
Share on other sites

Mathematically, this is how it works either in 2d or 3d. But implementation wise, you don't explicitly move everything else when you want to move the player perspective. You encapsulate this player perspective in some kind of camera class, and when rendering calls are called on objects, the renderer offsets the object position by the camera position hence creating the effect that the player is moving. In 2D, this can be as simple as substracting the camera position from object position. In 3d, you multiply the projection-view matrix by the object model matrix.

Share this post

Link to post
Share on other sites

 It really depends on the type of game you want to make.

Let's say, you have an endless runner or a shoot'em up. If you have a game that uses random map segments to be spawned an despawned as the player passes distance, moving the map instead of the player makes total sense.

On the other hand, if you have a detailed, handfully crafted map where the player can move in any direction, moving the player would be the obvious solution while moving the map would be a weird way to go.

In my game, I use a combination of both. It's really the player that is moving but I do shift the the map tiles' position (especially backgrounds) to create a parallax effect, thus the illusion of three dimensional depth.

Hope, that helps :)

Share this post

Link to post
Share on other sites

You should render everything at its real position, just during the rendering loop you get the camera coordinates to know what to draw, and draw everything at their real position minus the camera position.

Some libs already have a class for this, SFML has Views that are easy to use for doing camera things and split screens.

Edited by Phanoo

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!