# check distance between 2 instances

Get help using Construct 2

### » 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
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
61
S
22
G
14
Posts: 1,480
Reputation: 16,373

### » Thu May 23, 2013 12:21 am

@Yann
EDIT:
I've cleaned your code which is messy, now it's implemented.

The result was... I don't get what is working, there no working distance each sprites.

Here is a .capx you may review.

https://dl.dropboxusercontent.com/u/76227787/LoopDistanceIncomplete.capxJoannesalfa2013-05-23 08:18:45
B
104
S
29
G
23
Posts: 3,070
Reputation: 24,781

### » Thu May 23, 2013 10:09 am

@Joannesalfa
This code is an example of how to check distance between each instance to find the closest one for each of them.
So printing any distance in a text object doesn't make any sense.
closestInstance.capx
That's what you can use it for.
You can also use the dist variable which is just the distance between the current and the closest. And it is used during the loop to get to the closest one.

Answering the question "How can I check distance between 2 instances?" is closely tied to what you want to use this comparison for. So I had to provide such case as an illustration.

(also you made a mistake, when I write "!=" it means "not equal to")
Last edited by Yann on Sun Sep 14, 2014 11:26 am, edited 1 time in total.
B
61
S
22
G
14
Posts: 1,480
Reputation: 16,373

### » Thu May 23, 2013 11:07 am

@Yann Awesome! :D

I'm sorry about "!=" it's my first time to know it means not equal, maybe if you put "=/=" it would mean in common sense.

It's useful solution for RTS games.
B
104
S
29
G
23
Posts: 3,070
Reputation: 24,781

### » Thu May 23, 2013 1:14 pm

Serious? Just get a huge 0-opacity circular sprite, spawn and pin in every instance, test collisions.
B
7
S
3
G
1
Posts: 154
Reputation: 1,812

### » Thu May 23, 2013 5:27 pm

@breackzin
How would that give you the closest instance ?
Also if you want something to be invisible you better set it to invisible instead of setting the opacity to 0 so it's not processed by the drawing function.Yann2013-05-23 17:29:00
B
61
S
22
G
14
Posts: 1,480
Reputation: 16,373

### » Thu May 23, 2013 5:39 pm

Pick nearer.
Didn't know about the process of 0-opacity. Gonna use invisibility more often, thanks.
B
7
S
3
G
1
Posts: 154
Reputation: 1,812

### » Thu May 23, 2013 7:11 pm

@breackzin
Indeed you're right, you can do the same thing using pick nearest.
But still a circular sprite won't really be usefull here
Here is how it looks with pick nearest:
nearestInstance.capx
And if you know a bit how it works internally, this version is a bit less efficient than the one I provided.

And also, since the question was "how to compare distance between 2 instances", my code was merely an example of a specific use case.
You can do other interesting things like range checking or other fancy stuff (:Yann2013-05-23 19:13:29
B
61
S
22
G
14
Posts: 1,480
Reputation: 16,373

Previous