4
\$\begingroup\$

I have just implemented an algorithm which calculates the direction an enemy in my game should face when moving to a location.

This is a 2D game where characters can face in one of four directions: (Up, Down, Left, Right).

When navigating to a point, if the new point is further away on the X axis than the Y axis, the enemy should show it's left or right animation. However, if the movement would take the enemy more along the Y axis than the X axis, the enemy should show it's up or down animations.

To put it visually, the center of the X in the following image illustrates the position of my enemy on an coordinate plane. Depending on which quadrant his movement point is placed in, the respective animation for that quadrant should be shown.

Facing Direction Diagram Here is my implementation. My questions are:

  1. Does this algorithm accomplish what I am thinking it does? From watching it on my enemy, it looks good so far, however I could just be getting lucky!
  2. Can this algorithm be improved in any way?

protected FacingDirections CalculateFacingDirection(Vector2 navPos)
{
    Vector2 curPos = transform.position;

    float xDiff = Mathf.Abs(navPos.x - curPos.x);
    float yDiff = Mathf.Abs(navPos.y - curPos.y);

    if(navPos.x > curPos.x) //we are moving left
    {
        if(navPos.y > curPos.y) //we are moving up
        {
            return xDiff > yDiff ? FacingDirections.Left : FacingDirections.Up; //are we moving more left or up?
        }

        return xDiff > yDiff ? FacingDirections.Left : FacingDirections.Down; //are we moving more left or down?
    }
    else //we are moving right
    {
        if (navPos.y > curPos.y) //we are moving up
        {
            return xDiff > yDiff ? FacingDirections.Right : FacingDirections.Up; //are we moving more left or up?
        }

        return xDiff > yDiff ? FacingDirections.Right : FacingDirections.Down; //are we moving more left or down?
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Personally, I would make a Vector2 or Vector2F that you drop the difference between the points into, or the force being applied, and then use a Theta from that to determine the angle the enemy faces. \$\endgroup\$ – Der Kommissar Jul 10 '15 at 12:50
4
\$\begingroup\$

In my opinion you should first get the axis of the maximum direction via comparison of xDiff with yDiff:

protected FacingDirections CalculateFacingDirection(Vector2 navPos)
{
    Vector2 curPos = transform.position;

    float xDiff = Mathf.Abs(navPos.x - curPos.x);
    float yDiff = Mathf.Abs(navPos.y - curPos.y);

    if (xDiff > yDiff) // we are moving more along X axis
    {
        return navPos.x > curPos.x ? FacingDirections.Left : FacingDirections.Right;
    }
    // we are moving more along Y axis
    return navPos.y > curPos.y ? FacingDirections.Up : FacingDirections.Down;
}
\$\endgroup\$
-1
\$\begingroup\$

Additional: remove comments and try to explain your code using variables or code logic:

float xDiff = Mathf.Abs(navPos.x - curPos.x);
float yDiff = Mathf.Abs(navPos.y - curPos.y);
bool movingRight = navPos.x > curPos.x;
bool movingUp = navPos.y > curPos.y;

if(xDiff>yDiff)
{
  return movingRight ? FacingDirections.Left :  FacingDirections.Right;
}
else
{
  return movingUp ? FacingDirections.Up :  FacingDirections.Down;
}
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to CodeReview, Alex. This isn't much of an answer, perhaps more fitting as a comment. \$\endgroup\$ – Legato Jul 10 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.