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

Author Topic: Collision detection between sprite and shape  (Read 3763 times)

0 Members and 1 Guest are viewing this topic.

kingguru

  • Newbie
  • *
  • Posts: 12
    • View Profile
Collision detection between sprite and shape
« on: May 03, 2021, 08:56:47 pm »
I know collision detection is most likely a topic that has been brought up here quite often, so I'm sorry for bringing it up once again.

First of all, I am not very experienced with SFML, game development and 2D graphics in general so please bear with me.

I have written a very basic game using where balls (basically an sf::CircleShape) can hit a player (basically an sf::Sprite) and while using the intersects function works just fine I would of course prefer a bit more "pixel perfect collision detection".

Instead of reinventing the wheel I have found a few snippets like this one.

I've had to change the code slightly to use an sf::Shape for one of the objects, but I cannot really see how it should matter.

The problem is, that a collision very rarely happens and for small shapes, it seems like it doesn't happen at all.

My sf::CircleShape uses a texture that scales depending on the size and I have a suspicion that when comparing the pixels, the comparison happens using the full sized image/texture where only a corner of the original image is used which mostly only contains transparent pixels. As far as I've understood though, getInverseTransform should take care of that transformation right?

Hope my question makes some kind of sense. I am of course willing to share code and more details, but I'm a bit lost so I hope someone could give my some guidance from here.

Thanks a lot!


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Collision detection between sprite and shape
« Reply #1 on: May 04, 2021, 09:58:06 am »
Very few games use pixel perfect collision detection. In most cases it's really not necessary and it's much simpler to have a bounding box that's maybe a bit smaller than the visible object or multiple boxes that match the shape more closely.

I'd have to have a longer look at the code, but yes it seems that it would do reverse the transformation, to match the same points on the textures. So it is doing the check on the original image (or mask), but the inverse transform should calculate the correct position.

Looks like it's the same code as on the SFML Wiki: https://github.com/SFML/SFML/wiki/Source%3A-Simple-Collision-Detection-for-SFML-2
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kingguru

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Collision detection between sprite and shape
« Reply #2 on: May 04, 2021, 11:00:09 pm »
Thanks a lot eXpl0it3r.

I know and understand that pixel perfect collision detection is rarely needed, it just kind of bothered me that I could clearly see that the player died even if an actual collision hadn't occurred. Could definitely be because I know that is the case though and not a real problem  :)

The game in question is a very basic attempt at reimplementing the class Pang! game that I started years ago and only recently had a look at again. The code is shared under the GPL here.

There are tons of known issues with the code and of course even more I don't know about, but it's mostly just a way for me to try and play with writing code in a field I'm not very experienced in, so please keep that in mind if you feel like you have the courage to read the code :)

The collision detection I wanted to improve is currently happening in the main function (it shouldn't be there, I know) and is simply using the intersects member function.

It is the indeed the same code from the SFML wiki I've tried using. I thought it would be more or less "drop in" which is why I'm a bit puzzled.

Considering the actual problem (improving collision detection a bit), I've thought about using some basic geometric calculation to figure out if the circle intersects with the (player) rectangle instead. Maybe that would be a better solution?

Thanks a lot once again. You're input is most appreciated.

 

anything