How to “set the origin” of a Transform in sfml

試著忘記壹切 提交于 2021-02-10 18:22:39

问题


I have a problem using sfml where it seems sf::Transforms get applied relative to the origin of whole coordinate system and not the origin of the sprite/thing being transformed.

For example in the two following example I expect the same behavior and it's not what happens. (differences in bold, most of rest just serves to actually display a sfml window with a circle that gets scaled down when space is pressed)

First Example : here the circle is scaled down towards the top left corner of the window



    #include &ltSFML/Graphics.hpp&gt
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(800, 800), "Example");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
    
        shape.setPosition(400, 400);
        
        sf::Transform t;
        t.scale( 9/10.f , 9/10.f);
        sf::Transform t_n; 
          
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed){
                    window.close();
            }
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::Space) {
                    t_n = t_n * t; 
                }
            }
        } 
        
              
        window.clear();
        window.draw(shape, t_n); 
        window.display();
        }
        
        return 0;
      
    }
    

Second Example : Here the circle is scaled down towards the top left corner of its bounding box : this is the kind of behavior I want to achieve but using Transforms


    #include &ltSFML/Graphics.hpp&gt
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(800, 800), "Example");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
    
        shape.setPosition(400, 400);
        
        // no Transforms need declaring
        
        
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed){
                    window.close();
            }
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::Space) {
                    shape.scale(9/10.f, 9/10.f);
                }
            }
        } 
        
              
        window.clear();
        window.draw(shape); 
        window.display();
        }
        
        return 0;
      
    }
    

note: this is a possible duplicate of Transformations igrnores sf::Sprite's origin , but the only answer to this thread is my own, and the solution I suggest is really not ideal.

来源:https://stackoverflow.com/questions/65620996/how-to-set-the-origin-of-a-transform-in-sfml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!