# Most efficient way to do front-back test

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

## Recommended Posts

Hi Guys
I was wondering what would be the least expensive way of saying here is my character facing this direction and I want to return a boolean for other characters if they are within a 180 degree curve in front of me.

I can think of doing a dot product between my front vector, V1, and vector from target to me, V2, but that will involve a doing a square root operation in finding the length of V2 so I was thinking what others have to say.

Thoughts welcome.
Thanks.

##### Share on other sites
Your method is the correct way of doing it, but you don't need to square root anything, because you are only interested in the sign of the dot product's result. So the vectors don't have to be of unit length. So if your front vector is V1, and the vector from the target to the main character is V2, you just have:

dot(V1, V2) > 0 -> the target is within a 180 degree curve of the main character in the direction he's facing
dot(V1, V2) < 0 -> the target is "behind" the main character
dot(V1, V2) = 0 -> this one is a special case where the target is "in the corner of eye" of the main character, so how you define it is up to convention (it doesn't matter)

This calculation only requires a vector subtraction, a dot product (= 2-3 scalar multiplications and 1-2 scalar additions depending on which dimension you are using) and a scalar comparison, per character to test.

##### Share on other sites
oh yea. the projection of one vector onto another should be good enough. Lol, y didnt I think of that. Thats y we have the forums i guess.
Thank u sir Bacterius.

1. 1
2. 2
3. 3
Rutin
20
4. 4
frob
18
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632561
• Total Posts
3007088

×