I am using purebasic's drawing functions to draw my game objects. Right now both the ball and paddle are boxes. How would I go about adding collision detection so that i can perform an action when the ball and paddle collide? Any assistance in this matter would be greatly appreciated.
PureOoze
Collision Detection
Re: Collision Detection
Collisions can be detected by determining if coordinates overlap for the two objects, or if using sprites by using SpriteCollision().
Here is a simple set of code that illustrates how this might be done.
I appologize if this is overkill but I couldn't help myself, once I got started I didn't want to leave it incomplete.
Here is a simple set of code that illustrates how this might be done.
I appologize if this is overkill but I couldn't help myself, once I got started I didn't want to leave it incomplete.

Code: Select all
#ScreenW = 300
#ScreenH = 300
Procedure handleError(p_result, p_text.s)
If Not p_result
MessageRequester("Error", p_text)
End
EndIf
EndProcedure
Structure boxObj
x.i ;location of top-left corner
y.i
width.i
height.i
EndStructure
handleError(InitKeyboard(),"initKeyboard() failed")
handleError(InitSprite(), "initSprite() failed")
handleError(OpenWindow(0, 0, 0,#ScreenW, #ScreenH, "Use Keys: left, right, space, or Q", #PB_Window_SystemMenu), "couldn't open window")
handleError(OpenWindowedScreen(WindowID(0), 0, 0, #ScreenW, #ScreenH, 0, 0, 0), "couldn't open screen")
Enumeration
#background_spr ;sheer laziness
#ball_spr
#paddle_spr
EndEnumeration
;example
Define.boxObj ball, paddle
With ball
\x = #ScreenW / 2
\y = #ScreenH / 2
\width = 5
\height = 5
EndWith
With paddle
\x = 5
\y = #ScreenH - 10
\width = 40
\height = 5
EndWith
handleError(CreateSprite(#background_spr, #ScreenW, #ScreenH),"unable to createsprite for background")
handleError(CreateSprite(#ball_spr, ball\width, ball\height),"unable to createsprite for ball")
handleError(CreateSprite(#paddle_spr, paddle\width, paddle\height),"unable to createsprite for paddle")
StartDrawing(SpriteOutput(#background_spr))
Box(0, 0, SpriteWidth(#background_spr), SpriteHeight(#background_spr), RGB(0,0,0))
StopDrawing()
StartDrawing(SpriteOutput(#ball_spr))
Box(0, 0, SpriteWidth(#ball_spr), SpriteHeight(#ball_spr), RGB(255,0,0))
StopDrawing()
StartDrawing(SpriteOutput(#paddle_spr))
Box(0, 0, SpriteWidth(#paddle_spr), SpriteHeight(#paddle_spr), RGB(255,255,255))
StopDrawing()
Define bvel.POINT ;ball's velocity
bvel\x = 3
bvel\y = 2
;isActive = #False
Repeat
event = WindowEvent()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Q) Or KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow
Break
EndIf
If KeyboardPushed(#PB_Key_Right) And (paddle\x + paddle\width) < #ScreenW
paddle\x + 2
EndIf
If KeyboardPushed(#PB_Key_Left) And paddle\x > 0
paddle\x - 2
EndIf
If Not isActive
If KeyboardPushed(#PB_Key_Space)
ball\x = #ScreenW / 2
ball\y = #ScreenH / 2
bvel\x = Random(2) + 1 - 6 * Random(1)
bvel\y = 2
isActive = #True
score = 0
EndIf
Else
ball\x + bvel\x
If (ball\x + ball\width) > #ScreenW Or ball\x < 0 ;wall collision check
bvel\x = -bvel\x
ball\x + bvel\x + bvel\x
EndIf
ball\y + bvel\y
If ball\y < 0 ;top collision check
bvel\y = -bvel\y
ball\y + bvel\y + bvel\y
ElseIf SpriteCollision(#ball_spr, ball\x, ball\y, #paddle_spr, paddle\x, paddle\y) ;paddle collision check
score + 10
bvel\y = -bvel\y
ball\y + bvel\y + bvel\y
EndIf
If ball\y > #ScreenH
;beep_(100,100)
isActive = #False
If score > hiscore
hiscore = score
EndIf
EndIf
EndIf
DisplaySprite(#background_spr, 0, 0)
DisplaySprite(#paddle_spr, paddle\x, paddle\y)
DisplaySprite(#ball_spr, ball\x, ball\y)
StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(0, 0, "Score: " + Str(score), RGB(0, 255, 0))
DrawText((#ScreenW - TextWidth(Str(hiscore))) / 2, 0, Str(hiscore), RGB(255, 255, 255))
If Not isActive
DrawText((#ScreenW - TextWidth("press [Space] for new game")) / 2, #ScreenH / 3, "press [Space] for new game", RGB(255, 0, 255))
EndIf
StopDrawing()
FlipBuffers()
Delay(10)
ForEver