2D RPG move map, keep player centered

B29237c2e6976af375c56ee8e6eaa720
0
Kekke88 101 May 06, 2011 at 16:56

Hello everyone!

I have a question where I need some design help..

I am currently developing a ‘fun’ RPG game with a centered player that can move up, down, left and right.

But how do I move my map according to how the player moves?

How should my map look?

There should be non walkable places, and you should be able to throw things on the ground, what I was thinking was maybe have a class called something like cGroundUnit which was a 32x32 big texture that together with loads of them built a map.

But if I have let’s say 1000 cGroundUnits and move them in a foreach loop, I think the ground will have glitches like the ground is falling apart.

Is there any standard way to do this that I am not aware of?

//Kekke

5 Replies

Please log in or register to post a reply.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 May 06, 2011 at 17:42

I think that’s basically how they do it. I think you only show and move what’s visible from a map array. The best way to do this kind of thing in my book is start with some 32X32 tiles and experiment with it. First move one, then a couple, then a section of a map. They’re not going to fall apart if you move them each the same amount, but you have to decide when to add sections from the map and delete sections that are no longer visible.
You can probably also find tutorials or even engines that do that kind of thing, but it’s sometimes more interesting to come up with your own ideas and implement them.

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 101 May 06, 2011 at 18:15

Do not move map, move player only. This way problem of moving all map tiles transforms to problem of displaying map correctly. Effectively it is moving only visible tiles. So, first, determine which tiles will be visible (in form xMin-xMax, yMin-yMax for example) and, second, draw these tiles with displacement of reverse player position (tile with world coordinates xTile, yTile draw at position xTile-xPlayer, yTile-yPlayer).

B29237c2e6976af375c56ee8e6eaa720
0
Kekke88 101 May 06, 2011 at 23:00

@’

‘]Do not move map, move player only. This way problem of moving all map tiles transforms to problem of displaying map correctly. Effectively it is moving only visible tiles. So, first, determine which tiles will be visible (in form xMin-xMax, yMin-yMax for example) and, second, draw these tiles with displacement of reverse player position (tile with world coordinates xTile, yTile draw at position xTile-xPlayer, yTile-yPlayer).

Interesting.. are you saying like I should draw the player to center of the screen, BUT keep track of the X and Y coords (as in move player +5 X but keep drawing him in the center, and then draw mapX - playerX) and draw the map?

Will definitely try this tomorrow and post back how it goes..
@fireside

I think that’s basically how they do it. I think you only show and move what’s visible from a map array. The best way to do this kind of thing in my book is start with some 32X32 tiles and experiment with it. First move one, then a couple, then a section of a map. They’re not going to fall apart if you move them each the same amount, but you have to decide when to add sections from the map and delete sections that are no longer visible.
You can probably also find tutorials or even engines that do that kind of thing, but it’s sometimes more interesting to come up with your own ideas and implement them.

That is true, and for me, it is always much more interesting to come up and think by yourself rather than using something created by someone else (even though I know most of the programmers don’t wanna re-invent the wheel, I always want to re-invent it to learn before I use something already finished).

//Kekke

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 101 May 07, 2011 at 09:03

@Kekke88

Interesting.. are you saying like I should draw the player to center of the screen, BUT keep track of the X and Y coords (as in move player +5 X but keep drawing him in the center, and then draw mapX - playerX) and draw the map? Will definitely try this tomorrow and post back how it goes..

I suggest even to make the player one of the scene objects (derived from cMonster for example) and keep track of screen center in different pair of variables. Usually xScreen = xPlayer, yScreen = yPlayer, but you can easily handle special cases with the player displaced from the screen center (approaching map edge, earthquake, etc…).

B29237c2e6976af375c56ee8e6eaa720
0
Kekke88 101 May 07, 2011 at 15:37

Thanks for the help.

I made a big step and this is what I currently have;

http://www.youtube.com/watch?v=VqQcse77lzk

Collision Detection Code:

//Move character etc..

if (TileCollision())
            {
                player.X = (int)oldPlayerPosition.X;
                player.Y = (int)oldPlayerPosition.Y;
            }

// I should change this into only comparing to visible tiles
public bool TileCollision()
        {
            foreach (cGroundUnit curGround in myMap.GroundUnits)
            {
                if (curGround.Intersect(this.player) && !curGround.Walkable)
                    return true;
            }

            return false;
        }

//Normal intersect from XNA
public bool Intersect(cPlayer player)
        {
            Rectangle test = this.CollBoundingBox(player);
            if (player.BoundingBox.Intersects(test))
                return true;
            else
                return false;
        }

Now I just need to make a map editor..