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

Query functions ordered by distance #5

Closed
8bitskull opened this issue Sep 20, 2022 · 6 comments
Closed

Query functions ordered by distance #5

8bitskull opened this issue Sep 20, 2022 · 6 comments

Comments

@8bitskull
Copy link
Contributor

Would love to have either separate functions or a function option that returns hits ordered by distance.

Use case for example would be a unit seeking to target an enemy. It gets a list of enemies within its range using daabbcc.query(), and saves the first instance as a target because the table is ordered by distance.

@selimanac
Copy link
Owner

actually, I guess did this for my old project. I was searching for nearest enemy(or hero) in attack range on turn based game. It was a draft quality project but I'm going to try to find the code that I did. It might be useful.

@paweljarosz
Copy link

I believe the only struggle here would be to extract the positions for given ids from the dynamic tree to calculate those distances, right? ;)

@selimanac
Copy link
Owner

I believe the only struggle here would be to extract the positions for given ids from the dynamic tree to calculate those distances, right? ;)

Nope, this is the easiest part. It is possible to extract their center position and distance from given id. I already have those math functions. Most challenging part is the ordering the array by distance with ids. :) Unfortunately it is not simple as lua with C.

@selimanac
Copy link
Owner

Hi @8bitskull

Distance based shorting is on dev branch now. Please use this url as dependency:
https://github.com/selimanac/DAABBCC/archive/refs/heads/dev.zip

I'm going to clean up the code if works for you. I just did a few small sized tests but it is hard for me to test it on large scales as your game require.

I prefer to use separate functions since I don't want to implement an extra if-else check on the exist functions. Params are same. It is enough to add "_short" at the end:

  • aabb.query_id_short(group_id, aabb_id)
  • aabb.query_short(group_id, x, y, w, h)
  • aabb.raycast_short(group_id, start_x, start_y, end_x, end_y)

I made a change on returning results. Now, they are returning IDs and Distances. This might cause you a lot of extra work.
If so, please let me know, I can remove the distance from results and just kept the ID as before. But it might be useful(double checking) to compering the distances between those results and your Lua results:

{ --[[0x10e32b530]]
  1 = { --[[0x10e32b590]]
    id = 3,
    distance = 0
  },
  2 = { --[[0x10e32b630]]
    id = 0,
    distance = 39
  }
}

Also I updated the example(it is the same but just in case)

I hope this cause a performance gain...
I don't have high hopes but I'm going to take a look at the other issue today.

@8bitskull
Copy link
Contributor Author

Thanks very much @selimanac.

I can confirm that it works as expected. It reduces my targeting function from 24 lines to 9 lines and produces much more robust results - before, my function was guessing the closest enemy within an error margin to save on performance. Now I get the actual closest enemy.

@selimanac
Copy link
Owner

@8bitskull I made a typo on function names. All "_short" -> "_sort now. Sorry about that.
You can check out the 2.1 branch(default branch now). Also updated the readme.
I'm going to close this issue now, feel free to open a new issue if there is a problem.

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

No branches or pull requests

3 participants