Finish Line end on intersection

Blitz3D Forums/Blitz3D Beginners Area/Finish Line end on intersection

Graphics 800,600,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Type ball
	Field x,y
	Field xv,yv
End Type 

Type wall
	Field x,y,x2,y2,ox,oy
	Field wd,hg,placed1,placed2,axis ;(1 for x,2 for y)
End Type 

Setup()

While Not KeyDown(1)
	
	UpdateBalls()
	UpdateWalls()
	
	Draw()
	
Wend 
End 

Function Setup()
	
	b.ball=New ball
	b\x=Rand(100,700)
	b\y=Rand(100,500)
	b\xv=Rand(-5,5)
	b\yv=Rand(-5,5)
	
End Function

Function UpdateBalls()
	
	For b.ball=Each ball
		
		b\x=b\x+b\xv
		b\y=b\y+b\yv
		
		If b\x>=800 Then b\xv=-b\xv
		If b\x<=0 Then b\xv=-b\xv
		If b\y>=600 Then b\yv=-b\yv
		If b\y<=0 Then b\yv=-b\yv
		
		For w.wall=Each wall
		
			If LineOverlapCircle(w\x,w\y,w\x2,w\y2,b\x,b\y,5)=1 Then 
			If w\placed1=1 And w\placed2=1 Then 
			b\xv=-b\xv
			b\yv=-b\yv
			Else 
			Delete w
			Return 
			EndIf 
			EndIf 
				
		Next 
	Next 
	
End Function 

Function UpdateWalls()
	
p=GetMouse()

CheckWallCollisions()

For w.wall=Each wall
		
		If w\placed1=0 And w\placed2=0
			If w\axis=1 Then 
				If Not w\x2>800 And w\placed2=0 Then w\x2=w\x2+2
				If Not w\x<0 And w\placed1=0 Then w\x=w\x-2
				If w\x<0 And w\x2>800 Then w\placed1=1:w\placed2=1
			Else 
				If Not w\y2>600 And w\placed2=0 Then w\y2=w\y2+2
				If Not w\y<0 And w\placed1=0 Then w\y=w\y-2
				If w\y<0 And w\y2>600 Then w\placed1=1:w\placed2=1
			EndIf 
			
		
		EndIf 
	
Next 

If MouseHit(1) Then 
		w.wall=New wall
		w\x=MouseX()
		w\y=MouseY()
		w\hg=1
		w\wd=1
		w\x2=MouseX()
		w\y2=MouseY()
		w\ox=MouseX()
		w\oy=MouseY()
		w\axis=1
		FlushMouse
		Return 
EndIf 
		
If MouseHit(2) Then 
		w.wall=New wall
		w\x=MouseX()
		w\y=MouseY()
		w\hg=1
		w\wd=1
		w\x2=MouseX()
		w\y2=MouseY()
		w\axis=2
		FlushMouse
		Return 
EndIf 

End Function

Function Draw()
Local draw

	Cls 
	
	For b.ball=Each ball
		Oval b\x,b\y,10,10,1
	Next 
	
	For w.wall=Each wall
		Line w\x,w\y,w\x2,w\y2
	Next 
	Flip
	
End Function 

Function fLine(x,y,x1,y1,r=255,g=255,b=255)
; fLine will only draw horizonal or vertical lines, no diagonals
; Defaults to a white line.

argb=(b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))

If x=x1
	If y>y1 Then t=y1:y1=y:y=t
	For n=y To y1
	WritePixel x,n,argb,BackBuffer()
	Next
EndIf

If y=y1
	If x>x1 Then t=x1:x1=x:x=t
	For n=x To x1
	WritePixel n,y,argb,BackBuffer()
	Next
EndIf 
End Function

Function LineOverlapCircle(x1#,y1#,x2#,y2#,cx#,cy#,Radius#)
	;'Calc Closest Point To circle center
	Local dx31#=cx#-x1#
	Local dx21#=x2#-x1#
	Local dy31#=cy#-y1#
	Local dy21#=y2#-y1#
	Local d#=((dx21#*dx21#)+(dy21#*dy21#))
	If d#<>0 Then d#=((dx31#*dx21#)+(dy31#*dy21#))/d#
	;'Clip To the line segments legal bounds
	If d#<0.0 Then d#=0
	If d#>1.0 Then d#=1
	Local dx#=cx#-(x1#+(dx21#*d#))
	Local dy#=cy#-(y1#+(dy21#*d#))
	If Radius# => Sqr((dx#*dx#)+(dy#*dy#))
		;'Line intersects circle
		Return 1 
	EndIf
	Return 0
End Function

Function linesIntersect(x1%,y1%, x2%,y2%, x3%,y3%, x4%,y4%)

	numeratorA#  = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
	numeratorB#  = (x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
	denominator# = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)

	If denominator = 0.0 Then
;		Text(10,20,"No intersection")
;		If numeratorA = 0.0 And numeratorB = 0.0 And denominator = 0.0 Then
;			Text(10,40,"Lines are coincident")
;		Else
;			Text(10,40,"Lines are parallel")
;		End If
		Return False
	Else
		Ua# = numeratorA/denominator
		Ub# = numeratorB/denominator
		range1% = Ua >= 0.0 And Ua <= 1.0
		range2% = Ub >= 0.0 And Ub <= 1.0
		If range1 And range2 Then
			intersectX% = Floor(x1 + Ua*(x2-x1)+.5)
			intersectY% = Floor(y1 + Ua*(y2-y1)+.5)
			Return True
		Else
;			Text(10,20,"No intersection")
			Return False
		End If
    End If
End Function

Function CheckWallCollisions()
Local x,y,axis,x2,y2

For w.wall=Each wall

x=w\x
y=w\y
axis=w\axis
x2=w\x2
y2=w\y2

For w.wall=Each wall

If w<>Null

If w\axis<>axis

	If w\axis=1 Then 
	If axis=2 Then 
	If w\x=x Or w\x=x-1 Or w\x=x+1 Then 
	w\placed1=1
	If w\x2=x2 Or w\x2=x2-1 Or w\x2=x2+1 Then 
	w\placed2=1
	EndIf 
	EndIf 
	EndIf 
	Else If axis=2 Then 
	If w\y=y Or w\y=y-1 Or w\y=y+1 Then 
	w\placed1=1
	If w\y2=y2 Or w\y2=y2-1 Or w\y2=y2+1 Then 
	w\placed2=1
	EndIf
	EndIf 
	EndIf 

EndIf 

EndIf 

Next 

Next 

End Function  


what's the problem with this? I get a memory access in debug mode.