# 向量数学¶

## 坐标系（2D）¶

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

## 向量运算¶

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

Godot支持 Vector2Vector3 分别用于2D和3D. 本文讨论的数学规则同样适用于这两种类型.

### 成员访问¶

```# 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)
```

### 标量乘法¶

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

## 单位向量¶

### 归一化¶

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

### 反射¶

```# object "collision" contains information about the collision
var collision = move_and_collide(velocity * delta)
if collision:
var reflect = collision.remainder.bounce(collision.normal)
velocity = velocity.bounce(collision.normal)
move_and_collide(reflect)
```

## 点积¶

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

### 面向问题¶

```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)
```

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

### 法线计算¶

```func get_triangle_normal(a, b, c):
# find the surface normal given 3 vertices
var side1 = b - a
var side2 = c - a
var normal = side1.cross(side2)
return normal
```