diff --git a/tutorials/physics/ray-casting.rst b/tutorials/physics/ray-casting.rst index 8e17cfeb5..cde4e2180 100644 --- a/tutorials/physics/ray-casting.rst +++ b/tutorials/physics/ray-casting.rst @@ -87,13 +87,13 @@ And in 3D: .. code-tab:: gdscript GDScript func _physics_process(delta): - var space_state = get_world().direct_space_state + var space_state = get_world_3d().direct_space_state .. code-tab:: csharp public override void _PhysicsProcess(float delta) { - var spaceState = GetWorld().DirectSpaceState; + var spaceState = GetWorld3d().DirectSpaceState; } Raycast query @@ -109,7 +109,8 @@ may be used. For example: func _physics_process(delta): var space_state = get_world_2d().direct_space_state # use global coordinates, not local to node - var result = space_state.intersect_ray(Vector2(0, 0), Vector2(50, 100)) + var query = PhysicsRayQueryParameters2D.create(Vector2(0, 0), Vector2(50, 100)) + var result = space_state.intersect_ray(query) .. code-tab:: csharp @@ -117,7 +118,8 @@ may be used. For example: { var spaceState = GetWorld2d().DirectSpaceState; // use global coordinates, not local to node - var result = spaceState.IntersectRay(new Vector2(), new Vector2(50, 100)); + var query = PhysicsRayQueryParameters2D.create(new Vector2(), new Vector2(50, 100)); + var result = spaceState.IntersectRay(query); } The result is a dictionary. If the ray didn't hit anything, the dictionary will @@ -161,10 +163,9 @@ as shown in the following image: .. image:: img/raycast_falsepositive.png -To avoid self-intersection, the ``intersect_ray()`` function can take an -optional third parameter which is an array of exceptions. This is an -example of how to use it from a CharacterBody2D or any other -collision object node: +To avoid self-intersection, the ``intersect_ray()`` parameters object can take an +array of exceptions via its ``exclude`` property. This is an example of how to use it +from a CharacterBody2D or any other collision object node: .. tabs:: .. code-tab:: gdscript GDScript @@ -173,7 +174,9 @@ collision object node: func _physics_process(delta): var space_state = get_world_2d().direct_space_state - var result = space_state.intersect_ray(global_position, enemy_position, [self]) + var query = PhysicsRayQueryParameters2D.create(global_position, enemy_position) + query.exclude = [self] + var result = space_state.intersect_ray(query) .. code-tab:: csharp @@ -182,7 +185,9 @@ collision object node: public override void _PhysicsProcess(float delta) { var spaceState = GetWorld2d().DirectSpaceState; - var result = spaceState.IntersectRay(globalPosition, enemyPosition, new Godot.Collections.Array { this }); + var query = PhysicsRayQueryParameters2D.create(globalPosition, enemyPosition); + query.Exclude = new Godot.Collections.Array { this }; + var result = spaceState.IntersectRay(query); } } @@ -195,9 +200,9 @@ While the exceptions method works fine for excluding the parent body, it becomes very inconvenient if you need a large and/or dynamic list of exceptions. In this case, it is much more efficient to use the collision layer/mask system. -The optional fourth argument for ``intersect_ray()`` is a collision mask. For -example, to use the same mask as the parent body, use the ``collision_mask`` -member variable: +The ``intersect_ray()`` parameters object can also be supplied a collision mask. +For example, to use the same mask as the parent body, use the ``collision_mask`` +member variable. The array of exceptions can be supplied as the last argument as well: .. tabs:: .. code-tab:: gdscript GDScript @@ -205,9 +210,10 @@ member variable: extends CharacterBody2D func _physics_process(delta): - var space_state = get_world().direct_space_state - var result = space_state.intersect_ray(global_position, enemy_position, - [self], collision_mask) + var space_state = get_world_2d().direct_space_state + var query = PhysicsRayQueryParameters2D.create(global_position, enemy_position, + collision_mask, [self]) + var result = space_state.intersect_ray(query) .. code-tab:: csharp @@ -216,8 +222,9 @@ member variable: public override void _PhysicsProcess(float delta) { var spaceState = GetWorld2d().DirectSpaceState; - var result = spaceState.IntersectRay(globalPosition, enemyPosition, - new Godot.Collections.Array { this }, CollisionMask); + var query = PhysicsRayQueryParameters2D.create(globalPosition, enemyPosition, + CollisionMask, new Godot.Collections.Array { this }); + var result = spaceState.IntersectRay(query); } }