Test for Collisions on a Line

BlitzMax Forums/BlitzMax Programming/Test for Collisions on a Line

BaderHasan(Posted 2008) [#1]
I thought I'd share something I came up with today. This function RayCollide returns a list of objects it collides with. This could be useful for finding collisions between frames when objects are moving fast. "Line.bmp" is just a 1x1 pixel white dot or similar. It is stretched out and rotated into a proper line.

Any better ways to do this?

Global Line:TImage = LoadImage("Line.bmp") 

Function Distance:Float(x1:Float, y1:Float, x2:Float, y2:Float) 
	Local a:Float = x2 - x1
	Local b:Float = y2 - y1
	Return Sqr(a * a + b * b) 
End Function

Function RayCollide:Object[] (x1:Float, y1:Float, x2:Float, y2:Float, layer:Byte) 
	SetScale Distance(x1, y1, x2, y2), 1
	SetRotation ATan2((y2 - y1), (x2 - x1)) 
	
	Local ObjList:Object[] = CollideImage(Line, x1, y1, 0, layer, 0) 
	SetScale 1, 1
	Return ObjList
End Function