Skip to main content
GameDev.net gamedev.net
🔒 Locked

Most efficient way to do front-back test

Started by altra4u Apr 23, 2012 at 2:35 AM 1 replies 1.2k views
Original Post
altra4u
altra4u
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.
Bacterius
Bacterius
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.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
altra4u
altra4u
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.