Files
XCEngine/docs/api/math/ray/ray.md
2026-03-20 02:35:15 +08:00

1.8 KiB
Raw Blame History

Ray

命名空间: XCEngine::Math

类型: struct

头文件: XCEngine/Math/Ray.h

描述: 射线,用于 3D 拾取和光线追踪

概述

Ray 表示从原点出发沿特定方向延伸的无限射线。通过 direction 方向的归一化,确保射线方向向量为单位长度,支持精确的交点计算。广泛应用于光线追踪、碰撞拾取和视锥体裁剪等图形学场景。

注意:构造时 direction 会被自动归一化。

结构体成员

成员 类型 描述
origin Vector3 射线起点
direction Vector3 射线方向(归一化)

公共方法

方法 描述
Ray() 默认构造函数
Ray(origin, direction) 从起点和方向构造direction 会被自动归一化)
GetPoint 获取射线上 t 距离处的点
Intersects 与球体相交检测
Intersects 与有向包围盒相交检测
Intersects 与平面相交检测

使用示例

#include <XCEngine/Math/Ray.h>
#include <XCEngine/Math/Vector3.h>
#include <XCEngine/Math/Sphere.h>

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);
}

相关文档