Collisions EEK

BlitzPlus Forums/BlitzPlus Beginners Area/Collisions EEK

Kieran(Posted 2006) [#1]
Heyas,

Here's some code I'm playing with... Bet you can't guess what it is *8)

Anyway can someone point me in the right direction, whats the simplest way to detect a collision between paddles, and shoot it off on an angle?

Graphics 640, 480, 0, 2

SetBuffer BackBuffer()

; Initialize and center paddles.
Global PaddleY1 = 190
Global PaddleY2 = 190

; Initialize and center ball.
Global BallPosX = 317
Global BallPosY = 237

While Not KeyDown(1)
	Cls
	
	ProcessPaddle1()
	ProcessPaddle2()
	ProcessBall()
	
	Flip
Wend 

Function ProcessBall()

	BallPosX = BallPosX + 1
	Rect BallPosX, BallPosY, 6, 6
	
End Function

Function ProcessPaddle1()
	
	If KeyDown(30)
		PaddleY1 = PaddleY1 - 2
	ElseIf KeyDown(44)
		PaddleY1 = PaddleY1 + 2
	EndIf
	
	If PaddleY1 < 0 Then PaddleY1 = 0
	If PaddleY1 > 380 Then PaddleY1 = 380
	Rect 10, PaddleY1, 10, 100, 1
	
End Function

Function ProcessPaddle2()
	
	If KeyDown(200)
		PaddleY2 = PaddleY2 - 2
	ElseIf KeyDown(208)
		PaddleY2 = PaddleY2 + 2
	EndIf
	
	If PaddleY2 < 0 Then PaddleY2 = 0
	If PaddleY2 > 380 Then PaddleY2 = 380
	Rect 620, PaddleY2, 10, 100, 1
	
End Function




octothorpe(Posted 2006) [#2]
The simplest approach is to check if the ball's rectangle intersects a paddle's rectangle (using RectsOverlap().) If so, reverse the direction of the ball on the X-axis. Also, to have your pong paddles change the angle of the ball, add an amount to the Y component of the ball's velocity depending on how far from the centre of the paddle the ball struck.


Paul Murray(Posted 2006) [#3]
If you're using custom images with transparent edges, you can use ImagesCollide() to check for per-pixel collisions.

Check out http://blitzstuff.awardspace.biz/phpBB2/viewtopic.php?t=23
for a tutorial orginally from BlitzCoder.com for a pong game of you get stuck.


Kieran(Posted 2006) [#4]
octothorpe thanks mate, you where spot on, works a charm. I didn't want to load images and use imagesCollide(), just for something different.