[Gambas-user] gb3: 2D ray-casting algorithm

Kevin Fishburne kevinfishburne at ...1887...
Sun Aug 12 08:27:03 CEST 2012


I need to know if a point lies within a three-sided polygon (triangle) 
on a 2D plane for combat hit detection in my game. Raw speed, not 
mathematical purity is extremely important. I've read this:

http://en.wikipedia.org/wiki/Point_in_polygon

and many more articles and posts and have come to the conclusion that my 
brain is not wired to solve this sort of problem independently in a 
reasonable time frame. I somewhat understand the concept, but not the 
mathematical implementation.

 From what I understand, you take the point and create a ray from it (a 
line with a beginning but no end), then do an intersection calculation 
between the ray and each line segment of the triangle. If the number of 
intersections is odd then the point is inside the triangle. If the 
number of intersections is even then the point is outside the triangle. 
So I guess what I'm asking is how do you define a ray (even a "fake" one 
like a line segment with one point REALLY far away for speed) and how do 
you calculate if a ray intersects a line segment? Stuff like this 
humbles me because I know it must be simple mathematically but I'm just 
too damn stupid to grok it. Thanks, as always.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list