Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NavigationServer random point queries #75098

Merged
merged 1 commit into from
Dec 11, 2023

Conversation

smix8
Copy link
Contributor

@smix8 smix8 commented Mar 19, 2023

Adds query functions to get random points on navigation mesh to the NavigationServer.

Implements godotengine/godot-proposals#6181.

Random points can be queried per navigation map or per navigation region.
Random points can be queried uniformly (costs more performance) or not.

uniform_random_point_01

@smix8 smix8 requested review from a team as code owners March 19, 2023 12:36
@AThousandShips
Copy link
Member

It looks like there's some setup each time queried, would it be worth it to allow batching this, requesting several points at once?

@smix8
Copy link
Contributor Author

smix8 commented Mar 21, 2023

@AThousandShips
I am not sure. It would complicate the API to return multiple points for something that is not really performance critical and since the query allows for a navigation_layers filter caching parts of it is properly not very efficient with all the possible variants. I would guess people would also expect more or less equally spaced random positions when requesting multiple points, something that is not intended to be covered here.

@AThousandShips
Copy link
Member

AThousandShips commented Mar 21, 2023

Agreed, saw after commenting that the area of the regions are cached which would be the only real performance bottleneck, when pondering possible implementation of this on the original proposal I was considering an idea where a query structure could be returned which contained the areas for weighted lookup (i.e. how you solve it here), but I don't think the complexity is worth it, I don't think you'd do queries like this that often

@smix8 smix8 force-pushed the map_random_point_queries_4.x branch from 1feecf8 to cf55a9e Compare April 5, 2023 12:07
@smix8 smix8 force-pushed the map_random_point_queries_4.x branch 2 times, most recently from 6f33e83 to 2652803 Compare May 12, 2023 12:18
@smix8 smix8 modified the milestones: 4.1, 4.x Jun 6, 2023
@smix8 smix8 force-pushed the map_random_point_queries_4.x branch from 2652803 to 8734ed3 Compare June 15, 2023 13:43
@smix8 smix8 force-pushed the map_random_point_queries_4.x branch 2 times, most recently from cbb5c76 to a3d9697 Compare November 20, 2023 10:27
@smix8 smix8 modified the milestones: 4.x, 4.3 Nov 20, 2023
doc/classes/NavigationServer2D.xml Outdated Show resolved Hide resolved
doc/classes/NavigationServer3D.xml Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_region.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
modules/navigation/nav_map.cpp Outdated Show resolved Hide resolved
Adds query functions to get random points on navigation mesh to the NavigationServer.
@smix8 smix8 force-pushed the map_random_point_queries_4.x branch from a3d9697 to 64a5624 Compare December 7, 2023 23:20
@AThousandShips
Copy link
Member

Will test the code when I have the time but the code looks good to me

Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, unable to do any real runtime testing myself

@akien-mga akien-mga merged commit 9eb47ce into godotengine:master Dec 11, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@smix8 smix8 deleted the map_random_point_queries_4.x branch December 17, 2023 12:31
@godotengine godotengine deleted a comment from FoxdanceMedia Jan 11, 2024
@tudor07
Copy link

tudor07 commented Jan 31, 2024

I needed this, thank you so much!

@esklarski
Copy link

Wow wow wow ❤️

Thanks to everyone involved! This was just the thing I was looking for.

@Dorifor
Copy link

Dorifor commented Oct 3, 2024

region_get_random_point() does return me a Vector3 but it's always of value (0, 0, 0), has this happened to anyone ?

map_get_random_point() does work but only like half of the time, otherwise I also get the same value as region.

edit: thanks @jadenrose for the assistance 🙏

@jadenrose
Copy link

jadenrose commented Oct 5, 2024

region_get_random_point() does return me a Vector3 but it's always of value (0, 0, 0), has this happened to anyone ?

map_get_random_point() does work but only like half of the time, otherwise I also get the same value as region.

I just ran into this issue as well! According to the docs, any procedural operations won't be ready to use until 1 physics frame after the NavigationServer is mounted, at least that's my understanding. To solve this, I deferred my call to the method that was querying the server, then at the top of that method, I waited for a get_tree().physics_frame signal. After that, the queries started to work consistently.

Example:

  func on_enter():
-   goto_random_destination()
+   goto_random_destination.call_deferred()

...

  func goto_random_destination():
+   await get_tree().physics_frame

    # ... get your map/region
    var random_point = NavigationServer3D.map_get_random_point(map, 1, false) # no longer returning (0, 0, 0)

I think what this does is ensures that 1) The method gets called on the 2nd process frame (or the end of the 1st, I'm rusty on the specifics) so all your nodes are already in the tree and 2) The method waits for the server to do its setup (which happens during the first physics frame after the initial render) before trying to query it. Presumably, you could also just wait for an arbitrary number of milliseconds before querying the first time, but I like this way because there's no possibility of a race condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants