虚幻引擎源码-FVector解析

20 阅读1分钟

Math模块

 

using FVector = UE::Math::TVector<double>;

template<typename T>

struct TVector

{

static_assert(std::is_floating_point_v, "T must be floating point");

static_assert(std::is_floating_point_v, "T must be floating point");

public:

using FReal = T;

union

{

struct

{

/** Vector's X component. */

T X;

 

/** Vector's Y component. */

T Y;

 

/** Vector's Z component. */

T Z;

};

 

UE_DEPRECATED(all, "For internal use only")

T XYZ[3];

};

...

}

 

1.引入C++11的using关键词,代替老式typedef支持模板

2.UE::Math::TVector 实现了一个TVector的三维向量模板类TVector,要注意这里FVector使用的是double版本,在使用时存在与float的隐式转换

 

例如:

FVector Vector();

Vector.IsNearlyZero();

这里的IsNearlyZero源代码带参:

 

#define UE_KINDA_SMALL_NUMBER (1.e-4f)

bool IsNearlyZero(T Tolerance=UE_KINDA_SMALL_NUMBER) const;

template

FORCEINLINE bool TVector::IsNearlyZero(T Tolerance) const

{

    return

        FMath::Abs(X)<=Tolerance

        && FMath::Abs(Y)<=Tolerance

        && FMath::Abs(Z)<=Tolerance;

}

 

带的默认参数flaot,当使用FVector时可以不带参数,会自动隐式转换为float

如果改写 FVector: using FVector = UE::Math::TVector<int>;

FVector Vec(0,0,0);

Vec.IsNearlyZero(); // 编译报错:float(1.e-4f)无法隐式转换为int