SFML community forums

Help => Graphics => Topic started by: MarcusM on November 19, 2013, 12:18:00 pm

Title: [Sprite] Modifying the bounds of a sprite
Post by: MarcusM on November 19, 2013, 12:18:00 pm
Hi there.

Is it possible to modify the FloatRect retrieved from a sprite by using sf::Sprite::getGlobalBounds?

I have a character sprite that uses a texture of 8x8px, but I want to edit it, so that the physical bounds of the sprite are 7x6px, rather than 8x8px, when checking for collisions against other sprites such as platformer tiles.

I can think of two ways of doing this:

I would prefer the first method.
Title: Re: [Sprite] Modifying the bounds of a sprite
Post by: eXpl0it3r on November 19, 2013, 12:20:52 pm
The sprites bounds are calculated based on the sprites transformations, thus you can't just change it.

And from a design point of view, you should separate your drawing and logic, thus it's okay to use your own Rect. ;)
Title: Re: [Sprite] Modifying the bounds of a sprite
Post by: Nexus on November 19, 2013, 05:29:36 pm
Yes, use a separate rectangle. You shouldn't get it directly from your sprite, but from the logical entity class that represents your character. It is likely that such a class stores other logics-related properties, such as velocity, hitpoints, ...

Since the collision rectangle is the same for all characters of the same type, you don't need to store it in every instance -- instead you could have a central data table with this attribute. Don't make it global, but rather a member of another class.
struct CharacterData // one instance for each different character type
{
    sf::FloatRect collisionRect;
    int maxHitpoints;
    float maxSpeed;
    ...
};

std::vector<CharacterData> table;