Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Nov 16, 2016 at 03:37 AM · waypointpathfind

How to find the shortest path based on waypoints and their neigbors to reach a destination

Hello! So im want to make a game where are some nodes or planets, some planets are connected and you can send your army through them. Im stuck at implementing system that determines the shortes path from selected start planet to selected end planet. Look at screenshot from game Eufloria (pay attention to arrow) alt text

I know there is A* and Djikstra pathfinding, but i saw only how they are implemented on grid based system. How can i do that with waypoint system, noticing that, not all waypoints are connected with each other.

I figured that every planet should know its neighbors, easy. Checking if neighbor planet has at least 1 path is easy too, but how to find paths and to choose shortest is a bit confusing. And what if you have situation like this. alt text

As you can see first node from red path is closer to end goal ( heuristic if using a*), but the total blue path is more shorter. Or am i wrong, can you explain should i use pathfinding on system like this. Thanks in advance.

кщь.jpg (175.2 kB)
кщь-копия.jpg (135.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hilfor · Nov 16, 2016 at 08:46 AM

You actually can use Djikstra for this, but you'll need implement the algorithm yourself.

You need to create a graph containig all of the nodes (planets) and their connections to the other planets. If these are static nodes (i.e not moving) then the distance between every node can be calculated at the creation time using the absolute value of (transform.postition - targetNode.transform.position) and saved on every node. If the nodes are not static then the distance should be calculated every "tick" i.e on Update.

When you actually need to move you need to execute the algorithm and it should scan every possible path from the start node to the target node (planet), and then return the path to the start node.

You can checkout this video explaining Djikstra's algorithm

https://www.youtube.com/watch?v=WN3Rb9wVYDY

@UNDERHILL, this is not really a TSP (Traveling Sales Person) problem.

TSP is about connecting all of the nodes on a graph in the most optimal way possible.

The path, that we get after solving TSP, is the shortest path that connectes all of the nodes on the graph.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Nov 16, 2016 at 11:30 AM 0
Share

What kind of graph do i need?

avatar image Hilfor $$anonymous$$ · Nov 16, 2016 at 11:49 AM 0
Share

You need a logical representaion of the nodes layout in the level. Imagine you have your planets as nodes N1..N4 (for example). When you create the level, lets say that you manually set the neighbours of your nodes, for example:

  • N1 is neighbour of N2, N3

  • N2 is neighbour of N1, N3, N4

  • N3 is neighbour of N1

  • N4 is neighbour of N2, N1

Next, you need to (in code) search the level for the nodes and when creating the graph ask every node who are his neighbours and add them to a list or something. There are many ways to implement a graph structure, just google it (found a good in depth on msdn here)

I'm guessing that at some point the user will create an action involving one of the nodes (the start node) and another action with another node (the target node). So now you have the start and the end nodes, all is left for you to do is to execute the Djikstra algorithm with the start and end nodes as parameters, and voilà you have a the shortest path between the 2 nodes.

avatar image $$anonymous$$ · Nov 16, 2016 at 02:34 PM 0
Share

So graph is just a class that contains list of nodes?

avatar image Hilfor $$anonymous$$ · Nov 16, 2016 at 02:42 PM 0
Share

And the connections (if any) between them, yes

avatar image $$anonymous$$ · Nov 16, 2016 at 03:31 PM 0
Share

is this possible to store in a list more than one value for 1 index,

0 - 1,2,3

1 - 5,46

2 - 10,100,1000

or

0 - "$$anonymous$$r Clause", "Snake", "$$anonymous$$ario"

1 - "sadsdasd", "hello", "good bye"

or

0 - "0.1131231f","123.12323f" etc ?

avatar image Hilfor $$anonymous$$ · Nov 16, 2016 at 03:54 PM 0
Share

Yes, there are a number of ways you can do that. A matrix (2 dimentional array), a list of arrays, a list of hash sets or a list of any other data structure where you can store more than one value.

In general it is a bad habbit to store more than one value type in a data structure (i.e list, tree, a hash set or in this case a matrix). This will over complicate your code and introduce bugs. For example if you want to compare "hello" and 0, this will not work because one is a number (an integer to be exact) the other one is a string.

avatar image $$anonymous$$ · Nov 16, 2016 at 04:18 PM 0
Share

I dont need more than one value type in one list, i thought if list can store values in such way, that would be great to store neighbours like you listed above.

neigbors[0] = 2,3,4. So neighbours of planet 0 are 2,3,4.

avatar image
0

Answer by UNDERHILL · Nov 16, 2016 at 07:25 AM

Check out http://www.lalena.com/AI/Tsp/

This is one of the most difficult and fundamental challenges in computing.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Zodiarc · Nov 16, 2016 at 08:51 AM

You can also try the A* Algorithm or if you want something more fancy the Jump Point Search Algorithm

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Race Interfaces 0 Answers

Move an object through a set of positions 3 Answers

Facing moving direction using Waypoints 1 Answer

How do I add more waypoints to a moving platform script. 1 Answer

Smoothly rotate a rigidbody enemy 180 degrees when reaching a waypoint and path back to original waypoint 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges