h4344
h4344

Reputation: 151

C++ How can i find the vector between 2 points in a window?

Im working on a project right now and i need to make a function that finds the vector direction for a bullet. My current code is off and i cant seem to find the reason why.

float AngleX = pMouse->X() - This->DirectionX();
float AngleY = pMouse->Y() - This->DirectionY();

Upvotes: 0

Views: 2201

Answers (1)

Alnitak
Alnitak

Reputation: 339906

The best function for finding angles from (x, y) offsets is atan2(dy, dx), where dy and dx are the delta components in each direction.

Note that the result will be in radians, and that on some graphics systems the y axis goes down instead of up!

The particularly nice feature of atan2 is that it'll always give you the result in the full range of -π .. π which you can't get with a single acos or asin operation. The resulting angle will be the angle of the given line relative to the positive X axis, in an anti-clockwise direction.

Upvotes: 6

Related Questions