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

Author Topic: [Sprite] Modifying the bounds of a sprite  (Read 2917 times)

0 Members and 1 Guest are viewing this topic.

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
[Sprite] Modifying the bounds of a sprite
« 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:
  • Being able to modify the bounds of the sprite or
  • by modifying my collision detection code, so that instead of parsing the sf::Sprite of my objects, I should pass a custom sf::FloatRect which holds the collision detection bounds.

I would prefer the first method.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Sprite] Modifying the bounds of a sprite
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Sprite] Modifying the bounds of a sprite
« Reply #2 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;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: