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

Author Topic: Sprite doesn't transform in an inherited class  (Read 1260 times)

0 Members and 2 Guests are viewing this topic.

Phyton

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprite doesn't transform in an inherited class
« on: September 27, 2019, 04:55:53 pm »
Hi Forum,
i faced a problem with my inherited class and sf::sprite.
I have a custom class, that inherits from sf::Drawable and sf::Transformable
Here it is:
class DrawableObject : public sf::Drawable,public sf::Transformable
{
public:
        DrawableObject();
        ~DrawableObject();
        bool loadImage(std::string);
        sf::FloatRect getFloatRect();

private:
        sf::Texture m_texture;
        sf::Sprite m_sprite;
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                states.transform *= getTransform();
                target.draw(m_sprite, states);
        }
};
 
It works quite fine...i can render and move as expected !
But now i wanted to implement collision. I read that sf::transformable has no getGlobalBounds() function so
i thought that i just use the getGlobalBounds method from my sprite(m_sprite), but every value from the position to the FloatRect is nonsense(doesnt fit the values directly obtained from my class) !

Is there a way i can apply every tranformation i made to the class also to the Sprite ?
Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Sprite doesn't transform in an inherited class
« Reply #1 on: September 27, 2019, 08:03:25 pm »
You can transform the sprite position with the point transformation of an sf::Transform.
It would probably be easier (and cleaner) to just calculate the bounding box yourself.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Phyton

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sprite doesn't transform in an inherited class
« Reply #2 on: September 28, 2019, 09:24:08 am »
So i did write my own getGloabalBounds and it works good.
Thanks !