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

Author Topic: Rotating player depending on the mouse position  (Read 3383 times)

0 Members and 1 Guest are viewing this topic.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Rotating player depending on the mouse position
« on: November 23, 2015, 07:29:01 pm »
So basically I wanna make player1 rotate and always look at the cursor, but I'm stuck. For some reason code below doesn't work

 
click1Pos.x = (int)sf::Mouse::getPosition(cliff->window).x;
        click1Pos.y = (int)sf::Mouse::getPosition(cliff->window).y;
        moveToCursor = true;

        double one = playerYposition - mouseYposition;
        double two = playerX - mouseXposition;
        double tanges = (tan(one/two)* RAD);

        player1.setRotation(tanges);
« Last Edit: November 23, 2015, 07:34:06 pm by paupav »

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Rotating player depending on the mouse position
« Reply #1 on: November 23, 2015, 07:46:47 pm »
Shouldn't that be atan instead of tan? And are you sure you have to operate with angles in radians instead of degrees?

G.

  • Hero Member
  • *****
  • Posts: 1594
    • View Profile
Re: Rotating player depending on the mouse position
« Reply #2 on: November 23, 2015, 08:49:34 pm »
It's atan2, and "sfml rotate mouse position" yields lots of results on google.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Rotating player depending on the mouse position
« Reply #3 on: November 23, 2015, 09:42:10 pm »
Shouldn't that be atan instead of tan? And are you sure you have to operate with angles in radians instead of degrees?

I'm not sure if there is any function that returns angle in degrees, but I0ve definded #define RAD 57.29 and at the end I simply multiply angle in radians with that constant.

It's atan2, and "sfml rotate mouse position" yields lots of results on google.

Idk why it worked, but it didn't work with atan.