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

59 lines
1.1 KiB
Markdown

# Ray
3D 射线结构体,用于光线投射和拾取。
## 头文件
```cpp
#include <XCEngine/Math/Ray.h>
```
## 命名空间
`XCEngine::Math`
## 结构体定义
```cpp
struct Ray {
Vector3 origin; // 射线起点
Vector3 direction; // 归一化方向
};
```
## 构造函数
- `Ray()` - 默认构造
- `Ray(const Vector3& origin, const Vector3& direction)` - 从点和方向构造
## 实例方法
| 方法 | 返回值 | 描述 |
|------|--------|------|
| `GetPoint(t)` | `Vector3` | 获取射线上 t 距离处的点: `origin + direction * t` |
## 相交检测
| 方法 | 返回值 | 描述 |
|------|--------|------|
| `Intersects(Sphere, t)` | `bool` | 与球体相交,输出距离 t |
| `Intersects(Box, t)` | `bool` | 与 AABB 相交,输出距离 t |
| `Intersects(Plane, t)` | `bool` | 与平面相交,输出距离 t |
## 使用示例
```cpp
Ray ray(cameraPosition, rayDirection);
// 与球体相交
float t;
if (ray.Intersects(sphere, t)) {
Vector3 hitPoint = ray.GetPoint(t);
}
// 与平面相交
if (ray.Intersects(plane, t)) {
Vector3 hitPoint = ray.GetPoint(t);
}
```