Author Topic: Overlap and the issues I have therein  (Read 1333 times)

meanderingleaf

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Overlap and the issues I have therein
« on: Fri, Apr 29, 2011 »
Hello flixelators (flixilians?) I have a question regarding overlap I'm hoping you could shed some light on for me.

I have a group that I am overlapping with a single FlxSprite. However, for reasons unknown, the overlap callback is called twice. An example (with a single enemy in the group):

Code: [Select]
trace("bef")
FlxU.overlap(enmys, sm._targ, onOverlap)
trace("aft")

protected function onOverlap(e:Enemy, p:Player ):Boolean {
   trace("Overlap", e.name)
   return true //or false, doesn't matter
}

Produces this output (with enemy name set to math.random):

Code: [Select]
bef
Overlap 0.7879164693877101
Overlap 0.7879164693877101
aft

Any ideas why the callback is called twice? Thanks everyone.
« Last Edit: Fri, Apr 29, 2011 by meanderingleaf »

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Overlap and the issues I have therein
« Reply #1 on: Fri, Apr 29, 2011 »
that is strange.  my theory is that because nothing happens to the enemy or player after they overlap you are getting two overlaps:

  • enemy overlapping player
  • player overlapping enemy

i thought that it wouldn't work like that but now I'm not entirely sure... let me poke around a bit

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Overlap and the issues I have therein
« Reply #2 on: Fri, Apr 29, 2011 »
although i tlooks like you're using version 2.43, the v2.51 that's up on github dev branch should address this :)  i had a LOT of as far as i can tell COMPLETELY unnecessary colllision code in quad tree!

meanderingleaf

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Overlap and the issues I have therein
« Reply #3 on: Fri, Apr 29, 2011 »
Unneeded code! I feel you. Just cleaned up a site that was 30 files, got it down to a nice, clean 12.

I went and pulled what I believe to be Version 2.5 from https://github.com/AdamAtomic/flixel/tree/dev

Here are my results. Perhaps I'm calling the new collide function incorrectly?

Code: [Select]
trace("bef")
FlxG.overlap(sm._targ, enmys, onOverlap)
trace("aft")

protected function onOverlap(p:Player, e:Enemy):Boolean {
trace("Overlap", e.name)

return false
}

Now I get three callbacks

Quote
bef
Overlap 0.9123408296145499
Overlap 0.9123408296145499
Overlap 0.9123408296145499
aft

« Last Edit: Fri, Apr 29, 2011 by meanderingleaf »

meanderingleaf

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Overlap and the issues I have therein
« Reply #4 on: Fri, Apr 29, 2011 »
So, I dug around in the code a bit. I narrowed the multiple callbacks down to this block of code in FlxQuadTree, lines 510-518
Code: [Select]
if((_northWestTree != null) && _northWestTree.execute())
overlapProcessed = true;
if((_northEastTree != null) && _northEastTree.execute())
overlapProcessed = true;
if((_southEastTree != null) && _southEastTree.execute())
overlapProcessed = true;
if((_southWestTree != null) && _southWestTree.execute())
overlapProcessed = true;

return overlapProcessed;

Changing 'overlapProcessed = true' to 'return true' for these four cases seems to fix this, but I have no idea how it effects the overall functioning of the quad tree code.

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Overlap and the issues I have therein
« Reply #5 on: Fri, Apr 29, 2011 »
hmmm my guess is maybe the objects are on tree boundaries, so they're ending up in multiple trees.  i'm actually not entirely sure if there's a good way to deal with this from the quad-tree perspective - originally i had it so that objects were only added to a single tree no matter what, but walking that tree and doing the comparisons was prohibitively slow.

i ended up getting much better performance by just adding objects to each tree they are even partially in, and just comparing them multiple times.  those 4 if statements are the tree you're in recursing down into each of the 4 quadrants.

in practice, frequently this is not a super big problem, because usually objects that overlap are either separated, so they no longer overlap, or are !.exists or something, so those additional overlap checks don't actually come through.

i would actually prefer that the tree be structured in such a way that objects are only added once, but I would have to think really really hard about how to then walk that tree and do all the appropriate overlap checks in an efficient manner...

hope that makes sense.  i think this is a real "bug", but i'm not sure how to fix it without making the quad tree super slower...

in the meantime, if all you need to do is check the overlap between like the player and 3 enemies, you can try out the updates to FlxObject.overlaps(), which can handle and recurse groups now.  for large groups, it'll start to have performance issues, but for small groups it should be fine, and you should only get single overlap notifications.