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

Allow CollisionShape nodes to be indirect children of bodies #77937

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aaronfranke
Copy link
Member

@aaronfranke aaronfranke commented Jun 7, 2023

EDIT: Note that when this PR was first opened there were some caveats, but those have been resolved now. It should now be fully working including supporting when any node in the tree updates its transform.

Implements and closes godotengine/godot-proposals#535

This PR allows CollisionShape2D and CollisionShape3D nodes to be indirect children of bodies (CollisionObject2D and CollisionObject3D). A shape can only be connected to one body.

Screenshot 2023-06-06 at 10 03 24 PM

This is a highly demanded feature, see the discussion in godotengine/godot-proposals#535, #2174, godotengine/godot-proposals#1049, godotengine/godot-proposals#4559, godotengine/godot-proposals#5746 and https://ask.godotengine.org/31701/possible-rigidbody-have-colliders-that-not-direct-children.

Recently, Eoin from Microsoft has convinced me here that this is a vital feature. While I did debate with him about whether that's something worth standardizing... in terms of just whether the feature is good, I agree with him full stop, I don't see any reason to not have this. It just seems like a universally good idea.

The current code has the CollisionShape(2D/3D) looking for the CollisionObject(2D/3D) by running get_parent() and casting it to CollisionObject(2D/3D). So I made this a loop in the case that this cast fails, continue going up the tree until a body is found. All the code in CollisionObject(2D/3D) already works with a cached body reference and notifies it when things about the shape change like the transform.

The one caveat I can think of is that changing the transform of an intermediate node would not cause the shape to update. I don't know what the best solution to this is. We could just document that this doesn't work, or state that you would need to update the shapes manually if you do this via .update_in_shape_owner(). EDIT: This works now.

Production edit: closes godotengine/godot-roadmap#43

@aaronfranke aaronfranke added this to the 4.2 milestone Jun 7, 2023
@aaronfranke aaronfranke requested a review from a team as a code owner June 7, 2023 03:15
@aaronfranke aaronfranke changed the title Allow CollisionShape3D nodes to be indirect children of bodies Allow CollisionShape nodes to be indirect children of bodies Jun 7, 2023
@aaronfranke aaronfranke requested a review from a team as a code owner June 7, 2023 03:23
@aaronfranke aaronfranke force-pushed the col-shape-descendant branch 2 times, most recently from e635433 to e195156 Compare June 7, 2023 03:56
@aaronfranke aaronfranke requested a review from a team as a code owner June 7, 2023 05:31
scene/3d/collision_shape_3d.cpp Outdated Show resolved Hide resolved
scene/3d/collision_shape_3d.cpp Outdated Show resolved Hide resolved
scene/2d/collision_shape_2d.cpp Outdated Show resolved Hide resolved
scene/2d/collision_shape_2d.cpp Outdated Show resolved Hide resolved
scene/3d/collision_shape_3d.cpp Outdated Show resolved Hide resolved
@fire
Copy link
Member

fire commented Jun 8, 2023

What is the workaround for changing the transform of an intermediate node would not cause the shape to update we can document?

Recreate the node?

@aaronfranke
Copy link
Member Author

aaronfranke commented Jun 8, 2023

@fire Changing the transform and changing it back would work. Or maybe we should expose _update_in_shape_owner? EDIT: I have updated this PR to expose update_in_shape_owner.

@fire
Copy link
Member

fire commented Jun 8, 2023

Can't you use the NOTIFICATION_TRANSFORM_CHANGED?

@aaronfranke
Copy link
Member Author

@fire Checking the global transform would cause it to update when the body moves, which would be every frame for some bodies.

@fire
Copy link
Member

fire commented Aug 31, 2023

Was there a bandaid for changing the transform of an intermediate node would not cause the shape to update? What is the most popular behaviour for this case?

@aaronfranke
Copy link
Member Author

aaronfranke commented Aug 31, 2023

@fire Bandaid is I exposed the update method.

Another option would be to have CollisionShape3D detect when it's not a direct child, and if so, listen for the global transform changed notification in addition to local transform changed.

@lyuma
Copy link
Contributor

lyuma commented Dec 17, 2023

To clarify my comment with an actual usecase, imagine loading a level in a thread and adding it to the scene tree on the main thread, in particular, if the scene tree uses convex decomposition and primitives, rather than a single large ConcavePolygonShape3D. In large scenes this could reach into the thousands, so let's do a thought experiment on a particularly large number of collision shapes and see how this could impact things.

This example would involve instantiating a scene with 10000 collision shapes from a thread, and then later on the main thread, adding this sub-tree to the main scene tree using add_child.

Previously, this would have been pretty well optimized because most of the work of creatiing collision shape owners is done in the thread. My concern is how this change will impact the overhead of add_child in this case.

And a related concern is what happens when reparent or replace_child is used on such a subtree.

In both cases, I'm talking about an ancestor of the RigidBody3D / StaticBody3D / CollisionObject3D, such as a whole game level, not changing parents between the object and the shapes.

Perhaps one way to resolve this question is to do benchmarks on calling reparent of a large level with 10000 collision shapes to show what the performance impact is before vs after.

@aaronfranke
Copy link
Member Author

@lyuma Good catch, yes that is indeed a problem that may cause a performance regression in some projects. I have pushed a change to the PR that avoids the problem by running the code on PARENTED, and if that code successfully finds a collision object, the ENTER_TREE code will exit quite quickly.

Here is some test code:

func _ready():
	print("a")
	var test = TEST_PHYSICS.instantiate()
	print("b")
	add_child(test)
	print("c")
	remove_child(test)
	print("d")
	add_child(test)
	print("e")
	remove_child(test)
	print("f")
	var child = test.get_child(0)
	test.remove_child(child)
	print("g")
	add_child(test)
	print("h")
	test.add_child(child)
	print("i")

Here is the result when running that code, plus with some prints added into the C++ code:

a
col shape parented
col shape create shape owner in collision object
b
col shape enter tree
c
col shape remove shape owner from collision object
d
col shape enter tree
col shape create shape owner in collision object
e
col shape remove shape owner from collision object
f
g
h
col shape enter tree
col shape create shape owner in collision object
i

This should work correctly for all cases, with high performance for all common cases (direct children will be fast, instancing ahead of time then adding to the tree later will be fast).

Copy link
Contributor

@MewPurPur MewPurPur left a comment

Choose a reason for hiding this comment

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

Reviewed the code, it looks good to me.

@aaronfranke
Copy link
Member Author

Updated and re-tested this PR, still works as expected both for content made in Godot and imported from GLTF.

reduz
reduz previously approved these changes Feb 14, 2024
Copy link
Member

@reduz reduz left a comment

Choose a reason for hiding this comment

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

It looks good to me, it needs to be tested.

@reduz reduz dismissed their stale review February 14, 2024 12:47

Sorry I did not realize something

Copy link
Member

@reduz reduz left a comment

Choose a reason for hiding this comment

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

Needs a different way to receive notifications.

@reduz
Copy link
Member

reduz commented Feb 14, 2024

The main problem with this PR is that NOTIFICATION_TRANSFORM_CHANGED in this context is extemely costly, whereas the local variant has zero cost. If you have several rigid bodies moving, this will trigger the above notification every time they move, running the logic to adjust the shape in the parent body, hence incur into a very serious performance penalty.

As such I think this PR as-is is a no go, and this is the reason why this feature does not work as you would expect (performance). You would need to figure out a way to check if the intermediate nodes transform has changed without hooking up to the transform changed notification.

@aaronfranke
Copy link
Member Author

aaronfranke commented Feb 14, 2024

@reduz Yes, that is how this works. However note that it only uses the global transform changed for indirect children, it will still be fast for all existing cases of direct children.

As for "You would need to figure out a way to check if the intermediate nodes transform has changed without hooking up to the transform changed notification." I don't see how this can be done with Godot's current design.

Another option would be to just require the user to run update_in_shape_owner manually, which is how I set up this PR initially, but when I figured out I could keep the existing cases fast I thought the notification was nicer.

Also note that I would expect the common use cases of importing deeply nested colliders in GLTF would be mostly used for static level geometry where the categorization is useful. For most moving objects you usually don't have many colliders and therefore they would be direct children and fast.

@Zylann
Copy link
Contributor

Zylann commented Feb 15, 2024

Someone wanted to add a similar feature to my module Zylann/godot_voxel#558 and faced the same issue. In the current state of things there doesn't seem to be a nice way to avoid the extra costs. Checking difference from cached transforms is as close as it gets, but still relies on global transform change. I can imagine how to do that if the whole parent chain is made of classes that actually know each other (internally communicating directly in code), but not when it's any other class inheriting Node3D.

