briansemrau / godot_box2d

A C++ module that integrates the Box2D library with the Godot game engine by providing nodes for standard Box2D objects.
MIT License
45 stars 4 forks source link

Box2dPhysicsNodes and fixtures should still work in sub-scenes. #26

Closed jordo closed 3 years ago

jordo commented 3 years ago

during porting of my project, I discovered that obviously physics bodies and fixtures need to be supported in sub-scenes. i.e, the scene doesn't hold a world node, but is attached to another node which contains the world. In the screenshot below GameData scene is included in another scene (which contains the box2d world).

Screen Shot 2020-12-08 at 8 36 18 PM

Going to investigate how to support this.

briansemrau commented 3 years ago

This seems to be the primary folly of using a node to represent the physics space. Godot doesn't have this issue because it uses a physics server singleton.

Having the world as a singleton may be the only way to resolve this. We can keep Box2DWorld as a node to create spaces other than the singleton.

I believe the code to look at is https://github.com/briansemrau/godot_box2d/blob/4.0/scene/2d/box2d_physics_body.cpp#L147 (as well as in Fixture and Joint)

briansemrau commented 3 years ago

My initial guess at a solution would be to make a modification to look for the singleton world in case one is not found in the scene tree.

jordo commented 3 years ago

I dunno, i'm not 100% sure about it. I'm not a huge fan of singletons, and think there's a chance we don't need the world to do body and fixture setup.

We could also default to having at least a global world available at the root, if you never add another one. Going to investigate further, but you could be right.

jordo commented 3 years ago

IIRC The 'real root' in godot, is kinda above what you see above the scene tree (there's kinda one global root that loads scenes underneath it). I think that's the case. If that is the case, we can keep the 'World' lookup code the same (traverse up the tree until you find a world)

jordo commented 3 years ago

Alright, this should be working with https://github.com/briansemrau/godot_box2d/pull/31

briansemrau commented 3 years ago

Does this issue include the scope of running sub-scenes, even if just for testing? The current PR seems to make things better in the editor, but is limited to that.

jordo commented 3 years ago

sub-scenes should work, at least they do in my test project. I've got the body nodes in subscenes below my world

Screen Shot 2020-12-09 at 10 04 30 PM
jordo commented 3 years ago

GameData looks like this:

Screen Shot 2020-12-09 at 10 05 45 PM

I still like the warnings, cause it still give the user a warning that they need to know what they are doing for this to work

briansemrau commented 3 years ago

If you run this sub-scene, however, it won't work, right? Because there is no b2world

jordo commented 3 years ago

ya, that's correct. OK, i see what you are saying now.

jordo commented 3 years ago

So hmm, i feel like that is OK. If users set things up like this they should know what they are doing.

briansemrau commented 3 years ago

At least the warnings make it very clear. It's possible this will come up as an issue in the future, but it'll likely be handled when all the b2 stuff is put into a server. Topic for the future.

briansemrau commented 3 years ago

closing with #32 as followup to the discussion had