 # Thread: Finding the distance between 2 identical objects

1. ## Finding the distance between 2 identical objects

Hi, I'm new to the forums. I was just wondering if anyone could provide a way to find the distance between 2 identical objects.

In other words, if you create an active object and then duplicate it, how can you find the distance between them since they are the same object?

What I want to be able to do eventually is have an object which is duplicated 10 times where the copies are moved to different random locations and find the distance between object 1 and 2, then 2 and 3, then 3 and 4 (etc.) until it calculates the distance from 9 to 10 (where each distance is stored separately).

Thanks  Reply With Quote

2. ## Re: Finding the distance between 2 identical objects

In one event you have to pick out object 1, store its coordinates in two counters. Then pick out object 2 and find its distance to the position stored in the counters - store the position of instance 2 in the counters - find instance 3 - and so on... You can even do this in a fastloop for any number of instances of objects you have.  Reply With Quote

3. ## Re: Finding the distance between 2 identical objects

It was an easy problem but I was just trying to get it all done in one event instead of splitting it up into 2 events and recording the coordinates separately. I guess I was just hoping to avoid having to use counters and get it all done with alterable values. Don't know why I didn't think of that though. Thanks!  Reply With Quote

4. ## Re: Finding the distance between 2 identical objects

Andos, could you elaborate on the solution for this? I think I may be able to use it for a similar problem I'm having with actives that are grouped by a qualifier.  Reply With Quote

5. ## Re: Finding the distance between 2 identical objects

He can probably give a better explanation, but here's a simple version of what I did to figure out my specific problem (finding distances).

So say you start off with a bunch of identical objects.

-Start of frame
+Spread value (so they can be identified separately)(I actually don't know if I've ever used spread value for all items grouped under a qualifier but I'm assuming it works)

-Have some event that starts a loop X number of times (where X is the number of objects you have minus 1)

-On loop if Alterable Value of Object = Value of Loop Counter
+Set X1 counter to X coordinate of Object
+Set Y1 counter to Y coordinate of Object

-On loop if Alterable Value of Object = Value of Loop Counter + 1
+Set X2 counter to X coordinate of Object
+Set Y2 counter to Y coordinate of Object

At this point you will have the coordinates for the first 2 identical objects. Next have some event which calculates the distance via the distance formula and save it in whatever manner you want. Then have an event that adds 1 to the Loop Counter and the loop will start again. Since the Loop Counter is 1 higher it will look at object 2 and 3 instead of 1 and 2. It will continue until all distances have been determined.

Hopefully that's not too hard to follow.  Reply With Quote

6. Could someone please elaborate on the above? I've followed gk's steps but end up with a huge mess...  Reply With Quote

7. You'll need to explain what it is you don't get. He sort of elaborated it in his post. :P  Reply With Quote

8. So, I did everything in the post and the bits that weren't explained in code I assumed were:

Always:
--> set distance(altvar) to (distance btw x1/y1, x2/y2)
--> set loopindex("loop") to loopindex("loop")+1

but this does not work. Also how do I extract the distance if I want to use it? Is this done within the loop?  Reply With Quote

9. You could save the distance between two objects into a global value if you wanted to, and then use those results however you'd like (outside of the fast loop)

Yes, setting the loop index to +1 would be done within the loop. This way it would run the loop like:
0,2,4,6,8

and it would be comparing objects 0 and 1, 2 and 3, 4 and 5, 6 and 7, 8 and 9  Reply With Quote

10. Doing this with the ForEach extension doesn't seem to work (add 1 to Current Iteration within the loop), but I'll try it with normal loops.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•