# Find nearest object on a path?

Get help using Construct 2

### » Sun Nov 10, 2013 10:40 am

This is more of a general question.

Suppose I have a grid with walls. I have several objects within the grid. What's the best way to find which object is closest to another object based on path and not distance? I can't use the distance function because the closest object (based on actual distance) may not be the closest object due to walls, and another object may actually be 'closer' based on path.

Does anyone know of how I might accomplish this?
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

### » Sun Nov 10, 2013 11:57 am

I haven't tried it so this is just a thought, but maybe you can use the nodes in the path finding, have you tried that?
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,559

### » Sun Nov 10, 2013 12:01 pm

Can you elaborate a bit more on how I would use nodes?
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

### » Sun Nov 10, 2013 12:14 pm

If you have your tiles saved in an array, you could work with that, identify adjacent tiles, add them to another array and check those adjacent tiles for objects.

the other array could look like this:

each x -> one tile
y=0: x coordinate in tile-array
y=1: y coordinate in tile-array
y=2: status (0 = searched for object, 1 = searched for adjacent tiles)

okay let's say you are at a junction at 5,5. You run your function.
The function will return
(4,5), (6,5), (5,4), (5,6). It didn't find an object, so it will look further. For each x in the array where at(curx,2) = 0 -> run the function again.
The function should only add coordinates if they are not already in your array (to prevent from going back, searching fields twice). After every adjacent tile was checked for (4,5), its y2 is set to 1.
The search/loops are stopped once an object was found or no new adjacent tile was added to the array. After that the array is cleaned up (set size to 1,1,1 I guess).
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Sun Nov 10, 2013 12:50 pm

[QUOTE=Excal] Can you elaborate a bit more on how I would use nodes?[/QUOTE]

When path finding create a path it create nodes along those path, kinda like waypoints. You can get the position of these, which you can then use to do your tests.

As I said, I haven't tried something like what you are trying to do, so whether it works or not I don't know.

But here is a simple test of what it looks like. The green squares are nodes.

Just so you know sometimes the nodes aint showing, think that is because its probably trying to reach a point under a solid or because I made it really fast and there are a bug in it

But you should still be able to see what I mean....I hope :D

Path finding node test nimos1002013-11-10 12:54:18
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,559

### » Sun Nov 10, 2013 12:57 pm

[QUOTE=mindfaQ] If you have your tiles saved in an array, you could work with that, identify adjacent tiles, add them to another array and check those adjacent tiles for objects.

the other array could look like this:

each x -> one tile
y=0: x coordinate in tile-array
y=1: y coordinate in tile-array
y=2: status (0 = searched for object, 1 = searched for adjacent tiles)

okay let's say you are at a junction at 5,5. You run your function.
The function will return
(4,5), (6,5), (5,4), (5,6). It didn't find an object, so it will look further. For each x in the array where at(curx,2) = 0 -> run the function again.
The function should only add coordinates if they are not already in your array (to prevent from going back, searching fields twice). After every adjacent tile was checked for (4,5), its y2 is set to 1.
The search/loops are stopped once an object was found or no new adjacent tile was added to the array. After that the array is cleaned up (set size to 1,1,1 I guess).[/QUOTE]

This makes sense. I'm using a recursive function to do this, but I think I've created an endless loop, because the preview tends to crash.

The values in the BoardSpaces array contain the UID of the grid tiles. It's -1 if it doesn't exist.

My goal is to have the function recursively loop through the squares until it finds a player object, in which case ComputerTarget is set to that UID and the function should stop running. However, in my testing I get an endless loop and the program crashes.

Excal2013-11-10 13:29:12
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

### » Sun Nov 10, 2013 1:37 pm

Here's the .capx if anyone has the time to take a look. The .capx as a whole is pretty complex, but the relevant part is shown in the screenshot above and most of the events are commented.

BoardGame.capx

Excal2013-11-11 14:47:49
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

### » Mon Nov 11, 2013 12:06 pm

@Excal,

First of all, distance is a trick if you're dealing with a non static object.

But, I did the job by using instance variables, setting the objects who need to be checking against another object by storing their "Distances" inside this instance variable, then, pick the object with the higher/lower "Distance".

So:

= Every tick:
= For each Obstacle:
-> Set instance variable "Distance" to [distance(Player.x,Player.y,Obstacle.x,Obstacle.y)]

= Pick lowest "Distance" from Obstacle:
-> Do whatever you want...

Avoid making objects with the same exactly distance, or, the system will sort one of them...

If it's not possible, simple make an index, so, put inside the "Distance" a dot and plus, an index, like the IID or UID, and the system will always pick the objects with the same distance, but will select the smaller IID or UID or another instance variable, like ID, its your choice...
B
110
S
24
G
18
Posts: 1,391
Reputation: 23,017

### » Mon Nov 11, 2013 12:12 pm

@TELLES0808, I think a big WHOOSH just happened.

My first post explains how I cannot use the distance function to do this.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

### » Mon Nov 11, 2013 12:12 pm

TELLES0808: it is about distance along the path, not linear distance.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Next