How do I find the distance to closest 'like' object

Get help using Construct 2

Post » Sat Aug 20, 2016 4:45 am

i have been able to successfully measure the closest distance to non similar objects.. can anyone point me on the right path to find the nearest similar object's distance?

For instance you have 5 eggs randomly on the screen and you want to have an instance variable for each egg that is aware of the nearest other egg. How could I do that?

distance does not play nice with 'this.x,this.y" In my example CAPX it always chooses itself as the closest.

Heuristic that worked for non-similar objects

Image

Not working: (SEE CAPX)
Image
Last edited by Gillis on Sun Aug 21, 2016 4:20 am, edited 1 time in total.
Image
B
36
S
14
G
11
Posts: 142
Reputation: 8,465

Post » Sat Aug 20, 2016 1:11 pm

You need to store the location in variables, mark it as "selected" or something with a boolean, then in the next sub-event's conditions pick all objects of the same type*, then exclude the selected object and after that add a pick closest to X,Y condition.

Please see capx as an example:

https://www.dropbox.com/s/z1df71tscepzr ... .capx?dl=1

*remember every condition picks only out of the objects exluded by the conditions before/above it, you need to make sure you are selecting out of all objects again hence why you need pick all which resets it to all, then you narrow it down to all but the selected one.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Sat Aug 20, 2016 2:33 pm

This is a great example, but I need to be able to have each object know the closest other object without the "is selected." Do you know of any way to do that?

Thanks for posting this.
Image
B
36
S
14
G
11
Posts: 142
Reputation: 8,465

Post » Sat Aug 20, 2016 6:10 pm

Gillis wrote:This is a great example, but I need to be able to have each object know the closest other object without the "is selected." Do you know of any way to do that?

Thanks for posting this.


It is the same. Just a iteration more (starting from the selected one), in a nested loop. But, lets mask 'the selecteded one' so you are not able to find it.

https://www.dropbox.com/s/siaw7iruyrpcp ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,474
Reputation: 20,912

Post » Sat Aug 20, 2016 11:55 pm

thank you. pretty nice guys
Image
B
36
S
14
G
11
Posts: 142
Reputation: 8,465


Return to How do I....?

Who is online

Users browsing this forum: bluesun66, Malhog, plinkie, Syazanie Amirin and 1 guest