Search code examples
godotraycastinggdscript

Godot newbie puzzled by the hierarchy of nodes in regard to raycasting


I'm new to Godot, but not to OOP. I'm puzzled by something, which I guess goes to my lack of understanding of node hierarchies in general:

If raytracing intersection looks for collision objects; how come, then, it yields a parent node of the collision object? Does it somehow have to do with signals?

I looked at this example: https://kidscancode.org/godot_recipes/4.x/3d/shooting_raycasts/index.html (code at https://github.com/godotrecipes/3d_shoot_raycasts.) For example, in the node hierarchy, there is a node named "Ground" with a child named "Ground," which in turn has a child of type CollisionShape3D.

hierarchy

Although the mid level Ground node is of type StaticBody3D which means it is also a CollisionShape3D, it has the disable_mode removed, which from what I understand it does not participates in the collisions and raycastings.

inspector of Ground StaticBody3D

The code for shooting uses raycasting

    var collision = space.intersect_ray(query)
    if collision:
        $CanvasLayer/Label.text = collision.collider.name

I would have expected the raycasting collision to be with the CollisionShape3D, but "Ground" is displayed instead.

collider name

What's going on?


Solution

  • Although the mid level Ground node is of type StaticBody3D which means it is also a CollisionShape3D

    StaticBody3D derives from CollisionObject3D, not CollisionShape3D. A CollisionObject3D must have one or more CollisionShape3Ds as child nodes to describe the shape of the object. (This lets you build a complex physical shape from multiple primitive shapes.)

    I think the root of your confusion comes from this distinction. Multiple shapes combine to form a single collision object, and that collision object (in your case the StaticBody3D named Ground) is the thing that the raycast returns as the collider. However, note that you can use collision['shape'] to get the CollisionShape3D that the ray intersected with.

    it has the disable_mode removed, which from what I understand it does not participates in the collisions and raycastings.

    The disable_mode describes the behavior that the physics engine should take when the node is disabled, but it doesn't actually disable the node. So the static body is still participating in collision detection in this case.