---
title: "可放置 CPlaceable"
description: "位置、朝向与矩阵基类"
---

---
title: 可放置 CPlaceable
description: 位置、朝向与矩阵基类
---

`CPlaceable.h`

位置 / 朝向基类。`CEntity` 继承它。有矩阵走 `m_matrix`，否则走 `m_placement`。

## 取位置

<Api name="GetPosition / m_placement / m_matrix">
无矩阵时用 simple transform。

```cpp
CSimpleTransform m_placement;
CMatrixLink* m_matrix;
// GetPosition() 内联：有 matrix 取 matrix 位，否则 m_placement.m_vPosn
```

```cpp
CVector pos = entity->GetPosition();
```
</Api>

## 设位置

<Api name="SetPosn">
只改坐标不改朝向。

```cpp
void SetPosn(float x, float y, float z);
void SetPosn(CVector const& pos);
```

```cpp
ped->SetPosn(x, y, z);
```
</Api>

## 朝向与航向

<Api name="SetHeading / GetHeading / SetOrientation">
参数为弧度。

```cpp
void SetHeading(float heading);
float GetHeading();
void SetOrientation(float x, float y, float z);
void GetOrientation(float& x, float& y, float& z);
```

```cpp
float h = ped->GetHeading();
ped->SetHeading(h + 1.57f);
```
</Api>

## 轴向

<Api name="GetRight / GetForward / GetUp">
需要已分配矩阵。

```cpp
CVector& GetRight() const;
CVector& GetForward() const;
CVector& GetUp() const;
CVector GetRightDirection();
CVector GetTopDirection();
CVector GetAtDirection();
```

```cpp
CVector fwd = ped->GetForward();
```
</Api>

## 矩阵分配

<Api name="GetMatrix / AllocateMatrix / SetMatrix">

```cpp
CMatrixLink& GetMatrix();
void AllocateMatrix();
void AllocateStaticMatrix();
void FreeStaticMatrix();
void RemoveMatrix();
void SetMatrix(CMatrix const& matrix);
```

```cpp
CMatrixLink& m = ped->GetMatrix();
// 改 m 后实体位姿跟着变；复杂场景优先走游戏 API
```
</Api>

## 区域判定

<Api name="IsWithinArea">
2D / 3D 矩形盒。

```cpp
bool IsWithinArea(float x1, float y1, float x2, float y2);
bool IsWithinArea(float x1, float y1, float z1, float x2, float y2, float z2);
```

```cpp
if (ped->IsWithinArea(minX, minY, maxX, maxY)) {
}
```
</Api>