Need help testing vector separation.

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 3K views
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.
 
Physics news on Phys.org
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!