c++ - Why is sfml entity.setOrigin() not working? - Stack Overflow

I wrote some SFML code in C++ to set the sprite's origin to its center (0.125f) so that i can rota

I wrote some SFML code in C++ to set the sprite's origin to its center (0.125f) so that i can rotate it on its center:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <iostream>

using namespace sf;

void keys();
float deg = 0.0f, y = 0.0f;
int speed = 5;

int main()
{
    RenderWindow win(VideoMode({500,500}),"Bogus Run",Style::Close | Style::Titlebar);
    Font font("arial.ttf");
    Text fps(font,"0",25);
    fps.setFillColor(Color::Blue);
    win.setFramerateLimit(30);

    Texture ps("sprites/playerSprite.png");

    Sprite plr(ps);
    plr.setOrigin({0.125f,0.125f});
    plr.scale({.25f,.25f});
    std::cout << plr.getOrigin().x << std::endl << plr.getOrigin().y;

    while(win.isOpen()){
        while(std::optional ev = win.pollEvent()){
            if(ev->is<Event::Closed>()){
                win.close();
            }
        }

        //fps.setString("0");
        win.clear(Color::Cyan);
        win.draw(fps);
        keys();
        plr.move({0.0f,y});
        plr.rotate(degrees(deg));
        win.draw(plr);
        win.display();
    }

    return 0;
}

void keys(){
    if(Keyboard::isKeyPressed(Keyboard::Scancode::D)){
        deg = 1.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::A)){
        deg = -1.0f;
    }
    if(!Keyboard::isKeyPressed(Keyboard::Scancode::A) && !Keyboard::isKeyPressed(Keyboard::Scancode::D)){
        deg = 0.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::W)){
        y = -1.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::S)){
        y = 1.0f;
    }
    if(!Keyboard::isKeyPressed(Keyboard::Scancode::S) && !Keyboard::isKeyPressed(Keyboard::Scancode::W)){
        y = 0.0f;
    }
    deg *= speed;
    y *= speed;
}

...but when ever i run the code the rotation always occurs on the top left corner.

I wrote some SFML code in C++ to set the sprite's origin to its center (0.125f) so that i can rotate it on its center:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <iostream>

using namespace sf;

void keys();
float deg = 0.0f, y = 0.0f;
int speed = 5;

int main()
{
    RenderWindow win(VideoMode({500,500}),"Bogus Run",Style::Close | Style::Titlebar);
    Font font("arial.ttf");
    Text fps(font,"0",25);
    fps.setFillColor(Color::Blue);
    win.setFramerateLimit(30);

    Texture ps("sprites/playerSprite.png");

    Sprite plr(ps);
    plr.setOrigin({0.125f,0.125f});
    plr.scale({.25f,.25f});
    std::cout << plr.getOrigin().x << std::endl << plr.getOrigin().y;

    while(win.isOpen()){
        while(std::optional ev = win.pollEvent()){
            if(ev->is<Event::Closed>()){
                win.close();
            }
        }

        //fps.setString("0");
        win.clear(Color::Cyan);
        win.draw(fps);
        keys();
        plr.move({0.0f,y});
        plr.rotate(degrees(deg));
        win.draw(plr);
        win.display();
    }

    return 0;
}

void keys(){
    if(Keyboard::isKeyPressed(Keyboard::Scancode::D)){
        deg = 1.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::A)){
        deg = -1.0f;
    }
    if(!Keyboard::isKeyPressed(Keyboard::Scancode::A) && !Keyboard::isKeyPressed(Keyboard::Scancode::D)){
        deg = 0.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::W)){
        y = -1.0f;
    }
    if(Keyboard::isKeyPressed(Keyboard::Scancode::S)){
        y = 1.0f;
    }
    if(!Keyboard::isKeyPressed(Keyboard::Scancode::S) && !Keyboard::isKeyPressed(Keyboard::Scancode::W)){
        y = 0.0f;
    }
    deg *= speed;
    y *= speed;
}

...but when ever i run the code the rotation always occurs on the top left corner.

Share Improve this question edited Jan 17 at 17:43 genpfault 52.2k12 gold badges91 silver badges151 bronze badges asked Jan 17 at 17:35 thethe 12 bronze badges 11
  • 1 "plr.setOrigin({0.125f,0.125f})" - that's setting the origin pretty darn close to the top left corner.. I doubt there'd be any visible difference to 0.0, 0.0 in most situations. So, it's not broken, it's doing exactly what you asked for. You just asked for a thing that's different from what you wanted. – Jesper Juhl Commented Jan 17 at 17:45
  • yes, but the size of the sprite is {0.25f,0.25f} or, does the origin function work a little different? – the Commented Jan 17 at 17:49
  • I doubt your sprite is a quarter by a quarter pixel. You are somehow misunderstanding the API. – Jesper Juhl Commented Jan 17 at 17:50
  • That actually makes sense. ill change the origin a little and see the effects. – the Commented Jan 17 at 17:52
  • 1 It worked I changed the {0.125f,0.125f} to {512.f,512.f} and it seems to be working. Thanks again for ur help @JesperJuhl! – the Commented Jan 17 at 18:12
 |  Show 6 more comments

1 Answer 1

Reset to default -1

It worked I changed the {0.125f,0.125f} to {256.f,256.f} and it seems to be working.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351855a4623881.html

相关推荐

  • c++ - Why is sfml entity.setOrigin() not working? - Stack Overflow

    I wrote some SFML code in C++ to set the sprite's origin to its center (0.125f) so that i can rota

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信