Sign in to follow this  
altra4u

Most efficient way to do front-back test

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this