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 | Show 6 more comments1 Answer
Reset to default -1It 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
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 to0.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