Просмотр исходного кода

Simplify code for retrieving direction vectors

Yuri Roubinski 1 год назад
Родитель
Сommit
8377137565

+ 2 - 2
tutorials/math/vectors_advanced.rst

@@ -178,7 +178,7 @@ degrees to either side:
  .. code-tab:: gdscript GDScript
 
     # Calculate vector from `a` to `b`.
-    var dvec = (point_b - point_a).normalized()
+    var dvec = point_a.direction_to(point_b)
     # Rotate 90 degrees.
     var normal = Vector2(dvec.y, -dvec.x)
     # Alternatively (depending the desired side of the normal):
@@ -187,7 +187,7 @@ degrees to either side:
  .. code-tab:: csharp
 
     // Calculate vector from `a` to `b`.
-    var dvec = (pointB - pointA).Normalized();
+    var dvec = pointA.DirectionTo(pointB);
     // Rotate 90 degrees.
     var normal = new Vector2(dvec.y, -dvec.x);
     // Alternatively (depending the desired side of the normal):

+ 2 - 10
tutorials/navigation/navigation_introduction_2d.rst

@@ -148,11 +148,7 @@ NavigationServer2D and a NavigationAgent2D for path movement.
         var current_agent_position: Vector2 = global_position
         var next_path_position: Vector2 = navigation_agent.get_next_path_position()
 
-        var new_velocity: Vector2 = next_path_position - current_agent_position
-        new_velocity = new_velocity.normalized()
-        new_velocity = new_velocity * movement_speed
-
-        velocity = new_velocity
+        velocity = current_agent_position.direction_to(next_path_position) * movement_speed
         move_and_slide()
 
  .. code-tab:: csharp C#
@@ -199,11 +195,7 @@ NavigationServer2D and a NavigationAgent2D for path movement.
             Vector2 currentAgentPosition = GlobalTransform.Origin;
             Vector2 nextPathPosition = _navigationAgent.GetNextPathPosition();
 
-            Vector2 newVelocity = (nextPathPosition - currentAgentPosition).Normalized();
-            newVelocity *= _movementSpeed;
-
-            Velocity = newVelocity;
-
+            Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
             MoveAndSlide();
         }
 

+ 2 - 10
tutorials/navigation/navigation_introduction_3d.rst

@@ -153,11 +153,7 @@ a NavigationAgent3D for path movement.
         var current_agent_position: Vector3 = global_position
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
 
-        var new_velocity: Vector3 = next_path_position - current_agent_position
-        new_velocity = new_velocity.normalized()
-        new_velocity = new_velocity * movement_speed
-
-        velocity = new_velocity
+        velocity = current_agent_position.direction_to(next_path_position) * movement_speed
         move_and_slide()
 
  .. code-tab:: csharp C#
@@ -204,11 +200,7 @@ a NavigationAgent3D for path movement.
             Vector3 currentAgentPosition = GlobalTransform.Origin;
             Vector3 nextPathPosition = _navigationAgent.GetNextPathPosition();
 
-            Vector3 newVelocity = (nextPathPosition - currentAgentPosition).Normalized();
-            newVelocity *= _movementSpeed;
-
-            Velocity = newVelocity;
-
+            Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
             MoveAndSlide();
         }
 

+ 3 - 6
tutorials/navigation/navigation_using_navigationagents.rst

@@ -202,8 +202,7 @@ This script adds basic navigation movement to a Node3D with a NavigationAgent3D
 
         movement_delta = movement_speed * delta
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_position
-        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
+        var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_delta
         if navigation_agent.avoidance_enabled:
             navigation_agent.set_velocity(new_velocity)
         else:
@@ -236,8 +235,7 @@ This script adds basic navigation movement to a CharacterBody3D with a Navigatio
             return
 
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_position
-        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
+        var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
         if navigation_agent.avoidance_enabled:
             navigation_agent.set_velocity(new_velocity)
         else:
@@ -271,8 +269,7 @@ This script adds basic navigation movement to a RigidBody3D with a NavigationAge
             return
 
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_position
-        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
+        var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
         if navigation_agent.avoidance_enabled:
             navigation_agent.set_velocity(new_velocity)
         else:

+ 1 - 1
tutorials/navigation/navigation_using_navigationpaths.rst

@@ -121,6 +121,6 @@ the default navigation map by setting the target position with ``set_movement_ta
 
         current_path_point = current_path[current_path_index]
 
-        var new_velocity: Vector3 = (current_path_point - global_transform.origin).normalized() * movement_delta
+        var new_velocity: Vector3 = global_transform.origin.direction_to(current_path_point) * movement_delta
 
         global_transform.origin = global_transform.origin.move_toward(global_transform.origin + new_velocity, movement_delta)

+ 2 - 2
tutorials/physics/rigid_body.rst

@@ -39,7 +39,7 @@ Here is a custom ``look_at()`` method that will work reliably with rigid bodies:
     func look_follow(state, current_transform, target_position):
         var up_dir = Vector3(0, 1, 0)
         var cur_dir = current_transform.basis * Vector3(0, 0, 1)
-        var target_dir = (target_position - current_transform.origin).normalized()
+        var target_dir = current_transform.origin.direction_to(target_position)
         var rotation_angle = acos(cur_dir.x) - acos(target_dir.x)
 
         state.angular_velocity = up_dir * (rotation_angle / state.step)
@@ -58,7 +58,7 @@ Here is a custom ``look_at()`` method that will work reliably with rigid bodies:
         {
             var upDir = new Vector3(0, 1, 0);
             var curDir = currentTransform.Basis * new Vector3(0, 0, 1);
-            var targetDir = (targetPosition - currentTransform.Origin).Normalized();
+            var targetDir = currentTransform.Origin.DirectionTo(targetPosition);
             var rotationAngle = Mathf.Acos(curDir.X) - Mathf.Acos(targetDir.X);
 
             state.SetAngularVelocity(upDir * (rotationAngle / state.GetStep()));