Detect the side of an object respect to another

Is there an efficient way to detect which side an object is with respect to another (front, side or back)? I have an object that moves by pathfinding and the actions that are executed depend on the angle of that object with respect to the character. I divide it into 4 sides (front, one side, other side and back). I have tried in many ways but I do not have the expected results. I have close results but they don’t always work correctly. I’ve tried different conditions like the following (the names are in red now because I put generic names to make it clear).

This is the representation of how I was trying to detect if the object is looking to one side with respect to the character, but in a similar way I did it for the 4 sides according to a range of angles.
I also tried to detect the difference in angles between objects.


I also tried using the angle of movement of the object in pathfinding. In turn, I tried reversing the order of “object” and “character” in the conditions.
On the different sides I consider some with “greater or equal to” or “less than or equal to” to cover the entire range of angles.

You could try subtracting the two angles and then use that value as a condition. I created a basic test program to illustrate this


pretend the red object is the player and the black object is an enemy, both use top down movement behavior but player is controlled by wasd, and the enemy is controlled by arrow keys.

when the enemy is facing away from the player (facing the same direction) then the difference of angles is 0, thus if the difference is 0 the enemy is facing away from the player, relative to the player.
image
if the enemy is facing the player then the angle to of enemy relative towards player is 180, thus when the difference is 180 we know that the enemy is facing the player. Using this system we can define all 8 directions that the player and enemy can be facing relative to each other (utilizing negative angles)
image

image
You’ll no doubt have to make some tweaks to this system but I think it will help.

Keep in mind that angle and rotation are not the same thing as far as degrees. One is -180 to 180, and one is 0 to 360. (I never remember which is which)

Also keep in mind 0 is always east/facing right. So if it’s -180 to 180, you have to deal with it jumping from 180 to -180 if you’re on the left.

Yes very true! It does get quite convoluted so this may not be the best method to use. I used a system similar to this not too long ago but it certainly was not intuitive. I’ll see if I can figure out a better implementation.

Thanks for the help. I am studying the values ​​of the angles to find some mathematical solution. Now that I’m converting the angles and angle difference to text to see the values, I realize they’re not what I thought they were.

I have been able to solve it in a not very intuitive and simple way but at least for now it has been effective for me. I divided the range of angles into four quadrants delimited in a similar way as I had done before (front, left side, right side, and back). According to the angle of the object, a quadrant is assigned depending on the range of angles it is in, and I stored it in a variable (from 1 to 4 depending on the side). I did this for both (object and character). Then, under conditions, I make comparisons between the object variable and the character variable to execute actions based on which quadrant each is looking at.
I won’t mark it as solved for now. If anyone knows a simpler and more effective way, it will be welcome.