SFML 碰撞检测只在原点工作?
SFML collision detection only working at the origin?
我想知道为什么我的碰撞只有在我将精灵移动到屏幕原点时才有效。是我加载地图的方式有问题,还是我不完全理解精灵的工作原理?这是我的 tilemap
代码
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;
}
你只有一个精灵,你改变了它的位置来绘制它。好的,但是当你测试是否有碰撞时,你测试了你设置给你的精灵的最新位置的碰撞。所以,你不应该测试与你的精灵的碰撞。如果您仍然可以访问您的地图,请改用它。
编辑:
我看到了你的postUsing tiles in SFML and collision detection
sf::Texture是资源,sf::Sprite只是一个class来绘制,所以你可以用很少的纹理创建很多精灵。 (std::vector<std::vector<sf::Sprite>> map
)
我想知道为什么我的碰撞只有在我将精灵移动到屏幕原点时才有效。是我加载地图的方式有问题,还是我不完全理解精灵的工作原理?这是我的 tilemap
代码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;
}
你只有一个精灵,你改变了它的位置来绘制它。好的,但是当你测试是否有碰撞时,你测试了你设置给你的精灵的最新位置的碰撞。所以,你不应该测试与你的精灵的碰撞。如果您仍然可以访问您的地图,请改用它。
编辑:
我看到了你的postUsing tiles in SFML and collision detection
sf::Texture是资源,sf::Sprite只是一个class来绘制,所以你可以用很少的纹理创建很多精灵。 (std::vector<std::vector<sf::Sprite>> map
)