# Finding if a player is facing the back of another.

This topic is 1774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

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?

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

http://en.wikipedia.org/wiki/Dot_product

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...

Edited by Ravnock

##### Share on other sites
You should be able to do such a check with a couple vector dot products. Take the view vector of each and dot them. If they are both positive the guys are facing a similar direction. Compare the value to the angle tolerance you wish to enforce. Also if you dot the victims forward vector with the vector to the attacker you can discover if the attacker is behind, as it will be negative, and again you can tweak the angle tolerance by comparing it to the angle tolerance you want to allow.

##### Share on other sites

Thanks a lot, I will try and see if it works.