LJR
Scrolling maps.
For ease, let's just assume the level is one huge bitmap, say 1024x1024, and our the screen is 640x480.
That means the coordinates of the map can range from 0 to (1024-640), 0 to (1024-480) and not have any 'run-off' which extends past the map edges when rendered on screen.
When you center the player to the screen, you'd use something like the following to come up with the top-left coordinates to start blitting them map and the top-left of the screen (remember player coordinates are in mapspace, not screenspace).
map_x = player_x - (640 / 2);
map_y = player_y - (480 / 2);
Easy enough, let's take an example:
player_x = 500;
player_y = 600;
calculates to (using above calcs):
map_x = 180;
map_y = 360;
Ok, now let's say the player is much further left and up:
player_x = 200;
player_y = 100;
calculates to:
map_x = -120;
map_y = -140;
Hmm - that won't work, so now what? Those negative values are actually the offset to which move the player from the center of the screen! So, now throwing in the players screenspace coordinates in there:
// get center of screen coords
screen_x = (640/2);
screen_y = (480/2);
// adjust if at top/left edge of map
if(map_x < 0) {
screen_x += map_x;
map_x = 0;
}
if(map_y < 0) {
screen_y += map_y;
map_y = 0;
}
That's it! Now, how about the right and bottom edges? That's just as easy, since we can check if the map coordinates are greater then (1024-640),(1024-480):
if(map_x > (1024-640)) {
screen_x += (map_x - (1024-640));
map_x = (1024-640);
}
if(map_y > (1024-480)) {
screen_y += (map_y - (1024-480));
map_y = (1024-480);
}
Please excuse me if some of the math is a little off - I'm typing this from memory.
Jim Adams
[This message has been edited by Jim Adams (edited November 12, 1999).]
One which moves the "mans" tile position, based on user input or whatever.
After that, you call a "centering" or view positioning function.
When the man is out in the middle, it just centers the view on him. But once it reaches the edges, it clips the view to the boundry of the tile map, so it no longer moves as the man moves the last dozen or whatever tiles to the corner.
May be what the other poster was saying, but it's late and I felt the need to explain the general process.
Ok, I got a map and a man in the center of the screen. As I move him in any direction, he stays in the center and the map scrolls.
Still with me? Good.
Now say the map can't scroll anymore because it has reached its boundry. Now I want the man to start moving towards the boundry.
Can anyone help me set up this up?
Also, I need help on placing sprites on specific areas of the map. For instance, say I wanted to place the sprite at (50,50) and have him centered on the screen. How would I have the map scroll to the right place? And what if I put him where he couldn't be centered because of the map boundries? Like placing him in cell (0,0)?
Thanks in advance for the help.
- Ace