# Need help testing vector separation.

1. Jun 24, 2010

### marklee42

I'm writing code to check if polygons intersect, but I've ran into a problem.

I have two "vectors." Each has an origin and x and y components. They are facing towards each other. I need to write code that determines if there's a perpendicular axis that separates them. Here's a picture of what I mean:

http://upurs.us/image/15257.jpeg

I'm out of ideas. Any help would be greatly appreciated.

2. Jun 24, 2010

### CRGreathouse

Are the vectors perpendicular? Your question suggests that they are but your coordinates do not make that clear. (In particular, if the labels and my math are correct, they are perpendicular iff y=a.)

3. Jun 24, 2010

### Bill Simpson

Hint: If you constructed an infinite line that was perpendicular to the first vector and went through (x+a,y+a) of your first vector and then you ignore your first vector... now can you think of something useful to do to make progress towards your solution? See if you can figure it out from that hint before someone gives the answer away.

4. Jun 24, 2010

### marklee42

My diagram was ambiguous on the point labels, here's what I meant:

http://upurs.us/image/15271.jpeg

I also drew the line I think Bill Simpson was referring to. My guess is that I just need to check these lines for intersection. If so, I have further questions:

a) http://en.wikipedia.org/wiki/Line_segment_intersection" [Broken] is a bit out of my scope, and I'm not sure how this would work in code. In particular, I can't tell the computer to use substitution. Is there an equation that works given the endpoints?

b) The line can't be infinite length, but since I'm testing collision, it would work fine with an arbitrary value larger than the polygon.

Last edited by a moderator: May 4, 2017
5. Jun 24, 2010

### Karlx

As Bill Simpson told you, you construct the line that is perpendicular to the first vector and that goes through (x+a,y+b).
After that, you can forget your first vector and concentrate on to calculate the distance between this line and the origin of your second vector.
If this distance is lower than the length of the second vector, then it is possible to construct a separating axis (in fact, infinite of them).
If the distance is greater or equal than the length of the second vector, then it is not possible to construct a separating axis.

6. Jun 24, 2010

### Karlx

Following your diagram, you must calculate the distance between the green line (L) and the point (x2,y2).
The green line is a separating axis iff

distance [L,(x2,y2)] < sqrt[(x2-a2)^2+(y2-b2)^2]

7. Jun 24, 2010

### marklee42

Testing for distance and comparing to the length of the second vector makes sense. Turns out there's already a method that takes care of calculating the distance, so I shouldn't have any problems.

Thanks for the help!