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

Author Topic: How to get the position of a Shape within a window !?  (Read 6486 times)

0 Members and 4 Guests are viewing this topic.

Ubiquité

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to get the position of a Shape within a window !?
« on: July 02, 2011, 02:28:06 pm »
Hi,
I am making a pong and the ball an the players are simple shapes (rectangles). I move them with Shape::Move but when I use Shape::GetPosition, I expected to get the position of the shape relatives to the origine of the window but it appears to be relatives to the original position of the shape.
Could you help me?

Haikarainen

  • Guest
How to get the position of a Shape within a window !?
« Reply #1 on: July 02, 2011, 02:43:28 pm »
Do this;

Create a new class, called cPaddle or something, give it a "sf::Vector2f Position", and on its drawmethod, just make it draw a shape on its position!

Ubiquité

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to get the position of a Shape within a window !?
« Reply #2 on: July 02, 2011, 04:36:23 pm »
Thank you but is it normal if GetPosition behave so strangely ?
Currently my code looks like that :
Code: [Select]

//Pong
int main(void)
{
  const float PlayerSpeed = 2200;
  sf::Vector2<float> ballMove(-500.,00);
 

  sf::RenderWindow app(sf::VideoMode(800,600,32), "My Pong");
  sf::Shape player1 = sf::Shape::Rectangle(30,250,40,350,sf::Color::White);
  sf::Shape player2 = sf::Shape::Rectangle(800-40,250,800-30,350,sf::Color::White);
  sf::Shape ball = sf::Shape::Rectangle(800-405,295,800-395,305,sf::Color::White);

  // ball.SetCenter(5,5);

  const sf::Input& in = app.GetInput();

  sf::Clock clock;

  while(app.IsOpened())
    {
      sf::Event ev;
      while(app.GetEvent(ev))
        {
          if(ev.Type == sf::Event::Closed)
            app.Close();
        }
      app.Clear();

      //Move player1 (a & q)
      if(in.IsKeyDown(sf::Key::A))
        player1.Move(0,-PlayerSpeed*clock.GetElapsedTime());
     
      if(in.IsKeyDown(sf::Key::Q))
        player1.Move(0,PlayerSpeed*clock.GetElapsedTime());
     
      //Move player2 (p & m)
      if(in.IsKeyDown(sf::Key::P))
        player2.Move(0,-PlayerSpeed*clock.GetElapsedTime());
             
      if(in.IsKeyDown(sf::Key::M))
        player2.Move(0,PlayerSpeed*clock.GetElapsedTime());
             
      //Move the ball
     
      //test collisions between ball and frame borders
      //sf::Vector2<float> c = ball.GetPosition();
     
      //std::cerr << c.x << std::endl;
      //std::cerr << c.y << std::endl;
      std::cerr << ball.GetPointPosition(0).y << std::endl;

      if(ball.GetPointPosition(0).x <= 0. && ballMove.x < 0.)
         ballMove.x -= ballMove.x;
       if(ball.GetPointPosition(0).y <= 0. && ballMove.y < 0.)
         ballMove.y -= ballMove.y;

      ball.Move(ballMove.x*clock.GetElapsedTime(),ballMove.y*clock.GetElapsedTime());

      //Draw everything
      app.Draw(player1);
      app.Draw(player2);
      app.Draw(ball);
     
      app.Display();
      clock.Reset();
     
    }
}


The std::cerr always display the same value.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How to get the position of a Shape within a window !?
« Reply #3 on: July 02, 2011, 04:51:43 pm »
The behavior is not strange. You have to differentiate between the following coordinate systems:
  • Global coordinates of the shape's origin. They can be accessed through SetPosition(), Move() and GetPosition() and determine the shape's coordinates relative to the "world" (in the default view, this is the left-upper window corner).
  • Local coordinates of the shape's points relative to its origin. These are the coordinates you specify when you call the static factory functions Circle(), Rectangle() and Line(). After construction, the methods SetPointPosition() and GetPointPosition() can be used to access these coordinates.
You can compute one coordinate from another by calling TransformToGlobal() and TransformToLocal(), respectively.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ubiquité

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to get the position of a Shape within a window !?
« Reply #4 on: July 02, 2011, 04:54:23 pm »
Ok thank you. I think it worth mentioning in the tutorials.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How to get the position of a Shape within a window !?
« Reply #5 on: July 02, 2011, 05:00:55 pm »
Sometimes it pays to take a look at the documentation ;)
Quote
Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ubiquité

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to get the position of a Shape within a window !?
« Reply #6 on: July 02, 2011, 05:43:12 pm »
My  bad. It is my first SFML program ^^ (and I appreciate the "real C++" way)