Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rotating Tiles and keeping the player within Tile  (Read 1619 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Rotating Tiles and keeping the player within Tile
« on: May 12, 2020, 12:51:31 am »
Hello,

This is just a concept I'm trying to understand, I got curious about :-X. I have basic sf::RectangleShape's in a Tile struct and just being displayed 50 by 50 with a 64x64 tile size. I have a 32x32 rectangle shape considered the player that you can move around with the WASD keys.

With Q and E I have a backward and forward rotation of all tiles by -5 and 5. How exactly could I maintain keeping the player within the same tile? So essentially, moving by the rotation of the tiles? I thought I could just grab the position of the tile the player is on and rotate him the same amount. But that doesn't seem to work.

Thanks

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating Tiles and keeping the player within Tile
« Reply #1 on: May 12, 2020, 01:57:58 am »
You want to keep the player's relative position within the rectangle to remain the same after rotation of the rectangle?

Before rotation, convert the player's position to the rectangle's space:
sf::Vector2f convertedPlayerPosition{ rectangle.getInverseTransform().transformPoint(playerPosition) };

Then, transform your rectangle how you want (rotate the tiles).

Then transform the converted player position using the rectangle's transform:
sf::Vector2f newPlayerPosition{ rectangle.getTransform().transformPoint(convertedPlayerPosition) };

Don't forget to rotate the player too.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Rotating Tiles and keeping the player within Tile
« Reply #2 on: May 12, 2020, 03:31:01 am »
Hey,

Thanks for the attempt to help me. I greatly appreciate it. I wanted to clarify that I'm understanding your solution? Because it's not working out. I'm not exactly sure what you mean by "don't forget to rotate?" I'm sure there is something simple I'm missing. Following up, is there any way you can explain what's going on exactly to achieve this? What should I be searching to look more into this? Thanks

 
                auto playerPosition = player.getPosition();

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
                       for (auto& tile : m_map)                        
                                tile.rotate(-5);

                       for (auto& tile : inner_map)
                                tile.rotate(-5);

                        sf::Vector2f convertedPlayerPosition{
       m_map[7 * MapWidth + 11].getInverseTransform().transformPoint(playerPosition) };

                       player.rotate(-5);

                        sf::Vector2f newPlayerPosition{
         m_map[7 * MapWidth + 11].getTransform().transformPoint(convertedPlayerPosition) };

                      player.setPosition(newPlayerPosition);
/} // Q Key End
 

if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
                        for (auto& tile : m_map)
                                        tile.rotate(5);

                        for (auto& tile : inner_map)
                                        tile.rotate(5);

                         sf::Vector2f convertedPlayerPosition{
         m_map[7 * MapWidth + 11].getInverseTransform().transformPoint(playerPosition) };

                         player.rotate(5);

                         sf::Vector2f newPlayerPosition{
         m_map[7 * MapWidth + 11].getTransform().transformPoint(convertedPlayerPosition) };

                         player.setPosition(newPlayerPosition);        
}  // E Key End
 
PS: Yes, I want the rectangle (player) to stay within the rectangle (tile) that it is in. I hardcoded the player's position at (11, 7) to try to see if it worked. I tried to make the code look nice here, sorry if it looks bad.
« Last Edit: May 12, 2020, 03:37:28 am by SFMLNewGuy »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating Tiles and keeping the player within Tile
« Reply #3 on: May 12, 2020, 09:22:03 pm »
By "don't forget to rotate", I only meant so that your player is the same angle as the tile.

The "tile"'s inverse transform must be retrieved before the rotation/movement of the tile. The normal transform must be afterwards.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*