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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
devtastic
Prole
Posts: 3
Joined: Thu Jan 21, 2021 1:58 pm

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

Post 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
User avatar
dusoft
Party member
Posts: 791
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests