Sign in to follow this  
XXChester

Side scroller theory

Recommended Posts

Hello all. I have been searching on this for quite some time now and trying things out but I think it is time to finally ask a question. I have a platformer that I built that I am trying to change to a side scroller now. I have the map already layed out with tiles via a program I wrote and the tiles are loading properly.

What I am having trouble understanding and getting a definitive answer is this; In your update loop, when the player moves you have to update all of the tiles positions, monsters, and every item's position by the amount the player moved correct? I know a lot of articles say to use a camera which I am using but it basically just points to the player at all times.

I believe I have the rendering algorithm working properly (rendering tiles around the player only). A lot of the things I have read talk about updating the rendering rectangle of your background texture, so I was going to try this next by rendering to texture my tilemap but before I did that I figured I would ask the above question because I can't see a reason why you would have to convert everything to a texture first.

It shouldn't matter because I am just looking for theory here but I am writing in XNA 4.0.

If someone could point me in the right direction that would be great. Thank you.

Share this post


Link to post
Share on other sites
Basically what I do is create a rectangle instance that represents the viewport of the screen. I set the viewport's coordinates in the coordinate space of the level, so that when I draw the entities that are in the level, I just have to subtract the coordinates of the viewport from the coordinates of the entity in order to shift the entity into it's proper place.

Share this post


Link to post
Share on other sites
Okay so you are saying that you DO update all of the entities in your world's positions as you move your player around.

Thank you very much, that is what I assumed but I couldn't find a definitive answer anywhere.

**EDIT** If anyone else has suggestions I am open to alternatives, so please do write. Edited by XXChester

Share this post


Link to post
Share on other sites
I do not actually update the coordinates of the entities, only the one rectangle. When I draw the entities, I do a few things. Firstly, I make sure that the entity I'm currently drawing intersects the viewport. If it does not, there is no point in drawing it because it'll be outside of the screen anyway, so nothing has been done to said entity. Secondly, if it does lie somewhere within the viewport, I pass the translated coordinates to the drawing functions but do not change the level coordinates of the entity itself. In Java, a function for drawing an image looks kind of like this:

drawImage( int x, int y, int width, int height, BufferedImage image);

Therefore, I might call a function like so:

if ( viewport.contains(entity.bounds) )
drawImage( entity.bounds.x - viewport.x, entity.bounds.y - viewport.y, entity.bounds.width, entity.bounds.height, entity.image );

This way, no coordinates have been changed. They still retain the same position.

Share this post


Link to post
Share on other sites
boogyman has the right idea. Picture a camera following your character around the level. When your character moves, the world doesn't move to accommodate his new position. The camera does.
Just draw everything offset by the camera's position.

Share this post


Link to post
Share on other sites
Thanks guys, I will give this a try today. I think the part that I am getting hung up on is the design of my engine. I haven't hit a road block like this in all of the games I have built but I think this is the first one. My engine is a fully OOP design where everything is self encapsulated and basically what I am hearing is that at render time I need to pass a View Matrix to the SpriteBatch.

I will let you guys know how it goes.
Thanks agian.

Share this post


Link to post
Share on other sites
Perfect! Thanks so much for your help guys, I changed my engine's objects rendering methods to optionally take a Vector2 for position offset and it worked great. The part I was caught up on was exactly that, I didn't understand how you render a different part of the world without moving the objects and updating their BBoxes etc. But not that I have the scrolling working I can see when I turn the CD debug data on that the objects are rendered in 1 place but the collision is still taking place where in the world my character actually is....which thinking about it makes perfect sense.

So to sum up, what I had was correct (A camera that follows the player), it was jsut a matter of applying this offset to the rendering.

Thanks again, I appreciate your help.

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