Main Menu

Detect If Two Rectangle Collide

Started by kevin, March 20, 2010, 09:58:02 AM

Previous topic - Next topic

kevin

Detects If Two Rects Overlap

 This version returns a true if the rects overlap.

PlayBASIC Code: [Select]
setfps 60

Do


Cls 0


sx1=mouseX()
sy1=mouseY()
sx2=sx1+100
sy2=sy1+50

dx1=400
dy1=300
dx2=dx1+200
dy2=dy1+150


Colour1=$00ff00
Colour2=$ff0000

if RectsOverLap(sx1,sy1,sx2,sy2,dx1,dy1,dx2,dy2)
Colour1=$ffffff
Colour2=$ff0000
print "hit"

endif
boxc dx1,dy1,dx2,dy2,true,colour2

boxc sx1,sy1,sx2,sy2,true,colour1

Sync
loop



Psub RectsOverLap(sx1,sy1,sx2,sy2,dx1,dy1,dx2,dy2)
result=false
if sx1<dx2
if sx2>=dx1
if sy1<dy2
if sy2>=dy1
result=true
endif
endif
endif
endif
EndpSub Result








Detect if one Rect Is Inside Another


  This version only returns true when the source rect is inside the destination



PlayBASIC Code: [Select]
setfps 60

Do


Cls 0


sx1=mouseX()
sy1=mouseY()
sx2=sx1+100
sy2=sy1+50

dx1=400
dy1=300
dx2=dx1+200
dy2=dy1+150


Colour1=$00ff00
Colour2=$ff0000

if RectInsideRect(sx1,sy1,sx2,sy2,dx1,dy1,dx2,dy2)
Colour1=$ffffff
Colour2=$ff0000
print "Inside"

endif
boxc dx1,dy1,dx2,dy2,true,colour2

boxc sx1,sy1,sx2,sy2,true,colour1

Sync
loop


Psub RectInsideRect(sx1,sy1,sx2,sy2,dx1,dy1,dx2,dy2)
result=false
if sx1>=dx1
if sx2<dx2
if sy1>=dy1
if sy2<dy2
result=true
endif
endif
endif
endif
EndpSub Result









ATLUS


kevin


Sorry, but i don't understand your question

ATLUS


kevin


See Manual ->Sprites

RectHitSprite
CollisionFlag = RectHitSprite(RectX1, RectY1, RectX2, RectY2, SpriteIndex)

RectHitSpritePixels
CollisionState = RectHitSpritePixels(X1, Y1, X2, Y2, SpriteIndex, Accuracy#)




ATLUS


kevin


  The above functions can be used to do that.

ATLUS