Jump to content

Determining if sprite is 'in range' of another sprite?


ForgeableSum
 Share

Recommended Posts

Not looking for a purely distanceToObject-based solution since it would be too resource-intensive to run distanceToObject for every sprite against every other sprite in the updates loop. 

 

In range as in, I need to run a function when any sprite gets within x number of any sprite. A user on here suggested adding an invisible child to each sprite (the child sprite's width and height being the 'range') and then checking overlap. But how would one check for overlap of the children's children of a group

 

Looking for advice. 

Link to comment
Share on other sites

Checking for overlap is going to be at least as intensive as doing a distance calculation. Have you tried it? I bet you'll be surprised how fast it is. I just did a quick test and I can do 1000 distance calculations in 3 milliseconds.

Link to comment
Share on other sites

It is faster than I thought. I guess what threw me off is that if you put a console.log into the callback function of the overlap (and that overlap is something like group vs group), it becomes heavily taxing on frame rate. Stuff like moveToObject doesn't seem to have the same effect. Also, I'm not simply doing an overlap check on sprites in a group, that would not be performance intensive at all. I'm doing a check on Sprite vs Group on each sprite or some variation of that. 

 

Anyway, I ended up doing the following to optimize performance:

- Invisible 'range' sprites with x/y coordinates that are updated every 500 milliseconds to match the location of their corresponding sprite. These sprites have their own group so I can easily perform overlap checks. I couldn't find a way to perform overlap checks on the children of children of a group (hence I didn't make these the children of their corresponding sprites).

- A quad tree implementation which is cleared and repopulated every 500 mili.

- Every 500 milli, I clear and repopulate the qTree and find which sprites are close enough to overlap. Then I loop through the invisible range rects, find their corresponding sprites and loop through their closest sprites performing an overlap check. 

 

Of course it's a bit more complicated than that, but that's the basic idea. I've got it to the point where I've got no reduction in framerate with 500+ sprites on a large map. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...