Godot 新手对节点层次结构与射线投射感到困惑。

huangapple go评论57阅读模式
英文:

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

问题

我是新手Godot,但不是面向对象编程(OOP)。我对某些事情感到困惑,我猜这与我对节点层次结构的理解不足有关:

如果射线跟踪交点寻找碰撞对象,为什么它会返回碰撞对象的父节点呢?这是否与信号有关?

我查看了这个示例:https://kidscancode.org/godot_recipes/4.x/3d/shooting_raycasts/index.html
(代码在https://github.com/godotrecipes/3d_shoot_raycasts中。)
例如,在节点层次结构中,有一个名为“Ground”的节点,它有一个名为“Ground”的子节点,而后者又有一个CollisionShape3D类型的子节点。

虽然中级的“Ground”节点是StaticBody3D类型的,这意味着它也是CollisionShape3D,但它已经去除了disable_mode,根据我的理解,这意味着它不参与碰撞和射线检测。

射击的代码使用了射线检测

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

我本来希望射线检测会与CollisionShape3D发生碰撞,但实际上显示的是“Ground”。

发生了什么?

英文:

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.

Godot 新手对节点层次结构与射线投射感到困惑。

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.

Godot 新手对节点层次结构与射线投射感到困惑。

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.

Godot 新手对节点层次结构与射线投射感到困惑。

What's going on?

答案1

得分: 1

尽管中级Ground节点的类型为StaticBody3D,这意味着它也是CollisionShape3D

StaticBody3D派生自CollisionObject3D,而不是CollisionShape3DCollisionObject3D必须具有一个或多个CollisionShape3D作为子节点,以描述物体的形状。(这允许您从多个基本形状构建复杂的物理形状。)

我认为你混淆的根源来自这一区别。多个形状组合成一个碰撞对象,而碰撞对象(在你的情况下是名为GroundStaticBody3D)是射线投射返回的collider。但请注意,您可以使用collision['shape']来获取射线相交的CollisionShape3D

它已经移除了disable_mode,根据我的理解,这意味着它不参与碰撞和射线投射。

disable_mode描述了在节点禁用时物理引擎应采取的行为,但它实际上并不禁用节点。因此,在这种情况下,静态体仍然参与碰撞检测。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年7月31日 22:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定