tile collision

BlitzPlus Forums/BlitzPlus Beginners Area/tile collision

Function MoveBall(accuracy# = 1.0)
	Local Distance# = GetDistance(0, 0, BallXSpeed, BallYSpeed)
	Local Angle# = GetAngle(0, 0, BallXSpeed, BallYSpeed)
	For i# = 0 To Distance# Step 0
		BallX = BallX + Cos(Angle#) * Distance# * accuracy#
		BallY = BallY + Cos(Angle#) * Distance# * accuracy#
		i = i + Distance# * accuracy#
		If Collided() Then Return False
	Next
	Return True
End Function

Function GetAngle#(basex, basey, targetx, targety)
	If targety>basey-1 Then
		angle=ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
	Else
		If targetx>basex-1 Then
			angle=270+90-ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
		Else
			angle=360-ACos((targetx-basex)/Sqr((targetx-basex)*(targetx-basex)+(targety-basey)*(targety-basey)))
		EndIf
	EndIf

	If targetx=basex And targety=basey Then Return 0 Else Return angle
End Function

Function GetDistance#(x1#, y1#, x2#, y2#)
	Return Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
End Function


function MoveBall(accuracy#) will move the ball and return true if no collision was detected else will be returned false.
accuracy# values can be in range of 0 - 1
function Collided() is missing so you'll have to write it yourself though you should already have it.