Help with "bounce ball off wall code"

Miscellaneous Forums/General Discussion/Help with "bounce ball off wall code"

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.

Cool I'll check it out. Meanwhile I made this:

Strict

Graphics 800,600,0

Global c:TCircle = TCircle.Create(0,0,50)
c.color = TColor.Create(255,255,255)
Global l:TLine = TLine.Create(100,100,400,400)
l.CalcLength()

While Not KeyHit(key_escape)
	'Place cicle where mouse is
	c.x = MouseX()
	c.y = MouseY()
	'Change colour depending on collision status 
	If ccCircleLineIntersect(c,l) Then
		c.color.set(255,0,0)		
	Else
		c.color.set(0,255,0)		
	EndIf
	
	'Draw the scene
	Cls
	c.Draw()
	l.draw()
	Flip	
Wend

'----------------------------------------------------
Type TCircle
	Field x#
	Field y#
	Field r# 'radius
	Field Color:TColor
	
	Function Create:TCircle(x,y,r)
		c:TCircle = New TCircle
		c.x=x
		c.y=y
		c.r=r
		Return c
	End Function
	
	Method Draw()
		If Color<>Null Then Color.Apply
		DrawOval(x-r,y-r,r*2,r*2)
		If Color<>Null Then Color.Restore
	End Method
End Type

Type TLine
	Field sx#
	Field sy#
	Field ex#
	Field ey#
	Field length#
	Field Color:TColor
	
	Function Create:TLine(sx,sy,ex,ey)
		l:TLine = New TLine
		l.sx=sx
		l.sy=sy
		l.ex=ex
		l.ey=ey
		Return l
	End Function

	Method CalcLength#()
		length = Sqr((ex-sx)*(ex-sx)+(ey-sy)*(ey-sy))
	End Method
	
	Method Draw(DrawLastPixel=True)
		If Color<>Null Then Color.Apply
		DrawLine(sx,sy,ex,ey,DrawLastPixel)
		If Color<>Null Then Color.Restore
	End Method

End Type

Type TColor 'I'm using American spelling to keep it more like the Blitz commands.
	Field R=0
	Field G=0
	Field B=0
	Field Alpha#=1
	
	Function Create:TColor(r,g,b,alpha#=1)
		Local c:TColor = New TColor
		c.Set(r,g,b,alpha)
		Return c
	End Function
	
	Method Set(tR,tG,tB,talpha#=1)
		R = tR
		G = tG
		B = tB
		Alpha = tAlpha
	End Method

	Method Get(tR Var,tG Var,tB Var)
		tR = R
		tG = G
		tB = B
	End Method
	
	Method GetFull(tR Var,tG Var,tB Var, tAlpha# Var)
		tR = R
		tG = G
		tB = B
		tAlpha = Alpha
	End Method
	
	Method Apply()
		SetColor R,G,B
		SetAlpha Alpha
	End Method
	
	Method ApplyNoAlpha()
		SetColor R,G,B
	End Method

	Method Copy(c:TColor)
		R=c.R
		G=c.G
		B=c.B
		Alpha=c.Alpha
	End Method
	
	Method Restore()
		SetColor 255,255,255
		SetAlpha 1
	End Method
End Type

Function ccCircleLineIntersect%(c:TCircle,l:TLine)
	'Make sure you precalc the line length
	Local x0# = c.x
	Local y0# = c.y
	Local x1# = l.sx
	Local y1# = l.sy
	Local x2# = l.ex
	Local y2# = l.ey
	Local n# = Abs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1))	
	Local d# = l.length
	Local dist# = n/d
	If dist > c.r Return False
	Local d1# = Sqr((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1))
	If d1-c.r > d Return False
	Local d2# = Sqr((x0-x2)*(x0-x2)+(y0-y2)*(y0-y2))
	If d2-c.r > d Return False
	Return True
End Function

The circle goes red when it's over the line. I do have a bit of a problem with the circle near the end points though. Perhaps the line needs to be "infinite" in length? But then I couldn't make a square :-(

Now I have to work out the angle of reflection...