SFML community forums

Help => General => Topic started by: Jim70 on September 26, 2015, 05:22:51 pm

Title: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Jim70 on September 26, 2015, 05:22:51 pm
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;
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Nexus on September 26, 2015, 05:27:58 pm
This is expected, you should read the documentation (http://en.cppreference.com/w/cpp/numeric/math/atan2) of standard library functions ;)

Also, correct C++ is std::atan2, not atan2.
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Jim70 on September 26, 2015, 05:29:41 pm
This is expected, you should read the documentation (http://en.cppreference.com/w/cpp/numeric/math/atan2) 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?
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Nexus on September 26, 2015, 05:35:11 pm
What is "fixing"? That's correct, as I said.

If you need a [0, 360[ range, use std::fmod(x + 360.f).
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Jim70 on September 26, 2015, 05:40:45 pm
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?
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Nexus on September 26, 2015, 05:47:19 pm
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 (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) module, it really simplifies operations on vectors and angles.
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Jim70 on September 26, 2015, 05:50:08 pm
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 (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) module, it really simplifies operations on vectors and angles.

Hm, so x = std::fmod(0, 360)?
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: SpeCter on September 26, 2015, 07:48:20 pm
More like x = std::fmod(x + 360,360)
Yours would yield 0 all the time.
Title: Re: Negative calculations when using triggernomatry so that an object faces cursor
Post by: Laurent on September 26, 2015, 11:08:08 pm
It can be simpler in this particular case:

if (x < 0)
    x += 360;