Files
XCEngine/docs/api/math/ray/ray.md

61 lines
1.8 KiB
Markdown
Raw Normal View History

# Ray
2026-03-20 02:35:15 +08:00
**命名空间**: `XCEngine::Math`
2026-03-20 02:35:15 +08:00
**类型**: `struct`
**头文件**: `XCEngine/Core/Math/Ray.h`
2026-03-20 02:35:15 +08:00
**描述**: 射线,用于 3D 拾取和光线追踪
2026-03-20 02:35:15 +08:00
## 概述
`Ray` 表示从原点出发沿特定方向延伸的无限射线。通过 `direction` 方向的归一化,确保射线方向向量为单位长度,支持精确的交点计算。广泛应用于光线追踪、碰撞拾取和视锥体裁剪等图形学场景。
**注意**:构造时 `direction` 会被自动归一化。
## 结构体成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `origin` | `Vector3` | 射线起点 |
| `direction` | `Vector3` | 射线方向(归一化) |
2026-03-20 02:35:15 +08:00
## 公共方法
| 方法 | 描述 |
|------|------|
2026-03-20 02:35:15 +08:00
| [`Ray()`](ray-constructor-default.md) | 默认构造函数 |
| [`Ray(origin, direction)`](ray-constructor.md) | 从起点和方向构造direction 会被自动归一化) |
| [`GetPoint`](getpoint.md) | 获取射线上 t 距离处的点 |
| [`Intersects`](intersects-sphere.md) | 与球体相交检测 |
| [`Intersects`](intersects-box.md) | 与有向包围盒相交检测 |
| [`Intersects`](intersects-plane.md) | 与平面相交检测 |
2026-03-20 02:35:15 +08:00
## 使用示例
2026-03-20 02:35:15 +08:00
```cpp
#include <XCEngine/Core/Math/Ray.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Core/Math/Sphere.h>
2026-03-20 02:35:15 +08:00
using namespace XCEngine::Math;
Ray ray(Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.0f));
Vector3 point = ray.GetPoint(5.0f); // point = (5, 0, 0)
Sphere sphere(Vector3(10.0f, 0.0f, 0.0f), 1.0f);
float t;
if (ray.Intersects(sphere, t)) {
Vector3 hitPoint = ray.GetPoint(t);
}
```
## 相关文档
2026-03-20 02:35:15 +08:00
- [Math 模块总览](../math.md) - Math 模块总览
- [Vector3](../vector3/vector3.md) - 方向和点类型
- [Sphere](../sphere/sphere.md) - 球体类型
- [Box](../box/box.md) - 包围盒类型
- [Plane](../plane/plane.md) - 平面类型