Hi all
I have made a circle to circle collision test function (would be nice to have one of these built in *hint* *hint*). I've also half-inched a nice circle drawing routine and ellipse drawing routine off these forums (I think Minion and Gernot to credit) to show the collisions visually. Again, would be nice to have a fast and optimised version of these two functions built in (*hint* *hint*)
I would gladly welcome any suggestions for optimising the circoll collision function.
I have made a circle to circle collision test function (would be nice to have one of these built in *hint* *hint*). I've also half-inched a nice circle drawing routine and ellipse drawing routine off these forums (I think Minion and Gernot to credit) to show the collisions visually. Again, would be nice to have a fast and optimised version of these two functions built in (*hint* *hint*)
I would gladly welcome any suggestions for optimising the circoll collision function.
Code (glbasic) Select
SETSCREEN 640,480,0
LIMITFPS 60
BLACKSCREEN
GLOBAL x1=320
GLOBAL y1=240
GLOBAL rad1= 100
GLOBAL x2=320
GLOBAL y2=240
GLOBAL rad2= 50
WHILE TRUE
drawCircle(x1,y1,rad1, RGB(255,255,255))
LOCAL a,b
MOUSESTATE x2,y2, a,b
drawCircle(x2,y2, rad2, RGB(200,100,100))
IF circoll(x1,y1,rad1, x2,y2,rad2) THEN PRINT "Collision!", 10,10
SHOWSCREEN
WEND
// ------------------------------------------------------------- //
// -=# CIRCOLL #=-
// ------------------------------------------------------------- //
FUNCTION circoll: x1,y1,radius1, x2,y2,radius2
LOCAL dx, dy
IF x1 dx = x2 - x1
ELSE
dx = x1 - x2
ENDIF
IF y1 dy = y2 - y1
ELSE
dy = y1 - y2
ENDIF
LOCAL dist= SQR( (dx*dx) + (dy*dy) )
IF dist < (radius1+radius2)
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
// ------------------------------------------------------------- //
// -=# ELLPISE #=-
// ------------------------------------------------------------- //
FUNCTION drawEllipse: x, y, w, h, col
LOCAL dy, xi
w=w/2
h=h/2
x=x+w
FOR dy = -h TO h
xi = w/h * SQR(h*h - dy*dy)
DRAWLINE x-xi, y+dy+h, x+xi, y+dy+h, col
NEXT
ENDFUNCTION // ELLIPSE
// ------------------------------------------------------------- //
// -=# CIRC #=-
// ------------------------------------------------------------- //
FUNCTION drawCircle: x, y, r, c
// These values are defined LOCAL:
// x, y, r, c
LOCAL x1=SIN(0)*r
LOCAL y1=COS(0)*r
FOR j=1 TO 360
LOCAL x2=SIN(j)*r
LOCAL y2=COS(j)*r
DRAWLINE x+x1,y+y1,x+x2,y+y2,c
x1=x2
y1=y2
NEXT
ENDFUNCTION // CIRC