GetAngleBetweenLocations
GetAngleBetweenLocations(location, location)
Returns a float representing the angle between Location One and Location Two
float GetAngleBetweenLocations(
location lOne,
location lTwo
);
Parameters
lOne
Location One
lTwo
Location Two
Description
This is a convenience function that returns a float representing the angle between lOne and lTwo. This is done by extracting the positions from lOne and lTwo, then getting the distance between the two locations. Then the absolute change in the x axis is calculated and the result is applied to acos(deltaX / distance). The result is a number whoâs value represents the absolute bearing (where North=0) between the two locations.
Calls
int abs(int iNum)
float acos(float fNum)
int FloatToInt(float fNum)
float IntToFloat(int iNum)
vector GetPositionFromLocation(location loc)
float GetDistanceBetweenLocations(location lone, location lTwo)
Remarks
This method may suffer from round-off error, in the line:
IntToFloat(abs(FloatToInt(vPos1.x - vPos2.x)))
When vPos2.x is subtracted from vPos1.x and there is no guarantee that (vPos1.x > vPos2.x) is true. This author has to wonder about the absence of a fAbs function that returns the absolute value of a float, baring that why not simply write the following check:
float fChangeX = vPos1.x â vPos2.x;
if (fChangeX < 0)
fChangeX = -fChangeX;
as was done in GetChangedPosition. But this is merely this writerâs opinion; the precision error however can be real.
Also remember, this requires the include file x0_i0_position.
If you want to produce a float value between 0.0 and 360.0 degrees that tells you in absolute terms what point of the compass the target object is from the source object, with 0.0 being due north, then see the code snippet in 'Examples', below.
Requirements
#include " x0_i0_position "
Version
1.61
Example
// This is a different function to GetAngleBetweenLocations. The below function produces an absolute float value between 0.0 and 360.0, where 0.0 is north.
//The value produced is the angle/point of the compass of the target location ('lTo') from the source location ('lFrom').
float GetRelativeAngleBetweenLocations(location lFrom, location lTo)
{
vector vPos1 = GetPositionFromLocation(lFrom);
vector vPos2 = GetPositionFromLocation(lTo);
float dx = vPos2.x - vPos1.x;
float dy = vPos2.y - vPos1.y;
// Handle the vertical axis (North/South) to avoid division by zero
if (dy == 0.0f)
{
if (dx >= 0.0f) return 90.0f; // East
return 270.0f; // West
}
// atan(dx / dy) aligns 0 degrees with the positive Y-axis (North)
float fAngle = atan(dx / dy);
if (dy > 0.0f)
{
// Target is North of source (Quadrants 1 or 4)
// atan returns -90 to 90. If negative (West), wrap to 270-360 range.
if (dx < 0.0f)
fAngle += 360.0f;
}
else
{
// Target is South of source (Quadrants 2 or 3)
// Add 180 to shift the -90 to 90 range into 90 to 270.
fAngle += 180.0f;
}
return fAngle;
}
See Also
| functions: |
author: Michael Nork, editor: Jasperre, additional contributor(s): James E. King