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

Author Topic: Moving to a specific point on the window does not seem to work correctly  (Read 1360 times)

0 Members and 1 Guest are viewing this topic.

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Hey everyone!

I am having an issue getting the object to move to a point on the map. I have dealt with this before  but for some reason I am unable to get this to work correctly using a very simple codebase.

Here is the code:

//in main
object.setPosition(25, 600);
cordinates.x = 250;
cordinates.y = 0;


//function that is called
    if(object.object.getPosition().x == cordinates.x && object.object.getPosition().y == cordinates.y)
    {
        return true;
    }
    else
    {
        sf::Vector2f _tempPos = object.getPosition();
        sf::Vector2f _targetPos = cordinates;
        float dx = _targetPos.x - _tempPos.x;
        float dy = _targetPos.y - _tempPos.y;
        float angle = atan2(dx, dy);
        float directionX = cos(angle);
        float directionY = sin(angle);
        object.move({directionX, directionY});
    }
 

As the character moves he moves down and to the right instead of up and to the right but from what I have read in examples and etc. I believe to have the write code...but obviously I do not.

Thanks for the help guys

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile


Notice the y axis pointing up. In SFML the y axis is pointing down, so it's actually -sin(angle)