Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving Map Centered Around Player [Solved]  (Read 2722 times)

0 Members and 1 Guest are viewing this topic.

DuvnjakA

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Moving Map Centered Around Player [Solved]
« 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.
« Last Edit: October 06, 2015, 08:24:10 pm by DuvnjakA »

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Moving Map Centered Around Player
« Reply #1 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?

DuvnjakA

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Moving Map Centered Around Player
« Reply #2 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
« Last Edit: October 06, 2015, 08:17:52 pm by DuvnjakA »

DuvnjakA

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Moving Map Centered Around Player
« Reply #3 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. :)
« Last Edit: October 06, 2015, 08:23:50 pm by DuvnjakA »

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Moving Map Centered Around Player [Solved]
« Reply #4 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.

 

anything