SFML - Screen Scrolling

Started by
4 comments, last by mypel16000 11 years, 2 months ago

Hi, I am implementing screen scrolling on my game (I imagine it will be easy, just using sf::Sprite::useSubRect()), but this isn't the main problem. My game has zombies that have a position x, y (relative to the window) and it has different sprites located in different locations. If I wanted my player to move around a big 2000x2000 map, how would I know what the position of my stuff is (relative to the top corner of the big 2000px map. ) And adjust my code which now uses co-ordinates in the screen?

Advertisement

make your objects (zombies, sprites, ..) relative to the upper-left corner of the current view of the map (not the origin).
so, if you start looking at the map with a 150px offset, the x-position of an object is (m_ObjectPosition.x - 150)

Two ways:

1) Calculate camera position yourself, and the entities on the camera.

PosOnScreen = (PosInWorld - CameraPos);

2) Draw everything using absolute positions, and use SFML's sf::View to move the camera.

Whichever method you use, if you are drawing alot of objects, make sure you filter out which objects are actually visible on-screen before drawing them.

How do I use absolute position and SFML::View? Got a tutorial or some instructions?

I would also like to add my a star algorithm ( if someone gives me some help with it ) and functions that take x and y values.... what do I do?

Whatever algorithms you use have no bearing on your camera. All that matters is: A) Where in the world your objects are, and B) What portion of the world is visible. From that you calculate where on the screen the objects need to show up in.

As for sf::View, here's a tutorial on it.

Thank you servant of the lord, very useful

This topic is closed to new replies.

Advertisement