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

Author Topic: Sprite rotated hit box broken  (Read 2275 times)

0 Members and 2 Guests are viewing this topic.

AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
Sprite rotated hit box broken
« on: April 16, 2021, 12:09:04 pm »
I tried to make it so if the player intersects the sprite global bound it despawns. But if the sprite is rotated the hitbox is wierd and it hits in some wierd parts it isn't supposed to hit. Sorry for the bad english
bool checked = false;
                for (int i = 0; i < enemy1.vectorOfEnemies.size(); i++) {
                    for (int j = 0; j < bullet1.vectorOfBullets.size() && checked == false; j++) {
                        if (enemy1.vectorOfEnemies[i].getGlobalBounds().intersects(bullet1.vectorOfBullets[j].getGlobalBounds())) {

                            bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + j);
                            bullet1.vectorOfRotations.erase(bullet1.vectorOfRotations.begin() + j);
                            enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
                            enemy1.vectorOfRotations.erase(enemy1.vectorOfRotations.begin() + i);

                           

                            j--;

                            killEnemy.playAudio();

                            bullet1.cateGloante++;
                            checked = true;

                        }
                    }
                }
Can I make it so the hitbox seems more normal if the sprite is rotated?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Sprite rotated hit box broken
« Reply #1 on: April 17, 2021, 04:20:49 am »
SFML uses Axis Aligned Bounding Boxes for the global bounds. This means they can't rotate, instead they will grow and shrink as needed to fit the rotated sprite. This is good for rendering, but not for game collisions.

You'll either need to write your own Oriented Bounding Box intersect test (such as Separating Axis Theorem) or use a library like Box2D (2D physics).



AdrianHEY

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Sprite rotated hit box broken
« Reply #2 on: April 17, 2021, 11:14:35 am »
I am going to try to write the code. Thanks for the reply!

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Sprite rotated hit box broken
« Reply #3 on: April 17, 2021, 07:17:53 pm »
You might want to check this out: https://en.sfml-dev.org/forums/index.php?topic=22241.0
Sounds like it does what you want.

 

anything