SFML community forums
Help => General => Topic started 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;
-
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.
-
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?
-
What is "fixing"? That's correct, as I said.
If you need a [0, 360[ range, use std::fmod(x + 360.f).
-
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?
-
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.
-
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)?
-
More like x = std::fmod(x + 360,360)
Yours would yield 0 all the time.
-
It can be simpler in this particular case:
if (x < 0)
x += 360;