C++ SDL2 tile map collision platformer

Started by
3 comments, last by Endurion 6 years, 7 months ago

I'm trying to do platformer with a map reading from a file. I did it. It works well. The problem with it is, I don't know how to implement collision for it, that would be more advanced. I would like to add some more things like moving platforms etc. in the future. I wrote this : File Manager :


void FileManager::LoadFromFile(std::string fileName, 
std::vector<std::vector<int>> &map, std::vector<std::vector<int>> &col, 
std::vector<std::vector<int>> &shadow)
{
fileName = "Files/Config/" + fileName;
std::ifstream openFile(fileName);

map.clear();
col.clear();

if (openFile.is_open())
{
    while (!openFile.eof())
    {
        std::string line;
        std::getline(openFile, line);

        std::vector<int> tempVector;

        if (line.find("[map]") != std::string::npos)
        {
            tempVector.clear();
            state = MAP;
            continue;
        }
        else if(line.find("[collision]") != std::string::npos)
        {
            tempVector.clear();
            state = COLLISION;
            continue;
        }
        else if (line.find("[shadows]") != std::string::npos)
        {
            tempVector.clear();
            state = SHADOW;
            continue;
        }
        std::stringstream str(line);

        while (str)
        {
            std::getline(str, line, ' ');

            if (line != "")
            {
                switch (state)
                {
                    case MAP: tempVector.push_back(atoi(line.c_str())); break;
                    case COLLISION: tempVector.push_back(atoi(line.c_str())); break;
                    case SHADOW: tempVector.push_back(atoi(line.c_str())); break;
                }
            }
        }

        switch (state)
        {
            if (tempVector.size() > 0)
            {
                case MAP: map.push_back(tempVector); break;
                case COLLISION: col.push_back(tempVector); break;
                case SHADOW: shadow.push_back(tempVector); break;
            }

            tempVector.clear();
        }   
    }
}

openFile.clear();
openFile.close();
}

it reads from a file a tile map and a collision map

 

//----------------------------------------------

//------------- GameplayScreen -----------------


bool GameplayScreen::checkCollision(int pLeft, int pTop, int pRight, int 
pBot)
{
   for (int y = 0; y < col.size(); y++)
   {
       for (int x = 0;x < col[y].size(); x++)
       {
           if (col[y][x] != 0)
           {
               int top = y * 32;
               int bot = y * 32 + 32;
               int left = x * 32;
               int right = x * 32 + 32;

               if (pRight < left || pLeft > right || pTop > bot || pBot < top)
               {
                   std::cout << "NIC" << std::endl;
                   return false;
               }
               else
               {
                   std::cout << "COLLISION" << std::endl;
                   return true;
               }
           }
       }
   }
}

this method is going through the collision map, and if somewhere in a file is a number 1, it means that this block is collideable. If it's 0 it means this block isn't colideable. So if somewhere is 1 it returns collision.

//----------------------------------------------

//-------------- Player engine -----------------

https://pastebin.com/P4kCNxNs

Config::getSM() stands for ScreenManager that handle all screens like main menu, options menu, gameplay screen etc. ->GetGame() returns GameplayScreen instance which handle player class, enemies, tiles. ->getPosX() returns position of a map, because it's moving depends of player direction and position. If movespeed is negative, player is moving left, if positive, player moving right. If checkCollision returns false that mean, there is no collision, so player can easly move left or right. So I did check, if(!Config::getSM()->GetGame()->checkCollision()). The problem is that, it always returns false. How can I make it works? How can I make my code more advance?

Advertisement

One of the grand "features" of a tile map is that you can calculate the overlapped tiles from a rectangle easily. So you don't have to traverse the full map:


int x1 = pLeft / 32;
int x2 = ( pRight + 31 ) / 32;
int y1 = pTop / 32;
int y2 = ( pBottom + 31 ) / 32;

for ( int y = y1; y <= y2; ++y )
{  
  for ( in x = x1; x <= x2; ++x )  
  {    
    if ( col[y][x] != 0 )    
    {      
      // collision!    
    }  
  }
}

 

PS: why the P prefix?

 

PSS: This code message editor is annoying me to no end! Why the hell does it remove all line breaks whenever I go to code view.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

P stands for player. Did I forget to say? I'll try this code out soon. Thank you for the answer. I'm wondering about, I don't know how to name this method, collision like this :
left top, right top
left bottom, right bottom
left center, right center
Divide the blocks into these pieces and then compare with player. Have you got any "grand <eatures" for this? 

I usually call that method IsAreaBlocked. Just any descriptive name so you know by the name what it does.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement