Blitz3D+ Command Reference

ImagesOverlap ( image1,x1,y1,image2,x2,y2 )

Parameters

image1 - first image

x1 - x position image1 is drawn at

y1 - y position image1 is drawn at

image2 - second image

x2 - x position image2 is drawn at

y2 - y position image2 is drawn at

Description

Returns True if the rectangles of two images overlap.

This is the cheap collision test. It compares the two images' bounding boxes and nothing else, so transparent pixels count just as much as solid ones - two sprites whose corners graze each other register a hit even if the visible artwork never touches.

Cheap is often exactly right. Blocky graphics that fill their frames, a Robotron-style shooter, a pickup you want to be generous about, a broad-phase filter before the expensive test: all fine. Pass the same x,y you would pass to DrawImage and each image's handle is taken into account for you.

For pixel-accurate results use ImagesCollide instead. A good pattern in a busy game is both: ImagesOverlap over everything to throw out the obvious misses, then ImagesCollide on the few pairs that are close.

There is no frame parameter - the test always uses frame 0 of each image, and takes its size from that frame. If your animation frames differ in shape and you need that to matter, use ImagesCollide, which does take frames.

See also: ImagesCollide, RectsOverlap, ImageRectOverlap, ImageRectCollide.

Example

; ImagesOverlap Example
; ---------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

player=LoadImage("media/player.bmp")

; Create an asteroid image - a round boulder with empty corners
rock=CreateImage(96,96)
SetBuffer ImageBuffer(rock)
Color 130,120,110
Oval 8,8,80,80,True
SetBuffer BackBuffer()

px=100
py=100

While Not KeyDown(1)

    Cls

    ; Arrow keys steer the ship
    If KeyDown(203) Then px=px-3
    If KeyDown(205) Then px=px+3
    If KeyDown(200) Then py=py-3
    If KeyDown(208) Then py=py+3

    ; ImagesOverlap only compares the two images' bounding rectangles -
    ; very fast, but transparent corners count as a hit
    hit=ImagesOverlap(player,px,py,rock,272,192)

    DrawImage rock,272,192
    DrawImage player,px,py

    ; Flash a warning border while overlapping
    If hit Then
        Color 255,0,0
        Rect 0,0,640,480,False
        Rect 1,1,638,478,False
    End If

    Text 0,0,"Arrow keys: fly the ship into the asteroid   Esc: exit"
    If hit Then
        Text 0,20,"ImagesOverlap=1 - boxes touch (even the transparent corners!)"
    Else
        Text 0,20,"ImagesOverlap=0 - fast rectangle-only test"
    End If

    Flip

Wend

End

Index