2D triangle intersection?

Miscellaneous Forums/General Discussion/2D triangle intersection?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's some code from my PolyModule (BMAX).

Poly.ColVertex is a polygon collision vertice. My complex 2D poly objects have simple collision polygons.
Rem
bbdoc: Check to see if bounding boxes overlap
returns: True for Overlap
about: 
	 <table>
		<tr><td><b>poly:iPoly</td><td>Polygon to check against</td></tr>
	</table>
End Rem		
		Method BoundingOverlap(poly:iPoly)
			Local tx1# = Self.x + Self.iMinX
			Local ty1# = Self.y + Self.iMinY
			Local tx2# = Self.x + Self.iMaxX
			Local ty2# = Self.y + Self.iMaxY
			Local tx3# = Poly.x + poly.iMinX
			Local ty3# = Poly.y + poly.iMinY
			Local tx4# = Poly.x + poly.iMaxX
			Local ty4# = Poly.y + poly.iMaxY	
			If (ty2 < ty3) Return False
			If (ty1 > ty4) Return False
			If (tx2 < tx3) Return False
			If (tx1 > tx4) Return False
			Return True
		End Method
				
	' //////////////////////////////////////////////////////////--------------------------------------
Rem
bbdoc: Check to see if Polygons overlap
returns: True for Overlap and Intersection co-ordinates.
about: 
	 <table>
		<tr><td><b>poly:iPoly</td><td>Polygon to check against</td></tr>
		<tr><td><b>rx:Float Var</td><td>X Co-ordinate of Primary Intersection</td></tr>
		<tr><td><b>ry:Float Var</td><td>Y Co-ordinate of Primary Intersection</td></tr>
	</table>
End Rem			
		Method PolyOverlap(poly:iPoly,rx# Var, ry# Var)
			Local tx1#, ty1#, tx2#, ty2#, tx3#, ty3#, tx4#, ty4#
			Local dSqr#, d1#, d2#, dx#, dy#
			Local lx1#, ly1#, lx2#, ly2#
			Local i,j			
			If ((Self.vis = False) Or (poly.vis = False)) Then Return False
			If (Self.BoundingOverLap(poly) = 0)  Return False
			j = Poly.Col_VertexCount - 1 
			For i = 1 To Poly.Col_VertexCount - 1 
				Local Ax# = Poly.ColVertex[j].fx + Poly.x
				Local Bx# = Poly.ColVertex[i].fx + Poly.x
				Local Ay# = Poly.ColVertex[j].fy + Poly.y
				Local By# = Poly.ColVertex[i].fy + Poly.y
				If Self.RayIntersect(ax,ay,bx,by,rx,ry) Return True
				j = i
			Next
			Return False
		End Method
		
	' //////////////////////////////////////////////////////////--------------------------------------
Rem
bbdoc: Check to see if a point is within a Polygon
returns: True if inside.
about: 
	 <table>
		<tr><td><b>x:Float</td><td>X Co-ordinate of Point</td></tr>
		<tr><td><b>y:Float</td><td>Y Co-ordinate of Point</td></tr>
	</table>
End Rem		
		Method PointInside(x#,y#)
			Local i, j
			Local bInPoly = 0
			Local xt1#,xt2#,yt1#,yt2#
			j = Self.Col_VertexCount - 1 
			For i = 0 To Self.Col_VertexCount - 1 
				xt1 = Self.ColVertex[j].fx + self.x
				xt2 = Self.ColVertex[i].fx + self.x
				yt1 = Self.ColVertex[j].fy + self.y
				yt2 = Self.ColVertex[i].fx + Self.y
				If x < ( (xt1 - xt2) * (y - yt2) / (yt1 - yt2) + xt2 ) And ((yt2 <= y And y < yt1) Or (yt1 <= y And y < yt2)) Then  bInPoly = 1 - bInPoly
				j = i
			Next
			Return bInPoly
		End Method
		
	' //////////////////////////////////////////////////////////--------------------------------------
Rem
bbdoc: Check to See if a Ray Interects a Polygon
returns: True if Intersects and intersection co-ordinates
about: 
	 <table>
		<tr><td><b>cx:float</td><td>X Co-ordinate of Start Point</td></tr>
		<tr><td><b>cy:float</td><td>Y Co-ordinate of Start Point</td></tr>
		<tr><td><b>dx:float</td><td>X Co-ordinate of End Point</td></tr>
		<tr><td><b>dy:float</td><td>Y Co-ordinate of End Point</td></tr>
		<tr><td><b>rx:Float Var</td><td>X Co-ordinate of Primary Intersection</td></tr>
		<tr><td><b>ry:Float Var</td><td>Y Co-ordinate of Primary Intersection</td></tr>
	</table>
End Rem		
		Method RayIntersect(cx#,cy#,dx#,dy#,rx# Var, ry# Var)
			Local j:Int = Self.Col_VertexCount - 1 
			For Local i:Int = 1 To Self.Col_VertexCount - 1 
				Local Ax# = Self.ColVertex[j].fx + self.x
				Local Bx# = Self.ColVertex[i].fx + self.x
				Local Ay# = Self.ColVertex[j].fy + Self.y
				Local By# = Self.ColVertex[i].fy + Self.y
				
				Local u_b#  = (dy - cy) * (bx - ax) - (dx - cx) * (by - ay)

   				If ( u_b <> 0 ) 
					
					Local ua_t# = (dx - cx) * (ay - cy) - (dy - cy) * (ax - cx)
    					Local ub_t# = (bx - ax) * (ay - cy) - (by - ay) * (ax - cx)
					Local ua# = ua_t / u_b
       				Local ub# = ub_t / u_b

        				If ( 0 <= ua And ua <= 1 And 0 <= ub And ub <= 1 ) 
			                rx# = ax + ua * (bx - ax)
                		    	ry# = ay + ua * (by - ay)
                			Return True		            
        				EndIf 
         			EndIf
				
				j = i
			Next
			Return False
		End Method