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

Author Topic: Negative calculations when using triggernomatry so that an object faces cursor  (Read 3154 times)

0 Members and 1 Guest are viewing this topic.

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Sorry for the long title. Whenever the rotation of the object exceeds 180(degrees) when the rotation is supposed to be 350 its -10, 340 = -20, 330 = -30, and so on.

       const float PI = 3.14159265;

   int mx = sf::Mouse::getPosition(window).x;
   int my = sf::Mouse::getPosition(window).y;

   int tmp_rot = (atan2(my - y, mx - x)) * 180 / PI;

   rot = tmp_rot;

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
This is expected, you should read the documentation of standard library functions ;)

Also, correct C++ is std::atan2, not atan2.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
This is expected, you should read the documentation of standard library functions ;)

Also, correct C++ is std::atan2, not atan2.

gotcha. Ive given a toss at fixing that with no success. Any suggestions for fixing?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
What is "fixing"? That's correct, as I said.

If you need a [0, 360[ range, use std::fmod(x + 360.f).
« Last Edit: September 26, 2015, 05:45:53 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
What is "fixing"? That's correct, as I said.

If you need a [0, 360[ range, use std::fmod(x + 360.f).

Doesn't fmod require 2 arguments? and is x supposed to be the rotation or the x specified in the code?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
True, I forgot the second one. With a bit of thinking, I'm sure you'll find it out yourself :)
x is the value in [-180, 180[ that you want to map to [0, 360[.

By the way, you could have a look at Thor's Vectors module, it really simplifies operations on vectors and angles.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
True, I forgot the second one. With a bit of thinking, I'm sure you'll find it out yourself :)
x is the value in [-180, 180[ that you want to map to [0, 360[.

By the way, you could have a look at Thor's Vectors module, it really simplifies operations on vectors and angles.

Hm, so x = std::fmod(0, 360)?

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
More like x = std::fmod(x + 360,360)
Yours would yield 0 all the time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
It can be simpler in this particular case:

if (x < 0)
    x += 360;
Laurent Gomila - SFML developer