Side scroller theory

Started by
5 comments, last by XXChester 12 years, 1 month ago
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.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

Advertisement
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.

Yo dawg, don't even trip.

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.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

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.

Yo dawg, don't even trip.

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.
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.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

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.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

This topic is closed to new replies.

Advertisement