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

Author Topic: Angle change my position  (Read 1464 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Angle change my position
« on: June 04, 2011, 08:22:53 pm »
I have a problem with SetRotation function, she changes position of my picture and I dont know why, look the video with error

My picture


Source
Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>

typedef sf::Vector2<double> Point;

int main()
{
    sf::RenderWindow App(sf::VideoMode(100, 200, 32), "Eye");

    double radius = 7.5;
    int dir = 0;

    sf::Image imageEye;
    imageEye.LoadFromFile("eye.png");
    imageEye.SetSmooth(false);
    sf::Sprite spriteEye(imageEye);

    Point eye(50,100);

    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        if (App.GetInput().IsKeyDown(sf::Key::Space))
        {
            dir = 1;
        }

        else
        dir = 0;

        if(dir)
        {
            eye.x -= cos(45 * 3.14 / 180) * 0.5;
            eye.y += sin(45 * 3.14 / 180) * 0.5;
        }

        else
        {
            eye.x += cos(45 * 3.14 / 180) * 0.5;
            eye.y += sin(45 * 3.14 / 180) * 0.5;
        }

        App.Clear(sf::Color(64,64,64));

        if(dir)
            spriteEye.SetRotation(45);

        else
            spriteEye.SetRotation(135);

        spriteEye.SetPosition(eye.x,eye.y);
        App.Draw(spriteEye);
       
        App.Display();

        std::cout<<"eye x = "<< eye.x<<std::endl;
        std::cout<<"eye y = "<< eye.y<<std::endl;

        sf::Sleep(0.025f);
    }


    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Angle change my position
« Reply #1 on: June 04, 2011, 08:32:14 pm »
By default the center of rotation is at (0, 0), which is the top-left corner of the sprite. If you want to rotate around the center of your sprite, you must change the center of rotation accordingly (with SetCenter).
Laurent Gomila - SFML developer

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Angle change my position
« Reply #2 on: June 04, 2011, 08:56:15 pm »
Thank you  :)