67 lines
1.4 KiB
Markdown
67 lines
1.4 KiB
Markdown
# Plane
|
|
|
|
3D 平面结构体,由法线和距离表示。
|
|
|
|
## 头文件
|
|
|
|
```cpp
|
|
#include <XCEngine/Math/Plane.h>
|
|
```
|
|
|
|
## 命名空间
|
|
|
|
`XCEngine::Math`
|
|
|
|
## 结构体定义
|
|
|
|
```cpp
|
|
struct Plane {
|
|
Vector3 normal = Vector3::Up(); // 单位法线
|
|
float distance = 0.0f; // 原点到平面的有符号距离
|
|
};
|
|
```
|
|
|
|
平面方程: `dot(normal, X) + distance = 0`
|
|
|
|
## 构造函数
|
|
|
|
- `Plane()` - 默认构造 (y=0 平面)
|
|
- `Plane(const Vector3& normal, float distance)` - 从法线和距离构造
|
|
|
|
## 静态工厂方法
|
|
|
|
| 方法 | 返回值 | 描述 |
|
|
|------|--------|------|
|
|
| `FromPoints(a, b, c)` | `Plane` | 从三个不共线点创建 |
|
|
|
|
## 实例方法
|
|
|
|
| 方法 | 返回值 | 描述 |
|
|
|------|--------|------|
|
|
| `GetDistanceToPoint(point)` | `float` | 点到平面的有符号距离 |
|
|
| `GetClosestPoint(point)` | `Vector3` | 平面上最接近给定点的点 |
|
|
| `GetSide(point)` | `bool` | 点在平面的哪一侧 (true=正面) |
|
|
| `Intersects(Sphere)` | `bool` | 与球体相交 |
|
|
|
|
## 使用示例
|
|
|
|
```cpp
|
|
// 创建平面
|
|
Plane floor;
|
|
floor.normal = Vector3::Up();
|
|
floor.distance = 0.0f; // y=0 平面
|
|
|
|
// 或者从三点创建
|
|
Plane plane = Plane::FromPoints(p0, p1, p2);
|
|
|
|
// 检测点在哪侧
|
|
bool above = floor.GetSide(point);
|
|
float signedDist = floor.GetDistanceToPoint(point);
|
|
|
|
// 投影点到平面
|
|
Vector3 closest = floor.GetClosestPoint(point);
|
|
|
|
// 平面相交检测
|
|
if (plane.Intersects(sphere)) { ... }
|
|
```
|