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

Author Topic: Adding hitboxes/shapes/textures to a specific part of a sprite  (Read 1049 times)

0 Members and 2 Guests are viewing this topic.

DarthDuck

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Hello, I'm new to SFML and currently working on my first project with the library. I'm working on a really basic fantasy RPG and I'm trying to create a system to detect when the pixels of, (or at this point a rough rectangle around) a weapon collide with another entity, and do damage if a collision is true.

I know how to implement collision detection and I have an animation system working, the problem I have with creating a hitbox is that the "weapon" isn't it's own entity, it's just a part of the sprite texture for the time being.  I've checked the docs looking for a way to attach a separate shape or texture (like a bounding box) to a sprite but I haven't found anything. I've also tried creating an Sf rectangle shape to use as an underlay of sorts but getting it to move with the sprite has been tedious, and I'd assume computationally inefficient.

Could anyone with similar experience provide possible solutions? I'd appreciate it.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Adding hitboxes/shapes/textures to a specific part of a sprite
« Reply #1 on: August 27, 2023, 08:10:49 pm »
It sounds like you have an image that has something that includes the weapon. Is that right? I will assume it is for what I'm about to say although it should be similar even if that isn't the case.

Anyway, as I was reading I was thinking that you need a separate rectangle to check but I'd say an sf::FloatRect is all that is needed rather than an entire sf::RectangleShape, although they're quite cheap.

My fuller suggestion would be to create "hitboxes" for everything and then check them.
That is, something like this:
std::vector<sf::FloatRect> hitboxes;

Or, indeed, create separate entities that contain them. Something maybe like this:
class Entity
{
public:
    // ..
    sf::FloatRect getHitbox() const { return m_hotbox; }
private:
    sf::FloatRect m_hitbox;
};

You can then go on to make a linking system that allows entities to be linked to another and that should help with making one entity (the weapon with its hitbox) follow another. Of course, an entity doesn't necessarily need to be drawable (it may or may not have a sprite, for example).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything