Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Scrolling maps.

This topic is 6910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is considering that you don't want a black outline around your map? Some games just have a blank area as the boundry, in that case you could just keep your original way of doing things and not worry about when to switch over to boundry limit mode.


Share this post

Link to post
Share on other sites
This is actually simple.

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

Share this post

Link to post
Share on other sites
A simple way to do this, is to have two routines.

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.

Share this post

Link to post
Share on other sites
I want to thank everyone for their replies. I decided I'd just take the easy way out and have the screen centered around him at all times and to just allow the area where there was no map to come into view.

- Ace

Share this post

Link to post
Share on other sites
I really hope I ask this clearly...

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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!