Hello there guys, I am creating a game based on arena matches, with RPG style fights. I want some abilities to be more poweful (or only usable) when you are facing the enemy's back.
Assuming I have two entities (player or AI controlled pet) and that I know their positions and looking angles, is there any direct way to find the if one is facing the back of the other?
Right now I am using a quadrant approach, for instance If I had two entities A and B and I wanted to know if A is on B's back.
1) I define which quadrant B is relatively to A (assuming A's position as the origin).
2) I add and subtact 45º from A's looking angle and find the quadrants of those resulting angles (I assume the front is 90º wide).
3) If B is on one of those quadrants and its looking angle is in the interval defined in the former step, then A is on B's back.
But I am wondering if there is a better approach?
Thanks in advance.
Using the dot product between the two front vectors (FrontA and FrontB) you are able to know if they are looking in the same direction, and it's quite fast
if dot(FrontA,FrontB) > 0 then the angle is between PI/2 and -PI/2 (A is looking in the same direction than B)
else if (dot(FrontA,FrontB) < 0) they are looking in the opposite direction
After that you should check the vector distance between B and A to know if A is behind B
dot( Bpos-Apos, FrontA) > 0 then A is behind B
If the signs are ok it should work...