Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

C++ SDL2 tile map collision platformer

$
0
0
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?

Viewing all articles
Browse latest Browse all 17825

Trending Articles