1
Graphics / Re: Rotating Quads within Tile Map
« on: November 14, 2018, 09:42:37 pm »Sounds like you're not passing the correct element from your pose array (or the correct index of the element, however you wanna call it). Would be more helpful if you post all the relevant parts of the code.
I don't use forums like this often and it seems my image attachments aren't displaying :/
Here is a link to my output, you'll notice that all the tiles of the same part of the tileset have the same rotation. https://imgur.com/JhBUlVe
But heres some snippets
(click to show/hide)
// My set orientation function
void setOrientation(sf::Vertex* quad, int pose, int tu, int tv,
sf::Vector2u tileSize, int tileNumber) {
switch (pose) {
case 0: // Normal Pose
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
case 1: // Horizontal Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[1].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[3].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
case 2: // Vertical Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
case 3: // Horizontal + Vertical Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[1].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[2].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[3].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
}
}
This is the code the example gave that I have changed slightly by moving the tex cords assignment into the function.void setOrientation(sf::Vertex* quad, int pose, int tu, int tv,
sf::Vector2u tileSize, int tileNumber) {
switch (pose) {
case 0: // Normal Pose
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
case 1: // Horizontal Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[1].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[3].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
case 2: // Vertical Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
case 3: // Horizontal + Vertical Flip
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); // 2
quad[1].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); //3
quad[2].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); // 0
quad[3].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); // 1
}
}
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i) {
for (unsigned int j = 0; j < height; ++j) {
// get the current tile number
int tileNumber = tiles[i + j * width];
int tilePose = pose[i + j * width];
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
setOrientation(quad, tilePose, tu, tv, tileSize, tileNumber);
}
}
My array that gets passed infor (unsigned int i = 0; i < width; ++i) {
for (unsigned int j = 0; j < height; ++j) {
// get the current tile number
int tileNumber = tiles[i + j * width];
int tilePose = pose[i + j * width];
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
setOrientation(quad, tilePose, tu, tv, tileSize, tileNumber);
}
}
const int poses[] =
{
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 1,
0, 0, 3, 3, 1, 0, 3, 3, 3, 3, 1, 1, 0, 0, 0, 1,
0, 1, 0, 2, 3, 2, 2, 2, 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