Files
XCEngine/docs/api/components/transform-component/transform-component.md
ssdfasd a9d5a68dd6 docs: Add Component, GameObject, TransformComponent and Scene API documentation
- Add Component class documentation with lifecycle methods
- Add GameObject class documentation with component system
- Add TransformComponent documentation with transform methods
- Add Scene class documentation with GameObject management
- Add SceneManager singleton documentation with scene loading
- Update components.md overview with all component classes
- Update main.md navigation with Scene module
2026-03-22 03:33:55 +08:00

4.3 KiB
Raw Blame History

TransformComponent

命名空间: XCEngine::Components

类型: class

头文件: XCEngine/Components/TransformComponent.h

描述: 游戏对象变换组件,管理位置、旋转、缩放和层级关系。

概述

TransformComponent 是 XCEngine ECS 系统中的变换组件,每个 GameObject 都拥有一个 TransformComponent。它管理对象的局部空间Local和世界空间World位置、旋转四元数和缩放支持父子层级变换、方向向量计算前后左右上下、矩阵运算和坐标变换。变换组件使用延迟更新机制只有在需要时才重新计算世界变换矩阵。

枚举

枚举值 描述
Space::Self 相对于自身空间
Space::World 相对于世界空间

公共方法

局部空间

方法 描述
GetLocalPosition 获取局部位置
SetLocalPosition 设置局部位置
GetLocalRotation 获取局部旋转
SetLocalRotation 设置局部旋转
GetLocalScale 获取局部缩放
SetLocalScale 设置局部缩放
GetLocalEulerAngles 获取局部欧拉角
SetLocalEulerAngles 设置局部欧拉角

世界空间

方法 描述
GetPosition 获取世界位置
SetPosition 设置世界位置
GetRotation 获取世界旋转
SetRotation 设置世界旋转
GetScale 获取世界缩放
SetScale 设置世界缩放

方向向量

方法 描述
GetForward 获取前方方向
GetRight 获取右方方向
GetUp 获取上方方向

矩阵运算

方法 描述
GetLocalToWorldMatrix 获取局部到世界矩阵
GetWorldToLocalMatrix 获取世界到局部矩阵

层级结构

方法 描述
GetParent 获取父变换
SetParent 设置父变换
GetChildCount 获取子对象数量
GetChild 获取指定索引的子变换
Find 按名称查找子变换
DetachChildren 分离所有子变换

同级顺序

方法 描述
GetSiblingIndex 获取同级索引
SetSiblingIndex 设置同级索引
SetAsFirstSibling 设为第一个同级
SetAsLastSibling 设为最后一个同级

变换操作

方法 描述
LookAt 指向目标
Rotate 旋转
Translate 平移

坐标变换

方法 描述
TransformPoint 变换点坐标
InverseTransformPoint 逆变换点坐标
TransformDirection 变换方向向量
InverseTransformDirection 逆变换方向向量

使用示例

#include <XCEngine/Components/TransformComponent.h>

using namespace XCEngine::Components;

void TransformExample(TransformComponent* transform) {
    transform->SetPosition(Vector3(10, 0, 0));
    transform->SetRotation(Quaternion::FromEuler(0, 90, 0));
    
    Vector3 forward = transform->GetForward();
    Vector3 right = transform->GetRight();
    
    transform->Translate(Vector3(0, 0, 5), Space::Self);
    transform->Rotate(Vector3(0, 45, 0), Space::World);
    
    Vector3 worldPos = transform->GetPosition();
    Matrix4x4 l2w = transform->GetLocalToWorldMatrix();
}

相关文档