SFML community forums

Help => General => Topic started by: AdrianHEY on April 16, 2021, 12:09:04 pm

Title: Sprite rotated hit box broken
Post by: AdrianHEY 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?
Title: Re: Sprite rotated hit box broken
Post by: kojack 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).

https://www.youtube.com/watch?v=59BTXB-kFNs
Title: Re: Sprite rotated hit box broken
Post by: AdrianHEY on April 17, 2021, 11:14:35 am
I am going to try to write the code. Thanks for the reply!
Title: Re: Sprite rotated hit box broken
Post by: kojack 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.