How do I make maps scroll
Moderator: Coders of Rage
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
How do I make maps scroll
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?
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
Re: How do I make maps scroll
You'll find a pretty good way of doing it here:
Lazy Foo' Productions - Tiling
Lazy Foo' Productions - Tiling
Re: How do I make maps scroll
from what I remember, It's a lot like moving a sprite, only when your sprite moves up the BG moves down etc.
- RyanPridgeon
- 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
This is because you need to also relate the x and y in your collisions code to the camera's x and y.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.
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.
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
Re: How do I make maps scroll
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
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: How do I make maps scroll
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.
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
Re: How do I make maps scroll
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
-
- Chaos Rift Newbie
- Posts: 20
- Joined: Fri Mar 20, 2009 4:37 am
- Location: Germany
Re: How do I make maps scroll
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.
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.
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
Re: How do I make maps scroll
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
and the tile draw
and finally in in the update call to bool touches_wall(Player myPlayer, Camera myCam)
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));
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);
}
}
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
-
- Chaos Rift Newbie
- Posts: 20
- Joined: Fri Mar 20, 2009 4:37 am
- Location: Germany
Re: How do I make maps scroll
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.
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.
-
- Chaos Rift Newbie
- Posts: 30
- Joined: Sat Apr 18, 2009 8:17 pm
Re: How do I make maps scroll
I forgot to write that there but I did do it in my code.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.
it does not check collision for tiles of type 0I'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.
1 is a green wall
2 is a invisible wall
So pretty much only mess with the draw code, dont touch 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.
Thanks to gyro vorbis for inspiring me to learn how to make a 2d graphics engine