Find nearest object on a path?

Get help using Construct 2

Post » Mon Nov 11, 2013 2:27 pm

@Excal, can you re post the link to your CAPX? The current link is broken.
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Mon Nov 11, 2013 2:47 pm

@makotto, here you go: BoardGame.capx
Project Lead of Zems Online Card Game

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

Post » Mon Nov 11, 2013 5:15 pm

@mindfaQ, I noticed after posting, thx!

@Excal,

I did a sample a time ago, for a friend, and may it match perfectly for you.

It's using pathfinder, but it don't will make any difference for you, but will make possible to check the distance.

https://dl.dropboxusercontent.com/u/47035927/Samples/Path_Distance_Demo.capx

Enjoy...

Edited:

@Ashley, I would like ask you if is possible to implement a new function inside the pathfinder behavior?

Please, consider making a "travel distance" or something similar, so, we will be able to check the shortest and longest paths and improve our AIs.

I think most of the users needing it will not achieve workarounds like on the sample above.TELLES08082013-11-11 18:12:18
Image
B
106
S
23
G
17
Posts: 1,357
Reputation: 22,137

Post » Mon Nov 11, 2013 6:42 pm

How about calculating waypoints between each pathfinding node, and finding the object closest to a waypoint:

Demo
Click any whitespace to start pathfinding.
The closest object along the path will grow/shrink.
Capx

It's not precise, but it's an extremely close approximation.zatyka2013-11-11 18:43:56
B
63
S
19
G
8
Posts: 341
Reputation: 12,130

Post » Tue Nov 12, 2013 11:59 am

@zatyka, I think the issue here is the assumption I have a pathfinding object that is moving.

Since I am trying to find the closest object that can be pathed to, I would probably have to path to all four corners of the board to determine which object is closest along any path.

Running a pathfinding object four times will take time (possible noticeable game lag) and CPU :(Excal2013-11-12 11:59:41
Project Lead of Zems Online Card Game

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

Post » Tue Nov 12, 2013 12:26 pm

Testing it here with a Galasy S3 resulted in excellent result, but on low end devices it might lag.

The pathfinder will run once when the player move, so, you can setup a popping board saying something like "Waiting movement" when the CPU usage become more than a % (you can verify it under the system variables, and compare it), like in old turn based games (Xcom Apocalypse, etc).
Image
B
106
S
23
G
17
Posts: 1,357
Reputation: 22,137

Post » Tue Nov 12, 2013 12:51 pm

@TELLES0808, how does your example apply to multiple player objects? I think your implementation is probably the best one for my usage but I'm having issues adapting it for multiple entities owned by the player.

And I definitely agree with your suggest to @Ashley.
Project Lead of Zems Online Card Game

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

Post » Tue Nov 12, 2013 1:10 pm

It can be done by setting the objects to find a path for each player entity, in order, and store the results inside an array or variable.

To store it inside a variable, first check if the result is bigger than the actual, if not, set the value, otherwise, skip and leave the variable as it is.

To store it inside arrays (and maintain a history, being able to rollback the game could be a nice feature), you can sort the cells and pick the lowest result.

Look how awesome is the effect created using array:
https://dl.dropboxusercontent.com/u/4756521/Construct%20Demos/Record%20And%20Rewind/index.html
https://www.dropbox.com/s/aisj9jgctawhyvu/TimeRecordAndRewind.capx
Image
B
106
S
23
G
17
Posts: 1,357
Reputation: 22,137

Post » Wed Nov 13, 2013 7:45 am

@TELLES0808, I have implemented something similar to what you described earlier, basing my events off of the wonderful @vee41.

However, I am having problems getting my arrays to actually fill out. Here's what happens in my events:

Each time a computer object is spawned (three spawn in my game), an instance of an array called enemyDistance is created. The UID of this array is then assigned to the arrayUID instance variable of the computer object. Since 3 computer objects are spawned, there are 3 instances of this array and each one is assigned to 1 computer object.

The enemyDistance array is set to have a height equal to the number of player objects on the board. The idea is to use pathfinding's find path to calculate the distance from the computer object to each player entity. After all the distances (the UIDs of the objects are also stored) are calculated, the game is supposed to pick the closest one (based on path and not actual distance, which is the whole point of this setup) and set it as its target.

However, right now the distances aren't being computed for some odd reason, so all the distances stored are equal to zero. Can someone take a look and help me out?



Relevant events:





BoardGame.capx
Project Lead of Zems Online Card Game

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

Post » Wed Nov 13, 2013 9:58 am

I think I almost have it. Distances are calculated now, but sometimes the game seems to hang in calculation. In other words, in the debugger I see 'calculating path' constantly change from true to false and the 'activity' variable (which tells what the object is currently doing) change from 'action' (meaning paths have been calculated) to 'path' (which is when it is calculating paths).

However, I'm not sure why there is sometimes a failure to determine distance.

Definitely agree with @TELLES0808 on the suggestion for a 'path distance' expression of some kind that will let us derive the distance from a calculated path. I'll post in the general forum as a suggestion.
Project Lead of Zems Online Card Game

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

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests