Camera/screen moving

Started by
8 comments, last by Digivance 11 years, 1 month ago

I am having some issues with camera or screen and background animations...

All of the examples i can find are kinda focused that your screen and character is still while the background is actually moving and that gives you impression that you are the one moving.

I would like to script that your background is still and that your screen would be the one moving, like lets say you have dimension of the ''map''

1920 × 1080 and your screen would be 800 x 640.

If you would move the character, camera would be centered on it and he would be constantly changing coordinates while the background coordinates would be always the same...

Is that even possible to do something like that or is it always that your background is moving instead of you?

Tnx for any info if you can help me :P

Advertisement

Your camera is always still: it is your screen. However, nothing prevents you to store camera position however you like.

For example, if your camera's X is 18, just move world into opposite direction: -18.

A screen( menu) doesn't need to be attached to a camera and only be rendered in camera space, you can just as well use a 3D screen asset that is positioned in your game world and then use a freefly camera to focus on the object. See the DiRT and GRID series as examples of this type of UI.

If you wobble this camera you get the impression the world is moving while in actuality it is the camera that is moving and not the world.

If you aren't tied to any particular tech framework for UI you can write your own one that allows you to do this. There is nothing stopping you from realising this except your time and imagination.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

If this is for a 2d game, what you can do is attached the camera to the player. As long the player is not in the center of the screen and the level can scrool you move the player. Once you hit the center of the screen you still move the player but also move the camera, but subtracting the camera from the player position so in reality your player is not moving and will remain in the center of the screen while the camera increments. Then what you need to do is subtract from the position of every object by the camera that will simulate the level scrolling while keeping the player in the center. Once you get to the end of level where it can no longer scrool, then you stop incrementing the camera and keep moving the player which will simulate the player moving to the far edge of the screen which means you reach the end of the level.

But why does images that I blit on the surface lets say an image of another object/player etc... they move with my camera/screen the same way as my player,

like they are locked on the surface on those coordinates that i blit them? It would be fun if I would be making the HUD, but I dont want that for my objects should I decrement

them in the opposite direction when my player moves?

And yes it is for 2d game,

thanks for all the help

For most 2D games you position all your sprites in screen space. If you want to translate the camera's position to different coordinates, that's fine, but in the end you will have to apply your camera's new position in calculating the final sprite coordinates anyways, because that is how SDL determines where to blit objects onto the screen.

Your map may load sprites with storing positions in negative coordinates (if you choose to), but they will be off the screen and not be drawn unless you make the coordinates positive during the rendering step.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

Shouldn't you just displace everything by where you want to look at.

So if you want to look at the player just displace everything by the players position.

If you are at 200,300

then move the everything by -200,-300

So you draw yourself at 0,0

If you want 0,0 to be the center then add screenWidth/2 and screenHeight/2

If I understand you then if I would want to move by one pix like lets say 201,301 then i would need to move all the others objects by one pix and leave my character on 200, 300?

If so then isnt that really inefficient to move the world and all its objects instead of the character?

What I meant by move was that when drawing you displace the objects when drawing.

for eg


displace. = playerpos

for all obs

draw obj at objpos - displace

I believe what everyone is getting at here is that your screen is a little more than a canvas. It is 0,0 - width,height period. That never changes, the idea of a camera is completely conceptual and offers a means to calculate the offset of sprites, backgrounds and any other images that you are rendering. To "move" the camera you have to offset graphics based on the dimensions of the screen. Eg the screen ALWAYS shows 0,0 - width,height. Your camera's position can be used to calculate where to render the graphics to make it appear that you are looking at a different part of the scene.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

This topic is closed to new replies.

Advertisement