Up to date

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

# 高等向量数学¶

## 平面¶

The plane passes by the origin and the surface of it is perpendicular to the unit vector (or normal). The side towards the vector points to is the positive half-space, while the other side is the negative half-space. In 3D this is exactly the same, except that the plane is an infinite surface (imagine an infinite, flat sheet of paper that you can orient and is pinned to the origin) instead of a line.

### 到平面的距离¶

```var distance = normal.dot(point)
```

### 远离原点¶

```var point_in_plane = N*D
```

```var distance = N.dot(point) - D
```

```var distance = plane.distance_to(point)
```

```N = -N
D = -D
```

Godot also implements this operator in Plane. So, using the format below will work as expected:

```var inverted_plane = -plane
```

So, remember, the plane's main practical use is that we can calculate the distance to it. So, when is it useful to calculate the distance from a point to a plane? Let's see some examples.

### 在二维空间中构造平面¶

In the case of a normal and a point, most of the work is done, as the normal is already computed, so calculate D from the dot product of the normal and the point.

```var N = normal
var D = normal.dot(point)
```

```# Calculate vector from `a` to `b`.
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):
# var normal = Vector2(-dvec.y, dvec.x)
```

The rest is the same as the previous example. Either point_a or point_b will work, as they are in the same plane:

```var N = normal
var D = normal.dot(point_a)
# this works the same
# var D = normal.dot(point_b)
```

Doing the same in 3D is a little more complex and is explained further down.

### 平面的一些示例¶

Here is an example of what planes are useful for. Imagine you have a convex polygon. For example, a rectangle, a trapezoid, a triangle, or just any polygon where no faces bend inwards.

```var inside = true
for p in planes:
# check if distance to plane is positive
if (p.distance_to(point) > 0):
inside = false
break # with one that fails, it's enough
```

```var overlapping = true

for p in planes_of_A:
var all_out = true
for v in points_of_B:
if (p.distance_to(v) < 0):
all_out = false
break

if (all_out):
# a separating plane was found
# do not continue testing
overlapping = false
break

if (overlapping):
# only do this check if no separating plane
# was found in planes of A
for p in planes_of_B:
var all_out = true
for v in points_of_A:
if (p.distance_to(v) < 0):
all_out = false
break

if (all_out):
overlapping = false
break

if (overlapping):
print("Polygons Collided!")
```

## 三维碰撞检测¶

```var overlapping = true

for p in planes_of_A:
var all_out = true
for v in points_of_B:
if (p.distance_to(v) < 0):
all_out = false
break

if (all_out):
# a separating plane was found
# do not continue testing
overlapping = false
break

if (overlapping):
# only do this check if no separating plane
# was found in planes of A
for p in planes_of_B:
var all_out = true
for v in points_of_A:
if (```