Need help testing vector separation.

AI Thread Summary
The discussion centers on coding a solution to determine if two vectors representing polygons intersect. The user seeks to find a perpendicular axis that can separate the two vectors, which are facing each other. A key suggestion involves constructing a line perpendicular to the first vector and checking its distance from the origin of the second vector. If this distance is less than the length of the second vector, a separating axis exists; otherwise, it does not. The user has found a method to calculate the necessary distance, indicating progress in solving the problem.
marklee42
Messages
3
Reaction score
0
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.
 
Mathematics news on Phys.org
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.)
 
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.
 
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" 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:
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.
I hope this can help you.
 
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]
 
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!
 
Back
Top