Random thought: if the tree depth of the node from which the transform change originates is passed to the notification, we could figure out whether we have to update things or not with a simple instigator_depth >= body_depth, without having to store any extra thing in any node, apart from a uint8_t in shapes to store body_depth, which can likely be done at no cost if strategically packed with other members. This would also not require to evaluate the global transform each time, it just needs to know that a parent has moved. body_depth can stay up to date if the body is reparented since the body should know its shapes, or shapes could store a pointer to it using a bit more space and indirection. Of course there are likely other possible approaches, just happened to think of that

@bbbscarter
Copy link

(Apologies for jumping in at the end of a long thread.)

I'm also hitting various limitations with having to have all collision shapes as direct children of the relevant physics bodies. An example I didn't see above; it makes it tricky to write an asset importer that generates a scene containing multiple collision shapes, without imposing a particular physicsbody root node.

Another option would be to just require the user to run update_in_shape_owner manually, which is how I set up this PR initially, but when I figured out I could keep the existing cases fast I thought the notification was nicer.

Speaking personally - I would much rather have this feature with the additional manual step, than not have it at all. Even better - would it be possible to have it 'automatic' at edit time, with the manual call only needed when there's a runtime change?

@aaronfranke
Copy link
Member Author

would it be possible to have it 'automatic' at edit time, with the manual call only needed when there's a runtime change?

That would be the case regardless, as the code to set up the physics object would run once at runtime anyway.

@krumplespiff
Copy link

I have a use case where for ragdolls I want my physical bone 3d collision shapes to inherit rotation and location but not scale since my animations change the scale of bones, but this causes issues with scaled collision shapes. I would solve this by using remote transforms and only inheriting rotation and location, but I am unable to do this with the current implementation of collision shapes needing to be direct children of the bones.

@ShirenY
Copy link
Contributor

ShirenY commented Jul 3, 2024

Recently, I encountered a blocker because I need to make some of my collision sets into 'Prefabs,' so I don't have to modify them repeatedly for the same changes. However, collisions must be direct children of bodies, making this setup impossible. This limitation is hard to swallow, as nearly all the engines I've encountered allow similar setups.

Regarding the NOTIFICATION_TRANSFORM_CHANGED issue that @reduz mentioned, I have two ideas:

  1. Relative Transform Cache in CollisionShape3D:
    Create a relative transform cache in CollisionShape3D and check it to see if a collision update is really needed before applying it to the physics part. This is a straightforward solution. The downside is the extra transform calculation and comparison every frame if the body keeps moving. However, considering how important this feature is, I'm totally fine with this approach. I would say let's implement it first and optimize it later.

  2. New Signal with NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
    Add a new signal, NOTIFICATION_LOCAL_TRANSFORM_CHANGED, to Node3D. When CollisionShape3D attaches itself to the body, it also listens to these nodes on the path between them for this signal. This callback can replace the use of NOTIFICATION_TRANSFORM_CHANGED in the current PR. In my opinion, this is the most efficient and reasonable approach. Users only pay for what they do based on how far between the body and collisions in the hierarchy and how often they change the relative transforms of collisions.

    However, I have two concerns with this solution that go beyond my knowledge:

    • First, how efficient is the signal? Will this signal introduce too much cost even when not hooked?
    • Second, does this signal align with the overall design in the engine team's roadmap? Will we have something cooler than a signal specific to dealing with transform chain change notifications?
      I hope the core team can address these questions.

@aaronfranke
Copy link
Member Author

@ShirenY Note that there is already a NOTIFICATION_LOCAL_TRANSFORM_CHANGED, and this is used when the shape is a direct child. The tricky part is in figuring out how to make this happen when any node between the body and shape changes its transform, because it currently only happens for that specific node.

As for the first idea, that's a good idea. We can skip all calls into the physics server if the relative transform is unchanged by caching it and comparing to the cache.

@ShirenY
Copy link
Contributor

ShirenY commented Jul 3, 2024

The tricky part is in figuring out how to make this happen when any node between the body and shape changes its transform

Yeah, that's exactly what 2nd solution trying to promote. Currently node only can recieve NOTIFICATION_LOCAL_TRANSFORM_CHANGED from itself (AFAIK, is it?). What I'm trying to say is make NOTIFICATION_LOCAL_TRANSFORM_CHANGED a signal like visibility_changed, so that anyone else who is interested can listen to it. So that the shape can listen to all the nodes between it and the body.

@aaronfranke aaronfranke force-pushed the col-shape-descendant branch 2 times, most recently from 1d7f169 to 1ee8923 Compare July 15, 2024 05:12
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.

Allow RigidBody to have Colliders that are indirect children, not just direct children