-
I have a circle sprite which I try to rotate it accordingly for my game, but I dont seem to understand how SMFL rotation works . I even set the origin of the sprite in the center but always the positions changes when I rotate..
-
Could you please post a compilable and minimal example of your code.
-
so I have a head of a snake which can pose in 4 different states, with the face up, down, left and right , what I wanted to do is make use of rotations to rotate the head so that it faces the desired directions without changing the position of the head on the screen
I have made a workaround using texture rectangle...
if (mIsMovingUp)
{
movement.y -= mPlayerSpeed;
//how ?
}
if (mIsMovingDown)
{
movement.y += mPlayerSpeed;
//how ?
}
if (mIsMovingLeft)
{
movement.x -= mPlayerSpeed;
mPlayer[0].setTextureRect(sf::IntRect(64, 0, -32, 32));
//sprite face left
}
if (mIsMovingRight)
{
movement.x += mPlayerSpeed;
mPlayer[0].setTextureRect(sf::IntRect(32, 0, 32, 32));
//sprite face right
}
-
But can you please post the code that didn't work such that we can attempt to see what the problem was.
-
I just gave it ...
just give me an example on how to rotate a circle or rectangle on its standing position like this, the rotate function rotates it in a weird way, i would be glad if someone explains me how rotate() really works, but first this problem :
http://imgur.com/4qnse4t
-
The code you gave is your workaround. It does not at any point use the rotate() function and hence we cannot see what you were doing wrong with the rotate() function. Also, from the image you've given, it appears to be working.
-
check this code :
#include <SFML/Graphics.hpp>
int main()
{
sf::CircleShape s;
s.setFillColor(sf::Color::Red);
s.setRadius(100.f);
s.setPosition(700.f, 350.f);
sf::RenderWindow win(sf::VideoMode(1600, 900), "test");
while (win.isOpen())
{
sf::Event e;
while (win.pollEvent(e))
{
if (e.type == sf::Event::Closed)
win.close();
}
win.clear();
s.rotate(1); //rotate 1 degree each iteration
win.draw(s);
win.display();
sf::sleep(sf::seconds(0.01));
}
return 0;
}
the circle is indeed rotating but its initial position changes aswell, the question is how do i rotate the circle so the position wont change
-
Put
s.setCenter(100,100)
Also, I'd advise putting the rotate function before the clear function so that you have just clear, draw, display, without anything in between.
-
thanks it works , mind explaining a little how does rotate() work?
-
Do you mean how it works internally, or how you use it, because I'd be much more adept at answering the latter. Every transformable object has a centerpoint, which is by default the top left. Rotate takes an amount of degrees, by which to rotate it, note rotate, not set the total rotation to. It then rotates the corners around that point by the number of degrees given.
-
Put s.setCenter(100,100)
thanks it works
I'm not sure how. sf::CircleShape doesn't have a setCenter() method :P
As you probably discovered, the method to use is: setOrigin()
For some re-usable code to set any circle's origin to its centre, you can do this:
circle.setOrigin(circle.getRadius(), circle.getRadius());
-
My bad, forgot to check the documentation.