Yes, it's this question again. The reason I'm asking, despite the fact that there are 100 answers to be found on Google, is that they all revolve around atan2(). While atan2() is an awesome function, it's also apparently ineffective under certain circumstances.
All of this will be explained. This is my current code:
float angleBetweenVectors(sf::Vector2f a, sf::Vector2f b)
{
return 57.2957795f * atan2(b.y - a.y, b.x - a.x);
}
I take this, use it to rotate a missile sprite toward the mouse cursor, and then give it forward velocity. The problem is that the rotation is bit off for any angle not divisible by 45. In order to further prove my point, I've constructed a console program which should return a rotation of 22.5 degrees, because the ratio of x:y is 1:2. This gives me incorrect output. The code:
#include <iostream>
#include <math.h>
using namespace std;
int main(void)
{
// the parameters to atan2() are precalculated distances,
// so it should work
cout << "The rotation is: " << (57.2957795f * atan2(5,10)) << \
", but it shouldn't be.\n";
return 0;
}
And the output is: "The rotation is: 26.5651, but it shouldn't be." So either I'm misinterpreting how to use of atan2(), or it's just wrong. The assumption I'm making is that I'm misinterpreting/misusing it. Anybody know how to do this properly?