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, and should be fast.

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.

@aaronfranke aaronfranke force-pushed the col-shape-descendant branch from 6b76f08 to 063656f Compare August 31, 2023 18:01
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

@aaronfranke aaronfranke force-pushed the col-shape-descendant branch from 3fca4d6 to 103eed7 Compare March 27, 2024 02:27
@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
@aaronfranke aaronfranke force-pushed the col-shape-descendant branch from 1ee8923 to 40590ee Compare August 4, 2024 07:08
@aaronfranke aaronfranke force-pushed the col-shape-descendant branch from 40590ee to 53e6fef Compare August 31, 2024 23:49
@aaronfranke aaronfranke force-pushed the col-shape-descendant branch from 53e6fef to 64a5d59 Compare October 4, 2024 23:40
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally (rebased on top of master 04692d8), it works as expected with the following scene configuration:

image

Testing project: test_pr_77937.zip

In comparison, without this PR, none of the physics interactions work in the above testing project.

Code looks good to me.

Comment on lines +114 to +115
Transform3D transform_to_col_obj = _get_transform_to_collision_object();
if (transform_to_col_obj == transform_to_col_obj_cache) {
Copy link
Contributor

@mihe mihe Oct 17, 2024

Choose a reason for hiding this comment

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

Won't an exact comparison like this still cause this cached transform to be invalidated quite often just by the nature of floating-point precision?

Copy link
Member Author

Choose a reason for hiding this comment

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

If the transforms are exactly the same, then multiplying them will result in the exact same outputs. Within the same machine (not including network stuff), floating-point math is deterministic. If the float is slightly off due to precision issues, it will always be off by the exact same amount when the calculation is repeated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, somehow I thought the transform included that of the collision object, which it obviously doesn't, but even then you'd end up invalidating it just by virtue of the object moving around, so my concern never made much sense anyway.

Nevermind. :)

@Zylann
Copy link
Contributor

Zylann commented Oct 23, 2024

It's been a while but now I got 2 PRs to add something similar to my module, because I have nodes that need the same thing (not the physics part specifically, but rather the pattern of having child nodes that affect a common ancestor when transforms change).
I already commented about it in #77937 (comment) but got no reactions so I don't know if anyone considered it.

Today I wrote up a deeper post about how I would handle the transform notifications Zylann/godot_voxel#555 (comment)

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