Skip to content

Commit 8377137

Browse files
committed
Simplify code for retrieving direction vectors
1 parent 8e69f6b commit 8377137

File tree

6 files changed

+12
-31
lines changed

6 files changed

+12
-31
lines changed

tutorials/math/vectors_advanced.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ degrees to either side:
178178
.. code-tab:: gdscript GDScript
179179

180180
# Calculate vector from `a` to `b`.
181-
var dvec = (point_b - point_a).normalized()
181+
var dvec = point_a.direction_to(point_b)
182182
# Rotate 90 degrees.
183183
var normal = Vector2(dvec.y, -dvec.x)
184184
# Alternatively (depending the desired side of the normal):
@@ -187,7 +187,7 @@ degrees to either side:
187187
.. code-tab:: csharp
188188

189189
// Calculate vector from `a` to `b`.
190-
var dvec = (pointB - pointA).Normalized();
190+
var dvec = pointA.DirectionTo(pointB);
191191
// Rotate 90 degrees.
192192
var normal = new Vector2(dvec.y, -dvec.x);
193193
// Alternatively (depending the desired side of the normal):

tutorials/navigation/navigation_introduction_2d.rst

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ NavigationServer2D and a NavigationAgent2D for path movement.
148148
var current_agent_position: Vector2 = global_position
149149
var next_path_position: Vector2 = navigation_agent.get_next_path_position()
150150

151-
var new_velocity: Vector2 = next_path_position - current_agent_position
152-
new_velocity = new_velocity.normalized()
153-
new_velocity = new_velocity * movement_speed
154-
155-
velocity = new_velocity
151+
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
156152
move_and_slide()
157153

158154
.. code-tab:: csharp C#
@@ -199,11 +195,7 @@ NavigationServer2D and a NavigationAgent2D for path movement.
199195
Vector2 currentAgentPosition = GlobalTransform.Origin;
200196
Vector2 nextPathPosition = _navigationAgent.GetNextPathPosition();
201197

202-
Vector2 newVelocity = (nextPathPosition - currentAgentPosition).Normalized();
203-
newVelocity *= _movementSpeed;
204-
205-
Velocity = newVelocity;
206-
198+
Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
207199
MoveAndSlide();
208200
}
209201

tutorials/navigation/navigation_introduction_3d.rst

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,7 @@ a NavigationAgent3D for path movement.
153153
var current_agent_position: Vector3 = global_position
154154
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
155155

156-
var new_velocity: Vector3 = next_path_position - current_agent_position
157-
new_velocity = new_velocity.normalized()
158-
new_velocity = new_velocity * movement_speed
159-
160-
velocity = new_velocity
156+
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
161157
move_and_slide()
162158

163159
.. code-tab:: csharp C#
@@ -204,11 +200,7 @@ a NavigationAgent3D for path movement.
204200
Vector3 currentAgentPosition = GlobalTransform.Origin;
205201
Vector3 nextPathPosition = _navigationAgent.GetNextPathPosition();
206202

207-
Vector3 newVelocity = (nextPathPosition - currentAgentPosition).Normalized();
208-
newVelocity *= _movementSpeed;
209-
210-
Velocity = newVelocity;
211-
203+
Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
212204
MoveAndSlide();
213205
}
214206

tutorials/navigation/navigation_using_navigationagents.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ This script adds basic navigation movement to a Node3D with a NavigationAgent3D
202202

203203
movement_delta = movement_speed * delta
204204
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
205-
var current_agent_position: Vector3 = global_position
206-
var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
205+
var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_delta
207206
if navigation_agent.avoidance_enabled:
208207
navigation_agent.set_velocity(new_velocity)
209208
else:
@@ -236,8 +235,7 @@ This script adds basic navigation movement to a CharacterBody3D with a Navigatio
236235
return
237236

238237
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
239-
var current_agent_position: Vector3 = global_position
240-
var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
238+
var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
241239
if navigation_agent.avoidance_enabled:
242240
navigation_agent.set_velocity(new_velocity)
243241
else:
@@ -271,8 +269,7 @@ This script adds basic navigation movement to a RigidBody3D with a NavigationAge
271269
return
272270

273271
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
274-
var current_agent_position: Vector3 = global_position
275-
var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
272+
var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
276273
if navigation_agent.avoidance_enabled:
277274
navigation_agent.set_velocity(new_velocity)
278275
else:

tutorials/navigation/navigation_using_navigationpaths.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,6 @@ the default navigation map by setting the target position with ``set_movement_ta
121121

122122
current_path_point = current_path[current_path_index]
123123

124-
var new_velocity: Vector3 = (current_path_point - global_transform.origin).normalized() * movement_delta
124+
var new_velocity: Vector3 = global_transform.origin.direction_to(current_path_point) * movement_delta
125125

126126
global_transform.origin = global_transform.origin.move_toward(global_transform.origin + new_velocity, movement_delta)

tutorials/physics/rigid_body.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Here is a custom ``look_at()`` method that will work reliably with rigid bodies:
3939
func look_follow(state, current_transform, target_position):
4040
var up_dir = Vector3(0, 1, 0)
4141
var cur_dir = current_transform.basis * Vector3(0, 0, 1)
42-
var target_dir = (target_position - current_transform.origin).normalized()
42+
var target_dir = current_transform.origin.direction_to(target_position)
4343
var rotation_angle = acos(cur_dir.x) - acos(target_dir.x)
4444

4545
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:
5858
{
5959
var upDir = new Vector3(0, 1, 0);
6060
var curDir = currentTransform.Basis * new Vector3(0, 0, 1);
61-
var targetDir = (targetPosition - currentTransform.Origin).Normalized();
61+
var targetDir = currentTransform.Origin.DirectionTo(targetPosition);
6262
var rotationAngle = Mathf.Acos(curDir.X) - Mathf.Acos(targetDir.X);
6363

6464
state.SetAngularVelocity(upDir * (rotationAngle / state.GetStep()));

0 commit comments

Comments
 (0)