SFML community forums

Help => Graphics => Topic started by: kubala on April 11, 2012, 04:32:37 pm

Title: Problem with object location
Post by: kubala on April 11, 2012, 04:32:37 pm
Hi.

The circle(ball) in my programme should be in center of the window, but it's not. I noticed, that rectangle don't go how it should(it goes too far in down). What's wrong?
#include<SFML/Graphics.hpp>
#include<cstdio>
int main()
{
    sf::RenderWindow Window(sf::VideoMode(800,600,32), "Pong"/*, sf::Style::Close*/ );
    sf::Event Event;
    Window.SetFramerateLimit(60);

    sf::Image Image(50,200,sf::Color::White);
    sf::Sprite Sprite(Image);
    Sprite.SetCenter(Image.GetWidth()/2.0f, Image.GetHeight()/2.0f);
    Sprite.SetPosition(25, 100);

    sf::Shape Ball=sf::Shape::Circle(Window.GetWidth()/2.0f, Window.GetHeight()/2.0f,30,sf::Color::Green);
    Ball.SetPosition(Ball.GetPosition().x/2.0f, Ball.GetPosition().y/2.0f);

    while(Window.IsOpened())
    {
        //sf::Vector2f spr=Sprite.TransformToLocal(sf::Vector2f(Ball.GetPosition().x, Ball.GetPosition().y)  );

        Ball.Move(-2,0);
        printf("Ball %f",Ball.GetPosition().x);
        printf("\nSprite%f ",  Sprite.GetPosition().x,"\n\n");
        if(Ball.GetPosition().x<=Sprite.GetPosition().x)Ball.SetPosition(300,300);
        const sf::Input& Input = Window.GetInput();
        bool up = Input.IsKeyDown(sf::Key::Up);
        bool down = Input.IsKeyDown(sf::Key::Down);


        while(Window.GetEvent(Event))
        {
            if(Event.Type==sf::Event::Closed) Window.Close();


        }
        if(down) Sprite.Move(0, 10);
        if(up) Sprite.Move(0,-10);
        if(Sprite.GetPosition().x<=0) Sprite.SetX(0);
        if(Sprite.GetPosition().y<=0) Sprite.SetY(100);
        if(Sprite.GetPosition().y>=Window.GetHeight()) Sprite.SetY(Window.GetHeight()-100);
        Window.Draw(Sprite);
        Window.Draw(Ball);
        Window.Display();
        Window.Clear();

    }

    return 0;
}

Thank's for help.
Title: Re: Problem with object location
Post by: Laurent on April 11, 2012, 04:44:57 pm
The center point that you pass to sf::Shape::Circle is used to define the circle's local points, it has nothing to do with the overall position of the circle that you set with SetPosition.

You should create your circle around point (0, 0) so that SetPosition will then move the ball where you want correctly.
Title: Re: Problem with object location
Post by: kubala on April 11, 2012, 05:23:40 pm
Thank you for help   :).