78 lines
2.5 KiB
Markdown
78 lines
2.5 KiB
Markdown
# Transform
|
||
|
||
**命名空间**: `XCEngine::Math`
|
||
|
||
**类型**: `struct`
|
||
|
||
**头文件**: `XCEngine/Math/Transform.h`
|
||
|
||
**描述**: 变换组件,包含位置、旋转和缩放,用于层级变换计算
|
||
|
||
## 概述
|
||
|
||
`Transform` 是 XCEngine 中的核心变换组件,封装了 3D 空间中的位置(position)、旋转(rotation)和缩放(scale)信息。它支持层级变换计算,可以将点或方向从本地空间变换到世界空间,或进行逆变换。
|
||
|
||
该结构体设计用于游戏引擎的层级关系系统中,每个 `Transform` 可以通过 `operator*` 与父级 `Transform` 组合,实现累积变换。
|
||
|
||
## 结构体成员
|
||
|
||
| 成员 | 类型 | 描述 | 默认值 |
|
||
|------|------|------|--------|
|
||
| `position` | `Vector3` | 位置 | `Vector3::Zero()` |
|
||
| `rotation` | `Quaternion` | 旋转(四元数) | `Quaternion::Identity()` |
|
||
| `scale` | `Vector3` | 缩放 | `Vector3::One()` |
|
||
|
||
## 公共方法
|
||
|
||
| 方法 | 描述 |
|
||
|------|------|
|
||
| [`ToMatrix`](to-matrix.md) | 将变换转换为 4x4 矩阵 |
|
||
| [`Inverse`](inverse.md) | 返回变换的逆变换 |
|
||
| [`operator*`](operator-star.md) | 组合两个变换 |
|
||
| [`TransformPoint`](transform-point.md) | 变换一个点(考虑位置、旋转和缩放) |
|
||
| [`TransformDirection`](transform-direction.md) | 变换一个方向(仅考虑旋转和缩放) |
|
||
| [`InverseTransformPoint`](inverse-transform-point.md) | 逆变换一个点 |
|
||
| [`InverseTransformDirection`](inverse-transform-direction.md) | 逆变换一个方向 |
|
||
|
||
## 枚举
|
||
|
||
| 枚举 | 描述 |
|
||
|------|------|
|
||
| [`Space`](space.md) | 变换参考空间(Self 或 World) |
|
||
|
||
## 使用示例
|
||
|
||
```cpp
|
||
#include "XCEngine/Math/Transform.h"
|
||
#include "XCEngine/Math/Vector3.h"
|
||
#include "XCEngine/Math/Quaternion.h"
|
||
|
||
using namespace XCEngine::Math;
|
||
|
||
void TransformExample() {
|
||
Transform parent;
|
||
parent.position = Vector3(10.0f, 0.0f, 0.0f);
|
||
parent.rotation = Quaternion::Identity();
|
||
parent.scale = Vector3(2.0f, 2.0f, 2.0f);
|
||
|
||
Transform child;
|
||
child.position = Vector3(5.0f, 0.0f, 0.0f);
|
||
child.rotation = Quaternion::Identity();
|
||
child.scale = Vector3(1.0f, 1.0f, 1.0f);
|
||
|
||
Transform combined = parent * child;
|
||
|
||
Vector3 localPoint(1.0f, 0.0f, 0.0f);
|
||
Vector3 worldPoint = combined.TransformPoint(localPoint);
|
||
|
||
Vector3 worldDir(0.0f, 1.0f, 0.0f);
|
||
Vector3 transformedDir = combined.TransformDirection(worldDir);
|
||
|
||
Matrix4 mat = combined.ToMatrix();
|
||
}
|
||
```
|
||
|
||
## 相关文档
|
||
|
||
- [Matrix4](../matrix4/matrix4.md) - 4x4 矩阵
|