Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by TargonStudios · Feb 08, 2015 at 03:07 AM · javascriptpathalgorithm

Room Path Generator

So I have been trying to write a script in JavaScript, but have been pretty stumped on this for the past few hours. So pretty much, here is the scene: There are multiple rooms with varying amounts of doors that lead to other rooms. What I want, is for an AI to be able to find a path from what ever room it's in, to another room. To be able to find every door in which it needs to get through to reach it's destination room.

Here is an example along with a picture:

Let's say the AI is in room number 2, and wants to go to room 7. I need an idea for a script that calculates what doors it will need to go through to make it from room 2, to room 7. For example, it would have to take the door that leads to room 1, the door that leads to room 3, then the door that leads to room 7. (Each black line represents which door leads to which. Example: The door on the right of room 1 leads to the middle door in room 3)

alt text

I have had multiple ideas, all failing. I am asking simply for an idea, a place to start with this. I've tried for several hours, almost making it nowhere. Thanks in advance.

algorythem-problem.png (3.0 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

1 Reply

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

Answer by Alanisaac · Feb 08, 2015 at 03:12 AM

Sounds like you're looking for Pathfinding.

See http://answers.unity3d.com/questions/9025/pathfinding-tools-in-unity.html

Comment
Add comment · Show 9 · 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 TargonStudios · Feb 08, 2015 at 03:16 AM 0
Share

Not quite what I was looking for. That seems like more for a 3D environment, going around obstacles, while I'm trying to create a script that goes from one room to another. Thanks for the help though!

avatar image Alanisaac · Feb 08, 2015 at 03:29 AM 1
Share

It's still a Pathfinding problem, even if those specific libraries wouldn't be pertinent to this case.

Specifically, your case is finding the single-pair shortest path on an undirected graph with unit weight.

Edit: looks like you can use fancy algorithms (Dijkstra, Thorup), but a simple breadth-first search will do the trick here.

avatar image TargonStudios · Feb 08, 2015 at 03:46 AM 0
Share

"simple breadth-first search" Is what I was originally doing. What got me was how to store the data. Have an idea for that? (A$$anonymous$$A how to save the paths it has taken, then only use the correct one)

avatar image Alanisaac · Feb 08, 2015 at 05:01 AM 1
Share

So this SO question might be of use (though it isn't quite right) as might this blog post.

The basic concept is that as you enumerate, you store the previous node in an array. I hope the below explanation is followable.

So: say I have the graph where node 0 is connected to node 1, 2, and 3 and Node 3 is connected to nodes 4 and 5. I want the path between nodes 0 and 5.

  • I create an int[] previous array with the number of nodes in my graph (6).

  • I also create a bool[] visited of visited nodes and set all 6 to false.

  • I mark my starting node, 0 as visited with visited[0] = true;.

  • I enumerate the connections to 0 (1,2,3), and mark each as visited in the same way.

  • I store the previous node I'm co$$anonymous$$g from: previous[1] = 0; previous[2] = 0; previous[3] = 0;.

  • Then I enumerate 1 (0 is visited so do nothing)

  • Then I enumerate 2 (0 is visited so do nothing)

  • Then I enumerate 3 (4,5) and mark each as visited.

  • I store previous nodes: previous[4] = 3; previous[5] = 3;

  • Ah-ha! I found 5, which was my goal. In order to get my path back, I simply use each array value as the indexer for the next.

  • End => 5

  • previous[5] => 3

  • previous[3] => 0

  • $$anonymous$$y path is that sequence reversed: 0 - 3 - 5.

avatar image Alanisaac · Feb 08, 2015 at 02:50 PM 1
Share

It's what I saved in the array as I enumerated. Since previous[5] == 3 my next call should be to previous[3]. It does feel pretty weird to use values to index like that. You could also use a Dictionary (you said JavaScript, right? So -- think associative arrays) to store a string -> string mapping.

An example with strings might make more sense than numbers. Imagine we're solving the shortest path problem for this food chain:

food chain

I want to find the shortest path from green plant to "owl". As I enumerate the paths out of "green plant", I'm saving "green plant" as the previous value of "beetle", "grasshoppper", "wood mouse", and "snail". Like so: previous["wood mouse"] = "green plant".

Here's what the full previous[] array should look like when you're done (I enumerated from left to right):

 // Enumerating green plant...
 previous["beetle"] = "green plant"
 previous["grasshopper"] = "green plant"
 previous["wood mouse"] = "green plant"
 previous["snail"] = "green plant"
 // Enumerating beetle...
 previous["shrew"] = "beetle"
 previous["spider"] = "beetle"
 // Enumerating grasshopper... (nothing, because they're all visited already)
 // Enumerating wood mouse...
 previous["owl"] = "wood mouse"

Starting at our end point ("owl"),

 previous["owl"] = "wood mouse"

Now we use "wood mouse" as our index to find what its previous link was.

 previous["wood mouse"] = "green plant"

So our path is owl => wood mouse => green plant.

P.S. Notice how really, you're making a data structure that can be used to find the shortest path from your starting node to any end node. All you have to do is at the end of making your full enumerated paths, start at a different index in your previous array. Pretty neat!

P.P.S. I realized that what I described in my previous comment wasn't quite right. When you first encounter each node, you should mark it as visited, rather than when you begin to enumerate that node's neighbors. If you don't, and you have loops or cycles in your graph, you might run into trouble. I fixed it in the comment above.

Show more comments

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

22 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

Related Questions

Real-time day/night system breaks after noon 1 Answer

Starting with simple pathfinding with mesh 4 Answers

Viewport in the game, how to create a path~ 1 Answer

Player walk in a certain path 1 Answer

Angry Birds Space like Object Path Model 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