Get clicked Fixtures without checking every object?

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
Jugbot
Prole
Posts: 14
Joined: Mon Jun 24, 2019 9:54 pm

Get clicked Fixtures without checking every object?

Post by Jugbot »

Currently I am using ecs-pattern (I.e. a big loop) and checking for each fixture if

Code: Select all

e.fixture:testPoint(mx, my)
is true. However I will be switching to the async love mouse callbacks instead of polling and was wondering if there is a better way than checking every fixture in the game to see which the user clicked. Maybe something like a queryPoint(mx, my) that returns all fixtures that contain that point?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Get clicked Fixtures without checking every object?

Post by raidho36 »

No, you will have to check every relevant instance. You can narrow down the list of relevant instances by keeping such a list at hand, such as by using spatial partitioning of sorts. Box2d keeps such spatial partition internally (a quadtree) so if you can use physics' mechanisms then that'll do the job. Or you can use whatever other technique that allows to figure out approximately (or exactly) which object you should access based on some information about it. One example is using a space-filling curve in conjunction with hashtable-mode Lua table for storing and accessing the spatial information of the objects.

If you're not performance-constrained, you can simply loop over every single instance without performing any optimization to cut down on the processing. Keep in mind that minimum system requirements is a thing and it shouldn't be a $1000 machine.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get clicked Fixtures without checking every object?

Post by pgimeno »

There's no queryPoint, but there's World:queryBoundingBox which is close enough.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Get clicked Fixtures without checking every object?

Post by ivan »

queryBoundingBox will return potential fixtures that may collide with your point but then you still need to iterate each fixture returned by that query and check it using fixture:testPoint.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 5 guests