Files
XCEngine/docs/api/math/ray/ray.md
ssdfasd f5a34f8adc docs: 重构 API 文档 - components 和 scene 模块
- components: 修复英文标题为中文,添加缺失组件文档
  - 新增 camera-component, light-component, audio-source-component, audio-listener-component 类总览
  - 修复 get-position.md 格式
  - 更新 components.md 模块总览
- scene: 修复方法文档格式,新增缺失方法
  - 修复 find.md, create-game-object.md 英文标题
  - 新增 FindByID, SerializeToString, DeserializeFromString 方法文档
  - 更新 scene.md 类总览方法列表
2026-03-26 01:50:27 +08:00

1.8 KiB
Raw Blame History

Ray

命名空间: XCEngine::Math

类型: struct

头文件: XCEngine/Core/Math/Ray.h

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

概述

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

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

结构体成员

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

公共方法

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

使用示例

#include <XCEngine/Core/Math/Ray.h>
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Core/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);
}

相关文档