101 lines
2.9 KiB
Markdown
101 lines
2.9 KiB
Markdown
# Math 模块概览
|
|
|
|
**命名空间**: `XCEngine::Math`
|
|
|
|
**类型**: `module`
|
|
|
|
**描述**: XCEngine 的数学库模块,提供图形引擎常用的数学类型和函数。
|
|
|
|
## 概述
|
|
|
|
Math 模块提供了一套完整的图形数学库,包括向量、矩阵、四元数、变换和几何类型等。
|
|
|
|
## 模块内容
|
|
|
|
### 向量类型
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|------|------|------|
|
|
| [Vector2](./math-vector2.md) | `Vector2.h` | 二维向量 |
|
|
| [Vector3](./math-vector3.md) | `Vector3.h` | 三维向量 |
|
|
| [Vector4](./math-vector4.md) | `Vector4.h` | 四维向量 |
|
|
|
|
### 矩阵类型
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|------|------|------|
|
|
| [Matrix3](./math-matrix3.md) | `Matrix3.h` | 3x3 矩阵 |
|
|
| [Matrix4](./math-matrix4.md) | `Matrix4.h` | 4x4 矩阵 |
|
|
|
|
### 旋转/变换
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|------|------|------|
|
|
| [Quaternion](./math-quaternion.md) | `Quaternion.h` | 四元数 |
|
|
| [Transform](./math-transform.md) | `Transform.h` | 变换组件 |
|
|
|
|
### 几何类型
|
|
|
|
| 组件 | 文件 | 描述 |
|
|
|------|------|------|
|
|
| [Color](./math-color.md) | `Color.h` | 颜色 |
|
|
| [Ray](./math-ray.md) | `Ray.h` | 射线 |
|
|
| [Plane](./math-plane.md) | `Plane.h` | 平面 |
|
|
| [Sphere](./math-sphere.md) | `Sphere.h` | 球体 |
|
|
| [Box](./math-box.md) | `Box.h` | 盒子 |
|
|
| [Bounds](./math-bounds.md) | `Bounds.h` | 包围盒 |
|
|
| [AABB](./math-aabb.md) | `AABB.h` | 轴对齐包围盒 |
|
|
| [Frustum](./math-frustum.md) | `Frustum.h` | 视锥体 |
|
|
| [Rect](./math-rect.md) | `Rect.h` | 二维矩形 |
|
|
|
|
## 常量定义
|
|
|
|
| 常量 | 值 | 描述 |
|
|
|------|-----|------|
|
|
| `PI` | 3.14159265358979323846f | 圆周率 |
|
|
| `TWO_PI` | 6.28318530717958647692f | 2π |
|
|
| `HALF_PI` | 1.57079632679489661923f | π/2 |
|
|
| `DEG_TO_RAD` | PI / 180.0f | 度到弧度 |
|
|
| `RAD_TO_DEG` | 180.0f / PI | 弧度到度 |
|
|
| `EPSILON` | 1e-6f | 浮点精度 |
|
|
| `FLOAT_MAX` | 3.402823466e+38f | 浮点最大值 |
|
|
|
|
详细文档: [Math.h - 常量和辅助函数](./math-h.md)
|
|
|
|
## 辅助函数
|
|
|
|
| 函数 | 描述 |
|
|
|------|------|
|
|
| `Radians(float degrees)` | 度转弧度 |
|
|
| `Degrees(float radians)` | 弧度转度 |
|
|
|
|
## 使用示例
|
|
|
|
```cpp
|
|
#include <XCEngine/Math/Math.h>
|
|
#include <XCEngine/Math/Vector3.h>
|
|
#include <XCEngine/Math/Matrix4.h>
|
|
#include <XCEngine/Math/Quaternion.h>
|
|
|
|
using namespace XCEngine::Math;
|
|
|
|
// 向量运算
|
|
Vector3 a(1.0f, 0.0f, 0.0f);
|
|
Vector3 b(0.0f, 1.0f, 0.0f);
|
|
float dot = Vector3::Dot(a, b);
|
|
Vector3 cross = Vector3::Cross(a, b);
|
|
Vector3 normalized = Vector3::Normalize(a);
|
|
|
|
// 矩阵运算
|
|
Matrix4 model = Matrix4::TRS(position, rotation, scale);
|
|
Matrix4 view = Matrix4::LookAt(eye, target, up);
|
|
Matrix4 projection = Matrix4::Perspective(fov, aspect, near, far);
|
|
Matrix4 mvp = projection * view * model;
|
|
|
|
// 四元数运算
|
|
Quaternion q1 = Quaternion::FromEuler(0, 90, 0);
|
|
Quaternion q2 = Quaternion::FromAxisAngle(Vector3::Up(), Math::Radians(45.0f));
|
|
Quaternion combined = q1 * q2;
|
|
Vector3 rotated = q2 * Vector3::Forward();
|
|
```
|