Page 1 of 1

How do I make maps scroll

Posted: Mon Apr 20, 2009 9:11 pm
by deathsangel
Hey guys I need to figure out how to make maps scroll,

When I tried to create a camera x and a camera y and just move the hole scene by those values my collisions got all screwed up. So what do you guys think would be the best approach to this problem?

Re: How do I make maps scroll

Posted: Mon Apr 20, 2009 9:21 pm
by andrew
You'll find a pretty good way of doing it here:
Lazy Foo' Productions - Tiling

Re: How do I make maps scroll

Posted: Tue Apr 21, 2009 12:56 am
by YenTex
from what I remember, It's a lot like moving a sprite, only when your sprite moves up the BG moves down etc.

Re: How do I make maps scroll

Posted: Tue Apr 21, 2009 5:25 pm
by RyanPridgeon
deathsangel wrote: When I tried to create a camera x and a camera y and just move the hole scene by those values my collisions got all screwed up.
This is because you need to also relate the x and y in your collisions code to the camera's x and y.

You had the right idea to begin with. Just manipulate all the drawing positions by a camera x,y

And the appropriate parts of the collisions too.

Re: How do I make maps scroll

Posted: Tue Apr 21, 2009 10:35 pm
by deathsangel
so your saying to substract the camera x/y from everything not just tiles/player draw?

Re: How do I make maps scroll

Posted: Wed Apr 22, 2009 6:09 pm
by MarauderIIC
Yeah, use the camera's x and y for the background but add it back to the sprites so that the sprite's x and y relative to the screen doesn't change, or something like that.

Re: How do I make maps scroll

Posted: Sat Apr 25, 2009 3:48 pm
by deathsangel
I tried that and the collisions are still messed up, can somebody point me to source code where this is used, so that I can see how this should be done. Ty your help will be very well appreciated!

Re: How do I make maps scroll

Posted: Sat Apr 25, 2009 3:57 pm
by fantastico
You should have "world" coordinates and "view" coordinates. All the game objects positions are in world coordinates and only those are used in the game logic including collision detection. Drawing is seperate and you just translate world coordinates to view coordinates (using the camera position) when drawing.

So for example if you have a player with player.x, player.y coordinates you draw the player at player.x - camera.x, player.y - camera.y. I hope that's what you are trying to do anyway.

Re: How do I make maps scroll

Posted: Sat Apr 25, 2009 4:17 pm
by deathsangel
That is what I was doing but for some reason when I shifted the world coordinates(myCam.x myCam.y) the collision would not update...

I shifted the player draw

Code: Select all

player.get_rect(Camera myCam)
{
    SDL_Rect rect;
    rect.x = pX - myCam.getX();
    rect.y = pY - myCam.getY();
    rect.w = pW;
    rect.h = pH;

    return rect;
}

Code: Select all

SDL_BlitSurface(player.get_image(), NULL, screen, &player.get_rect(myCam));
and the tile draw

Code: Select all

for (int r = 0; r < rows; r++)
{
    for (int c = 0; c < columns; c++)
         {
         SDL_Rect rect;
         rect.x = c - myCam.getX();
         rect.y = c - myCam.getY();
         rect.w = tW;
         rect.h = tH;

         SDL_BlitSurface(tileImage, NULL, screen, &rect);
         }
}
and finally in in the update call to bool touches_wall(Player myPlayer, Camera myCam)

Code: Select all

for (int r = 0; r < rows; r++)
{
    for (int c = 0; c < columns; c++)
         {
         SDL_Rect rect;
         rect.x = c - myCam.getX();
         rect.y = c - myCam.getY();
         rect.w = tW;
         rect.h = tH;

         if (check_collision(rect, myPlayer.get_rect(myCam)))
             return true;
         }
}

return false;

Re: How do I make maps scroll

Posted: Sun Apr 26, 2009 3:37 am
by fantastico
The player drawing is fine. The tile drawing is probably not doing what you want it to, I think. You are doing rect.x =c - myCam.getX() but c is just the number of the tile (0, 1, 2, etc) but it should be scaled by the tile width: rect.x = c * tW - myCam.getX(). The same goes for rect.y. Right now the x and y coordinates of the tiles are only 1 unit (pixel?) apart.

I'm not sure where your walls are supposed to be because it looks like your code is checking for collision with every tile right now? But anyway, the camera shouldn't have to be used in the collision code. As I wrote before, all the game logic should be done in game coordinates. So if you have some tiles that are walls, you check for collision between them and the player ONLY by the wall's game coordinates and the player's game coordinates. Only in the drawing stage you then use the camera to draw at the correct positions.

Re: How do I make maps scroll

Posted: Sun Apr 26, 2009 10:33 pm
by deathsangel
You are doing rect.x =c - myCam.getX() but c is just the number of the tile (0, 1, 2, etc) but it should be scaled by the tile width: rect.x = c * tW - myCam.getX(). The same goes for rect.y. Right now the x and y coordinates of the tiles are only 1 unit (pixel?) apart.
I forgot to write that there but I did do it in my code.
I'm not sure where your walls are supposed to be because it looks like your code is checking for collision with every tile right now? But anyway, the camera shouldn't have to be used in the collision code.
it does not check collision for tiles of type 0
1 is a green wall
2 is a invisible wall
As I wrote before, all the game logic should be done in game coordinates. So if you have some tiles that are walls, you check for collision between them and the player ONLY by the wall's game coordinates and the player's game coordinates. Only in the drawing stage you then use the camera to draw at the correct positions.
So pretty much only mess with the draw code, dont touch the collision code?