Tell how many of object A overlap object B

Forum Home Forum Home > Construct 2 General > How do I....?
 Post Reply Post Reply
Author
4,407 Rep
Post Options Post Options   Quote SullyTheStrange Quote  Post ReplyReply Direct Link To This Post Topic: Tell how many of object A overlap object B
    Posted: 14 Feb 2012 at 9:21pm
That's what I'm trying to do. There's many object A's flying around, and a few stationary object B's. I want to tell how many A's are within a certain distance of each B at any time. It can either be within a certain distance or overlapping an invisible radius, either one will do. This is what I already tried:



That works great... if there's a single object B. Once there's more than one, the counters start flipping out and it doesn't work at all. I can see why it does that, I just don't know how to get around it... Any ideas?
All's fair in love and war, kid.
Back to Top
3,200 Rep
Post Options Post Options   Quote cacotigon Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:24pm
Can you just encapsulate this entire thing in an outer For Each Object B call? You want to check each B against the entire list of Object A.

EDIT: Keep in mind that a single object A could be in range of more than one Object B though. So if you want to account for this, you'll need to have an Object A boolean instance variable HasBeenCounted to make sure that each object A can only get "caught" once.

Edited by cacotigon - 14 Feb 2012 at 9:27pm
Back to Top
4,407 Rep
Post Options Post Options   Quote SullyTheStrange Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:27pm
Oh, duh, forgot to say I also tried that. That screencap is from a barebones capx I was using to test stuff.

The problem is that ObjectA's private var, Overlap, can only tell if it's overlapping ANY ObjectB, not a specific one. So if A is overlapping B1, and not B2, A's Overlap is 1, so for B2 it's constantly setting A's Overlap to 0 (and B1 sets it back to 1) and subtracting from its own Overlap.

I don't know if that explanation made any sense, but that's what appears to be happening.

EDIT: I see what you mean about the boolean, but I'm not sure exactly how to set it up... I'll post the test capx if anyone wants to mess with it.

Edited by SullyTheStrange - 14 Feb 2012 at 9:29pm
All's fair in love and war, kid.
Back to Top
11,750 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:29pm
+System: every tick
-> ObjectB: set Overlap to 0
+System: foreach ObjectB
+System: foreach ObjectA
    Local variable else = 1
    +System: distance(ObjectA.X,ObjectA.Y,ObjectB.X,ObjectB.Y <= 300)
      ->ObjectB: Add 1 to Overlap
      ->System: set else to 0
    +System: else = 1
    +ObjectA is overlaping Radius
      ->ObjectB: Add 1 to Overlap



There's only one issue
The radius sprite... how do you associate it to each ObjectB ?

Edited by Yann - 14 Feb 2012 at 9:35pm
Back to Top
4,407 Rep
Post Options Post Options   Quote SullyTheStrange Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:34pm
I saw your post pre-edit, Yann, and already put it in and it looks like it's working perfectly. What's the problem with the first version?
All's fair in love and war, kid.
Back to Top
11,750 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:36pm
In your attempt you don't filter specific instances of ObjectB

so the distance check will be true as soon as there's an ObjectB close...
Also System expression doesn't filter the SOL.
So it will change all the overlap variable of all instances of ObjectB

Edited by Yann - 14 Feb 2012 at 9:38pm
Back to Top
3,200 Rep
Post Options Post Options   Quote cacotigon Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:40pm
oh yeah, I see what you're getting at. Bleh, that *IS* annoying. Maybe something like this, a second boolean for keeping track if A is already in a "overlapping state" with any B this tick.

example:
For(B) --> B.Overlap = 0
For(A) --> A.AlreadyOverlapsThisCycle = false
    if A overlaps B
      B.Overlap++
      A.AlreadyOverlapsThisTick = true
    if A !overlaps B
      if !AlreadyOverlapsThisTick then A.Overlap = false



Edited by cacotigon - 14 Feb 2012 at 9:43pm
Back to Top
3,200 Rep
Post Options Post Options   Quote cacotigon Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:42pm
Yann can you explain what you mean by:
"Also System expression doesn't filter the SOL."?
Are you referring to the System distance call? Are you saying that it doesn't act on the current SOL list?
Back to Top
4,407 Rep
Post Options Post Options   Quote SullyTheStrange Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 9:46pm
Well, I dunno exactly what you mean Yann, because like I said... the first version you gave me works perfectly. It doesn't have to be absolutely flawless anyway, since it's for a minor feature in a minor game that I have to finish building for Thursday.

So uhh, thanks a bunch!
All's fair in love and war, kid.
Back to Top
3,200 Rep
Post Options Post Options   Quote cacotigon Quote  Post ReplyReply Direct Link To This Post Posted: 14 Feb 2012 at 10:03pm
Nevermind, I think Yann is referring to this from the manual:

"The System object

In Construct 2 the System object represents built-in functionality. It has no instances. This means most system conditions do not pick any instances: they are either true or false. If they are false the event stops running, otherwise the event continues without the picked instances having been changed. There are exceptions, though: if a system condition uses an object, such as Pick random instance, that will affect the picked objects.

System actions do not run on any picked objects: they simply run if all of the event's conditions were met."
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down