Jump to content
  • Advertisement
Sign in to follow this  
Jamiernmd

Directional vector question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I was just wondering how to go about determining what side a particular object is in regards to a 2d directional vector. (I.e. is it to the left or right?)

Thanks

Share this post


Link to post
Share on other sites
Advertisement
My favorite solution uses complex numbers: If your object is at z and your direction vector is w, you can compute z*conj(w) and look at the sign of the imaginary part.

R. Sedgewick refers to a function that does something similar as "CCW" (for "CounterClockWise"). Given three points, it tells you which way the turn they describe goes. You can search for it.

Share this post


Link to post
Share on other sites
You can do this very simply with a perp-dot product.

Given the object to be queried at 'b' and the object where you are standing 'a' and direction 'd', you compute

(b-a) perpdot d

and check the sign of the value.

perpdot between two vectors (x1,y1) and (x2,y2) is x1*y2 - y1*x2

Share this post


Link to post
Share on other sites
Aloha,

Let’s say your line is defined by the points A and B. In order to find out whether a point C is on the left or right you can look at the sign of the following determinant:
[Formula] \begin{vmatrix} B.x-A.x & C.x-A.x\\ B.y-A.y & C.y-A.y \end{vmatrix} [/Formula]
If positive C is left, if negative it is right, if zero all three a collinear.

Or in code:
bool isLeft(Vector2 A, Vector2 B, Vector2 C){
return ((B.x - A.x)*(C.y - A.y) - (B.y - A.y)*(C.x - A.x)) > 0;
}


That’s a very cheap test actually and it is quite common in computational geometry.
Hope that helps. smile.png

Edit: Too slow. smile.png That's exactly what luca-deltodesco just said. You beat me to it, buddy. smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!