Ich habe mich gefragt, warum meine Kollision nur funktioniert, wenn ich mein Sprite zum Ursprung des Bildschirms verschiebe. Ist es ein Problem mit der Art, wie ich meine Karte geladen habe, oder gibt es etwas darüber, wie Sprites funktionieren, die ich nicht vollständig verstehe? Hier ist mein Code für die tilemapSFML-Kollisionserkennung funktioniert nur am Ursprung?
Tilemap.cpp
#include "Tilemap.h"
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace sf;
//default constructor
Tilemap::Tilemap()
{
if (!texture.loadFromFile("map/purpleBlock.png"))
{
cout << "Error opening file" << endl;
}
sprite.setTexture(texture);
}
//reads tilemap from a text file into an array
void Tilemap::loadTile(RenderWindow &window, string filename)
{
string temp;
ifstream mapFile;
mapFile.open("map/test.map");
if (!mapFile.is_open())
{
cout << "Error opening " << filename << endl;
}
//getline(mapFile, tileSet);
//reading height, width, tile height, tile width and storing them into variables
getline(mapFile, temp);
tileWidth = stoi(temp, nullptr);
getline(mapFile, temp);
tileHeight = stoi(temp, nullptr);
getline(mapFile, temp);
width = stoi(temp, nullptr);
getline(mapFile, temp);
height = stoi(temp, nullptr);
data = new int[width*height];
//reading values into array
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
char temp;
mapFile >> data[x + y * width] >> temp;
}
}
mapFile.close();
}
//drawing the map onto the screen
void Tilemap::drawMap(RenderWindow &window)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
if (!data[x + y * width] == 0)
{
sprite.setPosition(x * 32, y * 32);
sprite.setTextureRect(IntRect(0, 0, tileWidth, tileHeight));
window.draw(sprite);
}
}
}
}
//testing collision
bool Tilemap::tileCollision(RenderWindow &window, RectangleShape &rect)
{
if (sprite.getGlobalBounds().intersects(rect.getGlobalBounds()))
{
cout << "Collision" << endl;
}
return true;
}