CollideLine

BlitzMax Forums/BlitzMax Module Tweaks/CollideLine

taxlerendiosk(Posted 2005) [#1]
This is some code to be able to collide a line between any two points of given pixel thickness. It should go in Max2D.bmx and could probably be vastly improved, I just bashed it together in the last few minutes to see if it would work.

Function CollideLine:Object[](px1,py1,px2,py2,width,collidemask%,writemask%,id:Object=Null) 
	Local	q:TQuad
	q=CreateQuadFromLine(px1,py1,px2,py2,width,id)
	Return CollideQuad(q,collidemask,writemask)
End Function


Function CreateQuadFromLine:TQuad(px1,py1,px2,py2,width,id:Object)
	Local	x0#,y0#,x1#,y1#,tx#,ty#
	Local	tx0#,ty0#,tx1#,ty1#,tx2#,ty2#,tx3#,ty3#
	Local	minx#,miny#,maxx#,maxy#
	Local	q:TQuad
	Local	f:TImageFrame

	Local Angle! = ATan2(py2 - py1,px2 - px1) + 135
	
	tx0 = origin_x+px1 - (Sin(Angle) * Width)
	ty0 = origin_y+py1 + (Cos(Angle) * Width)

	tx1 = origin_x+px1 + (Cos(Angle) * Width)
	ty1 = origin_y+py1 + (Sin(Angle) * Width)

	tx2 = origin_x+px2 + (Sin(Angle) * Width)
	ty2 = origin_y+py2 - (Cos(Angle) * Width)

	tx3 = origin_x+px2 - (Cos(Angle) * Width)
	ty3 = origin_y+py2 - (Sin(Angle) * Width)

	If freequads
		q=freequads
		freequads=q.link
	Else
		q=New TQuad
	EndIf
	q.link=Null
	q.id=id
	q.setcoords(tx0,ty0,tx1,ty1,tx2,ty2,tx3,ty3)	
	Return q
End Function