SFML community forums

Help => Graphics => Topic started by: DuvnjakA on October 06, 2015, 07:58:43 pm

Title: Moving Map Centered Around Player [Solved]
Post by: DuvnjakA on October 06, 2015, 07:58:43 pm
int y = playerPosition.y;
         int x = playerPosition.x;

         for (int i = y - 5; i < y + 5; i++)
            for (int j = y - 5; j < y + 5; j++)
            {
               switch (MainMap[j])
               {
               case 0:
                  zeros.setPosition({ j * 20.f, i * 20.f });
                  window.draw(zeros);
                  break;
               case 1:
                  ones.setPosition({ j * 20.f, i * 20.f });
                  window.draw(ones);
                  break;
               case 2:
                  twos.setPosition({ j * 20.f, i * 20.f });
                  window.draw(twos);
                  break;
               }
            }
         player.setPosition({ playerPosition.x * 20.f, playerPosition.y * 20.f });
         window.draw(player);

Would this be a correct way to approach this? My "Map" has 5 "walls" on each side (top, bottom, left, right) so that there will always be a drawing on the left,right,top,bottom regardless of where the player should be.
However, when I run this with the rest of my code, I get a small map that isn't centered around the player. I'm trying to figure out how to get the player centered all the time so that the map essentially follows the player. Does that make sense?
As I was saying, I get a strange map that has some pieces appearing and disappearing at different points where the player ball just moves around wherever with some strange limitations.
Title: Re: Moving Map Centered Around Player
Post by: AFS on October 06, 2015, 08:13:08 pm
^ Sorry, I deleted my previous answer because it was incorrect.

I'm not sure I understand what you are trying to do. You want your map to be basically a static background?
Title: Re: Moving Map Centered Around Player
Post by: DuvnjakA on October 06, 2015, 08:15:18 pm
Okay, I have a set tilemap that's like [13][21] (it's a vector so subject to change).

Screen size is 240,240 pixels.

Each tile is 20x20 . Thus allowing me to draw 5 blocks to the left and the right of my player.

What I'm trying to do is have the screen ONLY draw the 5 blocks to the left,right,above, and below my player block.

I have it drawing the map to the specifications I want, but the problem I'm having is my player block isn't moving 1 tile over at a time.

I can post a link to the code if you wish to see exactly what I'm doing and run it.

As I said, the sole problem i'm having right now is having the character block move only 1 tile at a time.

Link to the code:
http://hostcode.sourceforge.net/view/3499
Title: Re: Moving Map Centered Around Player
Post by: DuvnjakA on October 06, 2015, 08:22:02 pm
I see where my error is but I'm struggling to fix it. My error is drawing my actual player block. How can I set it in relation to the screen to always be in the middle? Set it to be a constant position in the middle?


Scratch that. Solved my issue. I merely set the player block to a set position in the center of my screen. :)
Title: Re: Moving Map Centered Around Player [Solved]
Post by: mkalex777 on October 07, 2015, 12:40:09 am
It seems that you are using render logic which is hard to understand and hard to control.
You can improve it by removing hard dependency of player entity and screen.
Just update coordinates and size of entities of your game world and don't touch screen.
At renderer code use View and just set it's center to the desired position.
So it will works like camera. And you can simply copy first person entity coordinates into View.Center and your first person will be always at the center of screen. At any moment you can change View.Center position and it will not affect your first person coordinates, so at any moment you can start to move camera from the first person to another world location.