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

Author Topic: Problem with rotation  (Read 4198 times)

0 Members and 1 Guest are viewing this topic.

leryss

  • Newbie
  • *
  • Posts: 5
    • View Profile
Problem with rotation
« on: December 29, 2015, 08:35:12 pm »
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..

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #1 on: December 29, 2015, 09:22:00 pm »
Could you please post a compilable and minimal example of your code.

leryss

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with rotation
« Reply #2 on: December 29, 2015, 09:49:05 pm »
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
        }

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #3 on: December 29, 2015, 10:00:08 pm »
But can you please post the code that didn't work such that we can attempt to see what the problem was.

leryss

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with rotation
« Reply #4 on: December 29, 2015, 10:11:47 pm »
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

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #5 on: December 29, 2015, 10:24:07 pm »
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.

leryss

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with rotation
« Reply #6 on: December 29, 2015, 10:30:25 pm »
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

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #7 on: December 29, 2015, 10:33:28 pm »
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.

leryss

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with rotation
« Reply #8 on: December 29, 2015, 10:35:51 pm »
thanks it works , mind explaining a little how does rotate() work?

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #9 on: December 29, 2015, 10:38:35 pm »
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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with rotation
« Reply #10 on: December 30, 2015, 12:35:21 am »
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());
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with rotation
« Reply #11 on: December 30, 2015, 10:07:51 am »
My bad, forgot to check the documentation.