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.

@aaronfranke
Copy link
Member Author

aaronfranke commented Aug 31, 2023

@fire @lyuma I was mistaken before, I expected that there would be a challenge with using the global transform to handle the intermediate node case, but actually after investigation we can do this in a performant way with no downsides. I have updated the PR, the intermediate node case now works fine with no performance cost to the regular case.

Copy link
Member

@fire fire left a comment

Choose a reason for hiding this comment

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

For Godot Engine 4.3 I approve the design, but we need to test this and get physics team approval, as this is a physics thing.

  1. design approval
  2. physics team sync
  3. technical review

@fire fire requested a review from a team October 14, 2023 16:19
@golfinq
Copy link
Contributor

golfinq commented Oct 17, 2023

Saw a call for testing on the physics channel on rocket chat, the code looks good and tested well with a small test project I made

test_pro.zip

Copy link
Contributor

@lyuma lyuma left a comment

Choose a reason for hiding this comment

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

My comments about 2D and 3D are the same. Most of the runtime performance is pretty similar: I think it could be adjusted slightly to avoid any need to call get_parent() in the hot path if this is important, but it's not my main concern.

Basically, my main concern is the change from PARENTED to ENTER_TREE notification drastically impacts scenes instantiated in another thread or instantiated in advance of being added to the scene tree, such as level loading.

Currently (<= godot 4.2) the collision object "shape owners" are created at the moment the CollisionObject3D is parented to its CollisionShape, not when it enters the scene tree. This also impacts threaded instantiation: the CollisionObject3D used to create the shape owners on a thread in this case.

With this change, the collision shapes will be created and destroyed every time any subtree is re-parented, added or removed. In addition, especially for large scene trees / levels, this change will force Godot to wait until the tree is added to the tree to create the shapes.

scene/3d/collision_shape_3d.cpp Outdated Show resolved Hide resolved
scene/2d/collision_shape_2d.cpp Outdated Show resolved Hide resolved
Comment on lines 59 to 61
Transform2D transform_to_col_obj = get_transform();
Node *parent = get_parent();
while (parent != collision_object) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather store the child of the collision_object. then we don't need to call get_parent() repeatedly in the loop, but can just check this == child_of_collision_object.

This is the only real performance impact I can see in terms of collision_object

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't understand what you mean by this. Can you explain further, maybe write some code and push it onto a branch?

scene/2d/collision_shape_2d.cpp Outdated Show resolved Hide resolved
@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.

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
9 participants