Hi phreakz

If you want to code a game or whatever and need a circle based collision detection, instead of the rect based SpriteCollision() command, i wrote a small example to explain you how to manage it... Please note that this circle based collision detection is not really fast and it need some cpu time... Hope you like this small example... Have fun...
Code: Select all
;-------------------------------------
;
; PB2.90 - CircleBased CollisionCheck
;
;-------------------------------------
;
#scrw = 640 ; screenwidth
#scrh = 480 ; screendheight
#scrd = 16 ; screendepth
;
obj1r = 48 ; Sprite1, Radius = 48 - C H A N G E T H I S F O R T E S T I N G :wink:
obj2r = 48 ; Sprite2, Radius = 48 - C H A N G E T H I S F O R T E S T I N G :wink:
;
;-------- Init all the needed system stuff --------
;
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageBox_ (0,"Error - CircleBased CollisionCheck", "PureBasic 2.90 - Example", #MB_ICONINFORMATION|#MB_OK)
End
EndIf
;
;-------- Init Screen amd set priority high --------
;
If OpenScreen(#scrw,#scrh,#scrd,"SuperBall 32k v1.0 by Secretly!") = 0
MessageBox_ (0,"Error - CircleBased CollisionCheck", "PureBasic 2.90 - Example", #MB_ICONINFORMATION|#MB_OK)
End
EndIf
;
SetPriorityClass_(GetCurrentProcess_(), 13 ) ; 13 = HIGH_PRIORITY_CLASS
;
;-------- Init some stuff for our sprites -------
;
obj1 = 1 ; Sprite1, SpriteNumber
obj1x = #scrw/2-obj1r ; Sprite1, set x position
obj1y = #scrh/2-obj1r ; Sprite1, set y position
;
obj2 = 2 ; Sprite2, SpriteNumber
;
Gosub SUB_CreateBalls ; Precalc the ball sprites
;
;-------- This is the magic program part -------
;
Repeat
ClearScreen (0,0,0) ; ClearScreen
;
ExamineMouse() ; Update mouse position
obj2x = MouseX() ; Get actual x position for Sprite2
obj2y = MouseY() ; Get actual y position for Sprite2
;
DisplayTransparentSprite (obj1, obj1x, obj1y) ; Sprite1 - Centre on screen!
DisplayTransparentSprite (obj2, obj2x, obj2y) ; Sprite2 - Thie one is moveable!
;
;-------- Calculate distance between two points --------
;
xdistance = Pow (((obj1x-obj2r) - (obj2x-obj1r)),2) ; This are the
ydistance = Pow (((obj1y-obj2r) - (obj2y-obj1r)),2) ; magic code lines
distance = Sqr (xdistance + ydistance) ; behind this routine :wink:
;
mindistance = obj1r+obj2r ; Calculate collision radius
;
;-------- Check if collision --------
;
If distance Draw white circles
;----------------------------------------
SUB_CreateBalls: ; !!! NOT OPTIMIZED !!!
;
StartDrawing(ScreenOutput()) ; ### START DRAWING ###
FrontColour (255,255,255) ; Set colour to white
Circle (obj1r,obj1r,obj1r) ; Create our FilledCircle
StopDrawing() ; ### STOP DRAWING ###
;
GrabSprite (obj1, 0, 0, obj1r*2, obj1r*2) ; Grab ball1 for easy handling
;
;----------------------------------------
ClearScreen(0,0,0) ; ClearScreen...
;
StartDrawing(ScreenOutput()) ; ### START DRAWING ###
FrontColour (255,255,255) ; Set colour to white
Circle (obj2r,obj2r,obj2r) ; Create our FilledCircle
StopDrawing() ; ### STOP DRAWING ###
;
GrabSprite (obj2, 0, 0, obj2r*2, obj2r*2) ; Grab ball1 for easy handling
;
Return
;----------------------------------------
; Subroutine -> Print the result
;----------------------------------------
SUB_PrintResult:
;
StartDrawing (ScreenOutput()) ; ### START DRAWING ###
DrawingMode (1) ; Background transparent
FrontColour (255,255,255) ; Set colour to white
DrawText ("Collision Result: "+Str(collision)) ; Print text and result
StopDrawing() ; ### STOP DRAWING ###
;
Return
;----------------------------------------
PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...
greetz
MrVainSCL! aka Thorsten