-2

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?

rantash68
  • 47
  • 1
  • 6

1 Answers1

1

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.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Thanks for the clarification, I was indeed confusing the two types; So my interpretation of what going on under the hood is that CollisionObject3D scans all children for those that are CollisionShape3D and add them to the physical world. when a CollisionShape3D is collided it tells/reference its parent, when of type CollisionObject3D. Is that about right? – rantash68 Jul 31 '23 at 15:09
  • 1
    @rantash68 The `CollisionObject3D` creates a physics object on the `PhysicsServer3D`. The `CollisionObject3D` also configures the physics object on the `PhysicsServer3D` with its properties, and some also set a callback that the `PhysicsServer3D` uses to notify changes including collision. The `CollisionObject3D` also scans its children for `CollisionShape3D` and adds them to physics object on the `PhysicsServer3D`. When a `CollisionShape3D` is added or removed dynamically, it notifies the `CollisionObject3D` which then updates the physics object on the `PhysicsServer3D` accordingly. – Theraot Jul 31 '23 at 15:54
  • 1
    @rantash68 If you change the properties of the `CollisionObject3D` it would update the physics object on the `PhysicsServer3D` accordingly. - During the physics frame, `PhysicsServer3D` moves, detects collisions, and resolves constrains, and uses the callbacks to notify the `CollisionObject3D` which might then update their properties or emit signals accordingly. - Take away: You can work with `PhysicsServer3D` directly, as `CollisionObject3D` and `CollisionShape3D` are an abstraction layer over it, and it might perform better since you are removing overhead. But working with `Node`s is easier. – Theraot Jul 31 '23 at 15:55