physics.queryRegion()

Type Function
Library physics.*
Return value Array of tables describing each hit
Revision Release 2024.3703
Keywords queryRegion, physics, collision
See also physics.reflectRay()

Overview

This function is used to find the objects that intersect with an axis-aligned (non-rotated) box. This box is defined by an upper-left coordinate and a lower-right coordinate.

The positions returned are in content space.

Syntax

physics.queryRegion( upperLeftX, upperLeftY, lowerRightX, lowerRightY )
upperLeftX (required)

Number. The upper-left x coordinate for the box region.

upperLeftY (required)

Number. The upper-left y coordinate for the box region.

lowerRightX (required)

Number. The lower-right x coordinate for the box region.

lowerRightY (required)

Number. The lower-right y coordinate for the box region.

Result Properties

hits will be an array of each DisplayObject colliding with the box region.

Example

local hits = physics.queryRegion( 10, 40, 100, 160 )

if ( hits ) then

    -- There's at least one hit
    print( "Hit count: " .. tostring( #hits ) )

    -- Output the results
    for i,v in ipairs( hits ) do
        print( "Object position: ", v.x, v.y )
    end
else
    -- No hits in region
end