Reputation: 39
Hi just a quick question. wondering what the best way to implement collisions with the map tiles i have setup. I was going to setup a rectangle for the player and check if it intersects with the rectangles that are solid. my only problem with this is that for some reason it gives me an error if i declare my rectangle in the header files. meaning I cant check to see if my player intersects with my map tiles because I cant use the rectangle from my map class in the player class and vice-versa. any help would be much appreciated.
Here is my map class:
#include "Map.h"
#include "Block.h"
#include <sstream>
using namespace std;
Map::Map()
{
//map ctor;
}
Map::~Map()
{
// map dtor
}
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
void Map::Initialise(const char *filename)
{
MapImage.LoadFromFile("Images/block.png");
std::ifstream openfile(filename);
std::vector <int> tempvector;
std::string line;
while(std::getline(openfile, line))
{
for(int i =0; i < line.length(); i++)
{
if(line[i] != ' ') // if the value is not a space
{
char value = line[i];
tempvector.push_back(value - '0');
}
}
mapVector.push_back(tempvector); // push back the value of the temp vector into the map vector
tempvector.clear(); // clear the temp vector readt for the next value
}
}
void Map::DrawMap(sf::RenderWindow &Window)
{
sf::Color rectCol;
sf::Sprite sprite;
for(i = 0; i < mapVector.size(); i++)
{
for(j = 0; j < mapVector[i].size(); j++)
{
if(mapVector[i][j] == 0)
rectCol = sf::Color(44, 117, 255);
else if(mapVector[i][j] == 1)
rectCol = sf::Color(255, 100, 17);
rect.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE);
rect.SetColor(rectCol);
Window.Draw(rect);
}
}
}
Upvotes: 0
Views: 932
Reputation: 276
You can create another class which will handle the map and player classes. For collision I advice you to use AABB( axis aligned bounding box) and to verify every frame the collision.
Upvotes: 1