This question already has an answer here:
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) s_height = int(start) e_width = int(end) e_height = int(end) # 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?