Core/Position: Refactor GetAngle -> GetAbsoluteAngle because code clarity is good.

Port From (https://github.com/TrinityCore/TrinityCore/commit/bc89e1cdb0da10e53cc9fa4a97565c05bb4c052e)
This commit is contained in:
hondacrx
2021-09-23 22:46:22 -04:00
parent 0a6d3a9342
commit 4a61a40fb8
17 changed files with 57 additions and 64 deletions
+22 -13
View File
@@ -107,13 +107,19 @@ namespace Game.Entities
return GridDefines.IsValidMapCoord(posX, posY, posZ, Orientation);
}
float ToRelativeAngle(float absAngle)
{
return NormalizeOrientation(absAngle - Orientation);
}
public float GetRelativeAngle(Position pos)
{
return GetAngle(pos) - Orientation;
return ToRelativeAngle(GetAbsoluteAngle(pos));
}
public float GetRelativeAngle(float x, float y)
{
return GetAngle(x, y) - Orientation;
return ToRelativeAngle(GetAbsoluteAngle(x, y));
}
public void GetPosition(out float x, out float y)
@@ -214,7 +220,7 @@ namespace Game.Entities
return dx * dx + dy * dy;
}
public float GetAngle(float x, float y)
public float GetAbsoluteAngle(float x, float y)
{
float dx = x - GetPositionX();
float dy = y - GetPositionY();
@@ -223,18 +229,24 @@ namespace Game.Entities
ang = ang >= 0 ? ang : 2 * MathFunctions.PI + ang;
return ang;
}
public float GetAngle(Position pos)
public float GetAbsoluteAngle(Position pos)
{
if (pos == null)
return 0;
return GetAngle(pos.GetPositionX(), pos.GetPositionY());
return GetAbsoluteAngle(pos.GetPositionX(), pos.GetPositionY());
}
float ToAbsoluteAngle(float relAngle)
{
return NormalizeOrientation(relAngle + Orientation);
}
public bool IsInDist(float x, float y, float z, float dist)
{
return GetExactDistSq(x, y, z) < dist * dist;
}
public bool IsInDist(Position pos, float dist)
{
return GetExactDistSq(pos) < dist * dist;
@@ -286,7 +298,7 @@ namespace Game.Entities
return IsInDist2d(center, radius) && Math.Abs(verticalDelta) <= height;
}
public bool HasInArc(float arc, Position obj, float border = 2.0f, float? orientation = null)
public bool HasInArc(float arc, Position obj, float border = 2.0f)
{
// always have self in arc
if (obj == this)
@@ -295,11 +307,8 @@ namespace Game.Entities
// move arc to range 0.. 2*pi
arc = NormalizeOrientation(arc);
float angle = GetAngle(obj);
angle -= orientation.HasValue ? orientation.Value : GetOrientation();
// move angle to range -pi ... +pi
angle = NormalizeOrientation(angle);
float angle = GetRelativeAngle(obj);
if (angle > MathFunctions.PI)
angle -= 2.0f * MathFunctions.PI;
@@ -308,13 +317,13 @@ namespace Game.Entities
return ((angle >= lborder) && (angle <= rborder));
}
public bool HasInLine(Position pos, float objSize, float width, float? orientation = null)
public bool HasInLine(Position pos, float objSize, float width)
{
if (!HasInArc(MathFunctions.PI, pos, 2.0f, orientation))
if (!HasInArc(MathFunctions.PI, pos, 2.0f))
return false;
width += objSize;
float angle = GetAngle(pos) - (orientation.HasValue ? orientation.Value : GetOrientation());
float angle = GetRelativeAngle(pos);
return Math.Abs(Math.Sin(angle)) * GetExactDist2d(pos.GetPositionX(), pos.GetPositionY()) < width;
}
+7 -7
View File
@@ -2973,7 +2973,7 @@ namespace Game.Entities
Vector3 vObj = new(dest.GetPositionX(), dest.GetPositionY(), dest.GetPositionZ());
Vector3 contactPoint = vThis + (vObj - vThis).directionOrZero() * Math.Min(dest.GetExactDist(GetPosition()), GetCombatReach());
return new Position(contactPoint.X, contactPoint.Y, contactPoint.Z, GetAngle(contactPoint.X, contactPoint.Y));
return new Position(contactPoint.X, contactPoint.Y, contactPoint.Z, GetAbsoluteAngle(contactPoint.X, contactPoint.Y));
}
void GetHitSpherePointFor(Position dest, out float x, out float y, out float z)
@@ -3047,7 +3047,7 @@ namespace Game.Entities
if (size == 0)
size = GetCombatReach() / 2;
float angle = pos1.GetAngle(pos2);
float angle = pos1.GetAbsoluteAngle(pos2);
// not using sqrt() for performance
return (size * size) >= GetExactDist2dSq(pos1.GetPositionX() + (float)Math.Cos(angle) * dist, pos1.GetPositionY() + (float)Math.Sin(angle) * dist);
@@ -3159,7 +3159,7 @@ namespace Game.Entities
{
GetNearPoint2D(out x, out y, distance2d + searcher_size, absAngle);
z = GetPositionZ();
UpdateAllowedPositionZ(x, y, ref z);
(searcher ?? this).UpdateAllowedPositionZ(x, y, ref z);
// if detection disabled, return first point
if (!WorldConfig.GetBoolValue(WorldCfg.DetectPosCollision))
@@ -3179,7 +3179,7 @@ namespace Game.Entities
{
GetNearPoint2D(out x, out y, distance2d + searcher_size, absAngle + angle);
z = GetPositionZ();
UpdateAllowedPositionZ(x, y, ref z);
(searcher ?? this).UpdateAllowedPositionZ(x, y, ref z);
if (IsWithinLOS(x, y, z))
return;
}
@@ -3190,10 +3190,10 @@ namespace Game.Entities
z = first_z;
}
public void GetClosePoint(out float x, out float y, out float z, float size, float distance2d = 0, float angle = 0)
public void GetClosePoint(out float x, out float y, out float z, float size, float distance2d = 0, float relAngle = 0)
{
// angle calculated from current orientation
GetNearPoint(null, out x, out y, out z, size, distance2d, GetOrientation() + angle);
GetNearPoint(null, out x, out y, out z, size, distance2d, GetOrientation() + relAngle);
}
public Position GetNearPosition(float dist, float angle)
@@ -3220,7 +3220,7 @@ namespace Game.Entities
public void GetContactPoint(WorldObject obj, out float x, out float y, out float z, float distance2d = 0.5f)
{
// angle to face `obj` to `this` using distance includes size of `obj`
GetNearPoint(obj, out x, out y, out z, obj.GetCombatReach(), distance2d, GetAngle(obj));
GetNearPoint(obj, out x, out y, out z, obj.GetCombatReach(), distance2d, GetAbsoluteAngle(obj));
}
public void MovePosition(Position pos, float dist, float angle)