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

Automate position updates (track game object position automatically) #6

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

Comments

@8bitskull
Copy link
Sponsor Contributor

I use go.animate() to move a lot of objects in a performant way. However I need to constantly update the aabb position for these objects, which means go.get_position() followed by aabb.update(). In my test, doing lots of updates was worse than the collision checking queries.

Britzl on Defold forums:
"The dmGameObject namespace has getPosition() but it requires a dmGameObject::HInstance. Not sure if there is a way to pass ids from Lua and turn them into dmGameObject::HInstance. If there is then you could provide the extension with a bunch of ids to keep updated and in the extension update function you could get the positions and update the aabb’s. @JCash will know if this is possible or not."

@selimanac
Copy link
Owner

Yes this could be very very useful a specially on performance side.
But as far as I know it is not possible to pass gameobject instance to C yet:
https://forum.defold.com/t/defold-1-2-181-has-been-released/68048/22?u=selimanac

@8bitskull
Copy link
Sponsor Contributor Author

Good news, I asked JCash and he says it is implemented:

https://defold.com/ref/stable/dmGameObject/#GetPosition:instance

Is this what is needed?

@selimanac
Copy link
Owner

selimanac commented Sep 20, 2022

Not sure. I guess this is only for GameObject which is created on C side.
If we are able to pass dmGameObject::HInstance from Defold(lua) to C then yes. I had a quick review on API docs and cant find anything related to getting instance. Maybe I am missing...

@selimanac
Copy link
Owner

There is GetInstanceFromIdentifieron gameobject.h but looks like it is not documented. This might be it.
https://github.com/defold/defold/blob/dev/engine/gameobject/src/dmsdk/gameobject/gameobject.h#L545

@selimanac
Copy link
Owner

@selimanac
Copy link
Owner

@8bitskull are you still using this on Void Scrappers? If so and you got time, I'm going to test this. Since it is not a released function I can't promise for making it work.

@8bitskull
Copy link
Sponsor Contributor Author

@8bitskull are you still using this on Void Scrappers? If so and you got time, I'm going to test this. Since it is not a released function I can't promise for making it work.

Yes I am, will gladly help in testing.

@selimanac
Copy link
Owner

selimanac commented Sep 20, 2022

@8bitskull Ok. Then please give me a few days. I got a project release this week. I'm going to try to figure this out as soon as possible.

@selimanac
Copy link
Owner

@8bitskull +also maybe I can prepare a much more custom version for the game. Which doesn't require checking collision on every frame(maybe just using callback). I can't promise, but I want to contribute on this if I had time.

@8bitskull
Copy link
Sponsor Contributor Author

Thank you very much @selimanac. I have managed to create a pretty good system by batching updates, so I think a custom version of the extension is not quite necessary. The extension itself is already massively useful - if you manage to implement the two issues I have raised it will be even more so and I will be able to use it in lots of future projects I have in mind as well!

...Well, we'll see how the game performs when I try to port it to Switch. But I think it will be my code that is the bottleneck, not this extension.

@selimanac
Copy link
Owner

@8bitskull I figure that out and it is possible to get position from C now. But require a more work on extension side. I will had to keep all the go instances, get their positions and maybe trigger a callback. This is going to be quite big change on the internals. I hope I can manage it on the next week.

I am planning to use callbacks because it will save a lots of performance. Not just for your project but every other projects...

@8bitskull
Copy link
Sponsor Contributor Author

Good luck! Please don't rush on my account, the demo performs adequately and full release is many weeks away.

Regardless, I look forward to seeing the result!

@selimanac
Copy link
Owner

selimanac commented Oct 4, 2022

@8bitskull Update: unfortunately I couldn't work on this last week. But I started today. My initial plan was the re-wrote the all thing but it seem like it's gonna take a long time. So I decided to go with a patch. We will have to do a lots of test :)

@selimanac
Copy link
Owner

@8bitskull Update: Almost ready for testing. Since I made a lots of critical changes I want to test it as much as I can. I'm going to build a simple game to see if everything works fine before send it to you. It is going to take a few more days I presume.

@selimanac
Copy link
Owner

Hi @8bitskull ,
I think it is ready now. I made a small game by using new version and new functions.
I did try to test every possible scenario as much as I can think of. It seems pretty stable.

I'll wait until you have been test this and when everything is fine then I'm going to release a build. It is here for now:
https://github.com/selimanac/DAABBCC/archive/refs/heads/v2.1.zip

There are two major functions related to Gameobjects:

  • aabb.insert_gameobject(group_id, url, w, h)
local go_url = msg.url("/go")
local w = 50
local h = 50

local aabb_id = aabb.insert_gameobject(enemy_group, go_url, w, h)
  • aabb.remove_gameobject(group_id, aabb_id)

This is the recomended way of removing Gameobject and its AABB.
But also it is safe to use aabb.remove, it is going to search and remove any Gameobject related to group id and proxy id.

These are pretty straight forward but you can check out the example project which I just develop if you need to. Also updated the example on this repo:
https://github.com/selimanac/space-shooter-daabbcc

There is two new more functions:

  • aabb.run(boolean) It is for stop/resume Gameobject iteration(get/set position) on update. It might be useful when pausing game. It might not effect much but give your frame/cpu a more time.
  • aabb.clear() is for cleaning everything(Groups, proxies, gameobjects). It is best to use it when collection is unloaded(when game is over and game collection is unloaded).

There is more on release notes.

I'm going to close this issue. Feel free to open new one...

@8bitskull
Copy link
Sponsor Contributor Author

Excellent work @selimanac , thank you very much. Will let you know if I encounter any issues.

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

2 participants