Up to date

This page is up to date for Godot `4.2`. If you still find outdated information, please open an issue.

# 向量数学¶

## 坐标系（2D）¶

2D 平面上的任何位置都可以用一对数字来表示。然而，我们也可以将位置 `(4, 3)` 看作是从 `(0, 0)` 点或原点出发的偏移。画一个箭头从原点指向点：

This is a vector. A vector represents a lot of useful information. As well as telling us that the point is at `(4, 3)`, we can also think of it as an angle `θ` (theta) and a length (or magnitude) `m`. In this case, the arrow is a position vector - it denotes a position in space, relative to the origin.

## 向量运算¶

```\$Node2D.position = Vector2(400, 300)
```

Godot supports both Vector2 and Vector3 for 2D and 3D usage, respectively. The same mathematical rules discussed in this article apply to both types, and wherever we link to `Vector2` methods in the class reference, you can also check out their `Vector3` counterparts.

### 成员访问¶

```# Create a vector with coordinates (2, 5).
var a = Vector2(2, 5)
# Create a vector and assign x and y manually.
var b = Vector2()
b.x = 3
b.y = 1
```

### 向量加法¶

```var c = a + b  # (2, 5) + (3, 1) = (5, 6)
```

### 标量乘法¶

Vectors represent both direction and magnitude. A value representing only magnitude is called a scalar. Scalars use the float type in Godot.

```var c = a * 2  # (2, 5) * 2 = (4, 10)
var d = b / 3  # (3, 6) / 3 = (1, 2)
var e = d * -2 # (1, 2) * -2 = (-2, -4)
```

Multiplying a vector by a positive scalar does not change its direction, only its magnitude. Multiplying with a negative scalar results in a vector in the opposite direction. This is how you scale a vector.

## 实际应用¶

### 移动¶

A vector can represent any quantity with a magnitude and direction. Typical examples are: position, velocity, acceleration, and force. In this image, the spaceship at step 1 has a position vector of `(1, 3)` and a velocity vector of `(2, 1)`. The velocity vector represents how far the ship moves each step. We can find the position for step 2 by adding the velocity to the current position.

Velocity measures the change in position per unit of time. The new position is found by adding the velocity multiplied by the elapsed time (here assumed to be one unit, e.g. 1 s) to the previous position.

In a typical 2D game scenario, you would have a velocity in pixels per second, and multiply it by the `delta` parameter (time elapsed since the previous frame) from the _process() or _physics_process() callbacks.

### 指向目标¶

To find a vector pointing from `A` to `B`, use `B - A`.

## 单位向量¶

### 归一化¶

Normalizing a vector means reducing its length to `1` while preserving its direction. This is done by dividing each of its components by its magnitude. Because this is such a common operation, Godot provides a dedicated normalized() method for this:

```a = a.normalized()
```

Because normalization involves dividing by the vector's length, you cannot normalize a vector of length `0`. Attempting to do so would normally result in an error. In GDScript though, trying to call the `normalized()` method on a vector of length 0 leaves the value untouched and avoids the error for you.

### 反射¶

The surface normal has a value of `(0, -1)` because this is a horizontal surface. When the ball collides, we take its remaining motion (the amount left over when it hits the surface) and reflect it using the normal. In Godot, there is a bounce() method to handle this. Here is a code example of the above diagram using a CharacterBody2D:

```var collision: KinematicCollision2D = move_and_collide(velocity * delta)
if collision:
var reflect = collision.get_remainder().bounce(collision.get_normal())
velocity = velocity.bounce(collision.get_normal())
move_and_collide(reflect)
```

## 点积¶

The mathematical notation ||A|| represents the magnitude of vector `A`, and Ax means the `x` component of vector `A`.

However, in most cases it is easiest to use the built-in dot() method. Note that the order of the two vectors does not matter:

```var c = a.dot(b)
var d = b.dot(a)  # These are equivalent.
```

The dot product is most useful when used with unit vectors, making the first formula reduce to just `cos(θ)`. This means we can use the dot product to tell us something about the angle between two vectors:

### 面向问题¶

The green arrows `fA` and `fB` are unit vectors representing the zombie's facing direction and the blue semicircle represents its field of view. For zombie `A`, we find the direction vector `AP` pointing to the player using `P - A` and normalize it, however, Godot has a helper method to do this called direction_to(). If the angle between this vector and the facing vector is less than 90°, then the zombie can see the player.

```var AP = A.direction_to(P)
if AP.dot(fA) > 0:
print("A sees P!")
```

## 叉积¶

```var c = Vector3()
c.x = (a.y * b.z) - (a.z * b.y)
c.y = (a.z * b.x) - (a.x * b.z)
c.z = (a.x * b.y) - (a.y * b.x)
```

With Godot, you can use the built-in Vector3.cross() method:

```var c = a.cross(b)
```

The cross product is not mathematically defined in 2D. The