Page 1 of 1

How to write a simple query world using love.physics?

Posted: Sat Feb 22, 2025 5:09 pm
by devtastic
I'm trying to use vanilla love.physics (box2d) but I could't find a direct function to query a specific region (circle, rectangle etc.). There is one function named World:queryBoundingBox() but I can't figure out how to use this. Ultimately, I want to query a circular area in front of the player and return the object class in that query area. I'm using STI to import Tiled levels. I have set the class names of relevant colliders inside Tiled (for bushes, chest etc.) and I want to retrieve this data from my query function. Can someone help me write a simple function for this using only love.physics? My progress on the function so far is this,

Code: Select all

local function Player:queryWorld()
    -- Spawn a query collider on player
    local query_collider = love.physics.newCircleShape(self.x, self.y, 50)
    query_collider:setSensor(true)

    -- List to store all the colliders
    local inside_query = {}

    -- Collider classes: TODO
end

Re: How to write a simple query world using love.physics?

Posted: Sun Feb 23, 2025 3:20 pm
by dusoft
You just setup a sensor that's moving similar to a player and just receive callbacks of collisions.

You don't have to actively query the world, instead just use the callbacks and resolve whatever collisions you need directly there:
https://love2d.org/wiki/Tutorial:Physic ... nCallbacks

Be aware that order of fixtures is not set, so you will have to check both colliding fixtures (e.g. player can be fixture_a, but it also can be fixture_b sometimes).