Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to determine which sprite to use when one agent is "facing" another agent.

My game is 2d, and uses 8-directional movement. Deciding which sprite to use for movement is easy enough since there are only 8 options when moving form one square to another.

def direction(self, start, end):
    # method for determining facing direction
    s_width = int(start[0])
    s_height = int(start[1])
    e_width = int(end[0])
    e_height = int(end[1])
    # check directions
    if s_height < e_height and s_width == e_width:
        return 'down'
    elif s_height < e_height and s_width > e_width:
        return 'downright'
    elif s_height == e_height and s_width > e_width:
        return 'left'
    elif s_height > e_height and s_width > e_width:
        return 'upleft'     
    elif s_height > e_height and s_width == e_width:
        return 'up'
    elif s_height > e_height and s_width < e_width:
        return 'upright'
    elif s_height == e_height and s_width < e_width:
        return 'right'
    elif s_height < e_height and s_width < e_width:
        return 'downleft'

It's verbose, but gets the job done. start is the agents current coordinate position, and as you might guess end is the next step in the list of waypoints which makes up an agents path. The problem is that using the same function for determining which sprite should be used when attacking another agent does not look very good at all.

For example, if the target is down, but only one pixel to the right, we would want to show the 'down' sprite, but this function returns 'downright' instead. How can we change it to appropriately return the fitting sprite?

share|improve this question
    
I'm not 100% sure, but to me it seems like I'm asking for something different than what you linked. –  user11177 May 10 '13 at 5:37
    
If you really think so. The answers in the question I linked show both how to get the angle and how to match it to the nearest direction. The answers I the question you linked I only show how to get the angle. –  Byte56 May 10 '13 at 14:28
add comment

marked as duplicate by Byte56, msell, Anko, Josh Petrie, bummzack May 10 '13 at 22:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

You can test the distance between self and target to determine what sprite to use. If the distance is greater than a set MAX, then it looking to left/right otherwise it is just looking down. For example,

let self = (sx, sy) target = (tx, ty)

if (sy < ty)
    distx = abs(sx - tx);
    if (distx <= MAX_X OR sx == tx)
        return  "down"
    else 
        if (sx < tx) then return "downright" else "downleft"
    endif
else // handle up

Notes:

  1. I am using pseudocode not any specific language
  2. I used x/y pair instead of width/height since the former refers to coordinates not dimension like the latter.
share|improve this answer
add comment

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