check distance between 2 instances

Get help using Construct 2

Post » Mon Jan 02, 2012 7:21 am

Hi,

I have multiple instances of the same object moving around.
How can I check collision course or distance between 2 instances?


Thanks,
Roi
B
20
S
5
G
5
Posts: 37
Reputation: 4,072

Post » Mon Jan 02, 2012 8:53 am

Distance(instance1.x,instance1.y,instance2.x,instance2.y)
B
7
S
1
G
1
Posts: 32
Reputation: 881

Post » Mon Jan 02, 2012 10:58 am

how can i do that?
all instances have the name of the object.
B
20
S
5
G
5
Posts: 37
Reputation: 4,072

Post » Mon Jan 02, 2012 2:05 pm

Extract from the how do I FAQ

Picking/selecting instance(s)

    Picking separate instances from a same object - LINK
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Mon Jan 02, 2012 4:46 pm

Sorry I still have difficulties with this.
If I have only 2 instances I guess I can use uid 1 and 2.
What if I have multiple? which I do.
What if its random? I want to use for-each loop.

I want to check distance between 2 instances of the same object
and I have more than 2 instances of that object.
roidayan2012-01-02 16:48:17
B
20
S
5
G
5
Posts: 37
Reputation: 4,072

Post » Mon Jan 02, 2012 5:02 pm

First checking between two instances is a pain because of picking.
Also it can be pretty cpu intensive if you have many instances because you will have to check instance.count * instance.count times
But anyway it can be done with picking ID like :
Code: Select all
distance(sprite(0).x,sprite(0).y,sprite(1).x,sprite(1).y)

So I guess for distance checking it would look like
Sprite has two instance variable :
- closest   //will hold the UID of the closest instance
- dist      // will hold the distance of the instance matching the UID
Code: Select all
System: for "first" from 0 to sprite.Count-1
   System: for "second" from 0 to sprite.Count-1
   System: Pick Sprite instance loopindex("first")
      Local variable tmpDist = 0 //for clarity
      -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)
      // Check if closer
      System: Sprite(loopindex("first").dist > tmpDist
         -> Sprite: set closest to Sprite(loopindex("second)).UID
         -> Sprite: set dist to tmpDist
      // Only happen at first check
      System: loopindex("second") = 0
         -> Sprite: set closest to Sprite(loopindex("second)).UID
         -> Sprite: set dist to tmpDist

You should get the UID of the closest instance of each sprite to each other in the sprite.closest instance variable.
Last edited by Yann on Sun Sep 14, 2014 11:24 am, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Jan 02, 2012 5:22 pm

Thanks.

I actually only have 3 but I wanted to know how to do it if I have random
number of instances.
B
20
S
5
G
5
Posts: 37
Reputation: 4,072

Post » Mon Jan 02, 2012 5:24 pm

let me know if that worked :D... I didn't test that, just wrote as it came on the little quick reply thingy and then edit a bit.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon May 20, 2013 12:33 pm

@Yann I'm sorry to bump your old thread.

There something made me confuse a lot is:

"System: Sprite(loopindex("first").dist > tmpDist"

Isn't using compare two values?

I didn't understand at all, would you clarify this solution?Joannesalfa2013-05-20 12:43:32
B
96
S
25
G
20
Posts: 3,054
Reputation: 22,642

Post » Wed May 22, 2013 11:49 pm

@Joannesalfa
Hmm after carefull reading this code shouldn't work
Here is a corrected/commented version
Code: Select all
// two nested loop to compare each instances to each other
+ System: for "first" from 0 to sprite.Count-1
     Local number firstPass = 1 // tell if it's the first pass (to set default "second" as closest)
     
     + System: for "second" from 0 to sprite.Count-1
     + System: loopindex("first") != loopindex("second") // avoid comparing with itself
        Local number tmpDist = 0 // will hold the distance between the current "first" and "second" instance
        -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)

        + System: Pick Sprite instance loopindex("first")
            // first time we check this "first" instance
            // the "second" is set as the closest by default
            //  OR      
            // the current distance kept in sprite.dist is greater than the current one
            // it means the current "second" one is closer, so we overwrite the values with this one
            + System: firstPass = 1 
               OR
            + System: Sprite(loopindex("first").dist > tmpDist 
                 -> System: set firstPass to 0
                 -> Sprite: set closest to Sprite(loopindex("second)).UID
                 -> Sprite: set dist to tmpDist
It probably looks better without all those comments actually :D
Code: Select all
+ System: for "first" from 0 to sprite.Count-1
     Local number firstPass = 1
     
     + System: for "second" from 0 to sprite.Count-1
     + System: loopindex("first") != loopindex("second")
        Local number tmpDist = 0
        -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)

        + System: Pick Sprite instance loopindex("first")
            + System: firstPass = 1 
               OR
            + System: Sprite(loopindex("first").dist > tmpDist 
                 -> System: set firstPass to 0
                 -> Sprite: set closest to Sprite(loopindex("second)).UID
                 -> Sprite: set dist to tmpDist
Last edited by Yann on Sun Sep 14, 2014 11:24 am, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: Matthew de, onzephyr, signaljacker and 0 guests