Finish Line end on intersection

Blitz3D Forums/Blitz3D Beginners Area/Finish Line end on 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.

ok, thank you:
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 

Type block
Field x,y,x2,y2,w,h,placed,cont
End Type 

Global onone=0
Global ballsin=0

Setup()

While Not KeyDown(1)
	
	UpdateBalls()
	UpdateWalls()
	UpdateBlocks()
	
	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 UpdateBlocks()

If Not onone=1 Then 
If KeyHit(57)
	bl.block=New block
	bl\x=MouseX()
	bl\y=MouseY()
	bl\x2=MouseX()+1
	bl\y2=MouseY()+1
	onone=1
EndIf 
Else
If KeyHit(57) Then 
For bl.block=Each block
	bl\placed=1
	onone=0
Next 
EndIf 
EndIf 

For bl.block=Each block

If bl\placed<>1 Then
bl\x2=MouseX()
bl\y2=MouseY()
bl\w=bl\x2-bl\x
bl\h=bl\y2-bl\y
EndIf 

If bl\placed=1 Then 
	
ballsin=0
	
For b.ball=Each ball

If b\x>bl\x And b\x<bl\x2 And b\y>bl\y And b\y<bl\y2 Then 
If bl\cont=0 Then bl\cont=1
ballsin=ballsin+1
EndIf 

Next 

EndIf 

Next 

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 Or w\placed2=0
			If w\axis=1 Then 
				If Not w\x2>800 And w\placed2=0 Then w\x2=w\x2+1
				If Not w\x<0 And w\placed1=0 Then w\x=w\x-1
				If w\x<0 Then w\placed1=1 
				If w\x2>800 Then w\placed2=1
			Else 
				If Not w\y2>600 And w\placed2=0 Then w\y2=w\y2+1
				If Not w\y<0 And w\placed1=0 Then w\y=w\y-1
				If w\y<0 Then w\placed1=1 
				If w\y2>800 Then 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 
	
	For bl.block=Each block
	Color 255,0,0
	Rect bl\x,bl\y,bl\w,bl\h,0
	Color 255,255,255
	Next 
	
	Text 0,0,ballsin
	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 v.wall=Each wall

x=v\x
y=v\y
axis=v\axis
x2=v\x2
y2=v\y2

For w.wall=Each wall

If v<>w
	
	If w<>Null
	
If w\axis<>axis

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

EndIf 

EndIf 

Next 

Next 

End Function  
;~IDEal Editor Parameters:
;~F#B1#C9#DE#F2
;~C#Blitz3D



And here's a diagram of what i'm trying to achieve: