How do I make maps scroll

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

How do I make maps scroll

Post 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?
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: How do I make maps scroll

Post by andrew »

You'll find a pretty good way of doing it here:
Lazy Foo' Productions - Tiling
YenTex
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 31
Joined: Sun Mar 01, 2009 9:38 pm

Re: How do I make maps scroll

Post 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.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: How do I make maps scroll

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

Re: How do I make maps scroll

Post by deathsangel »

so your saying to substract the camera x/y from everything not just tiles/player draw?
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How do I make maps scroll

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

Re: How do I make maps scroll

Post 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!
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: How do I make maps scroll

Post 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.
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

Re: How do I make maps scroll

Post 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;
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: How do I make maps scroll

Post 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.
deathsangel
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Apr 18, 2009 8:17 pm

Re: How do I make maps scroll

Post 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?
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine :)
Post Reply