Use WAngle.ArcTan in Traits.Util.GetFacing.

This commit is contained in:
Paul Chote
2013-07-22 22:11:58 +12:00
parent e08445f762
commit b50ba0bfb9
2 changed files with 11 additions and 65 deletions

View File

@@ -67,7 +67,8 @@ namespace OpenRA
return new WAngle(Angle - 512).Tan(); return new WAngle(Angle - 512).Tan();
} }
public static WAngle ArcTan(int y, int x) public static WAngle ArcTan(int y, int x) { return ArcTan(y, x, 1); }
public static WAngle ArcTan(int y, int x, int stride)
{ {
if (y == 0) if (y == 0)
return new WAngle(x >= 0 ? 0 : 512); return new WAngle(x >= 0 ? 0 : 512);
@@ -81,7 +82,7 @@ namespace OpenRA
// Find the closest angle that satisfies y = x*tan(theta) // Find the closest angle that satisfies y = x*tan(theta)
var bestVal = int.MaxValue; var bestVal = int.MaxValue;
var bestAngle = 0; var bestAngle = 0;
for (var i = 0; i < 256; i++) for (var i = 0; i < 256; i+= stride)
{ {
var val = Math.Abs(1024*ay - ax*TanTable[i]); var val = Math.Abs(1024*ay - ax*TanTable[i]);
if (val < bestVal) if (val < bestVal)

View File

@@ -32,38 +32,19 @@ namespace OpenRA.Traits
public static int GetFacing(WVec d, int currentFacing) public static int GetFacing(WVec d, int currentFacing)
{ {
return GetFacing(new int2(d.X, d.Y), currentFacing); if (d.LengthSquared == 0)
} return currentFacing;
public static int GetFacing(PVecInt d, int currentFacing) // OpenRA defines north as -y, so invert
{ var angle = WAngle.ArcTan(-d.Y, d.X, 4).Angle;
return GetFacing(d.ToInt2(), currentFacing);
// Convert back to a facing
return (angle / 4 - 0x40) & 0xFF;
} }
public static int GetFacing(CVec d, int currentFacing) public static int GetFacing(CVec d, int currentFacing)
{ {
return GetFacing(d.ToInt2(), currentFacing); return GetFacing(d.ToWVec(), currentFacing);
}
public static int GetFacing(int2 d, int currentFacing)
{
if (d == int2.Zero)
return currentFacing;
int highest = -1;
int highestDot = -1;
for( int i = 0 ; i < fvecs.Length ; i++ )
{
int dot = int2.Dot( fvecs[ i ], d );
if( dot > highestDot )
{
highestDot = dot;
highest = i;
}
}
return highest * 8;
} }
public static int GetNearestFacing( int facing, int desiredFacing ) public static int GetNearestFacing( int facing, int desiredFacing )
@@ -161,41 +142,5 @@ namespace OpenRA.Traits
return Util.ExpandFootprint(cells, true); return Util.ExpandFootprint(cells, true);
} }
static int2[] fvecs =
{
new int2( 0, -1331 ),
new int2( -199, -1305 ),
new int2( -391, -1229 ),
new int2( -568, -1106 ),
new int2( -724, -941 ),
new int2( -851, -739 ),
new int2( -946, -509 ),
new int2( -1004, -259 ),
new int2( -1024, 0 ),
new int2( -1004, 259 ),
new int2( -946, 509 ),
new int2( -851, 739 ),
new int2( -724, 941 ),
new int2( -568, 1106 ),
new int2( -391, 1229 ),
new int2( -199, 1305 ),
new int2( 0, 1331 ),
new int2( 199, 1305 ),
new int2( 391, 1229 ),
new int2( 568, 1106 ),
new int2( 724, 941 ),
new int2( 851, 739 ),
new int2( 946, 509 ),
new int2( 1004, 259 ),
new int2( 1024, 0 ),
new int2( 1004, -259 ),
new int2( 946, -509 ),
new int2( 851, -739 ),
new int2( 724, -941 ),
new int2( 568, -1106 ),
new int2( 391, -1229 ),
new int2( 199, -1305 )
};
} }
} }