1. Vector
1-1. Dot Product
Normalize한 두 벡터에 대해, cos 값을 얻을 수 있다.
FVector A(1, 0, 0);
FVector B(0, 1, 0);
float Dot = FVector::DotProduct(A, B);
1-2. Cross Product
두 벡터의 Normal Vector를 구할 수 있다.
FVector A(1, 0, 0);
FVector B(0, 1, 0);
FVector Cross = FVector::CrossProduct(A, B);
1-3. Distance
FVector A(0, 0, 0);
FVector B(3, 4, 0);
float Distance = FVector::Distance(A, B); // Distance: 5.0
// 최적화용
float DistanceSquared = FVector::DistSquared(A, B); // DistanceSquared: 25.0
1-4. Direction
FVector A(0, 0, 0);
FVector B(1, 1, 0);
FVector Direction = (B - A).GetSafeNormal(); // A to B
1-5. Projection
FVector A(3, 4, 0);
FVector B(1, 0, 0);
FVector Projection = A.ProjectOnTo(B); // Projection: (3, 0, 0)
1-6. Mirror
FVector A(1, 2, 3);
FVector Normal(0, 1, 0);
FVector Mirrored = A.MirrorByVector(Normal); // Mirrored: (1, -2, 3)
1-7. Rotation
// Vector 축을 기준으로 회전
FVector A(1, 0, 0);
FVector Rotated = A.RotateAngleAxis(90, FVector(0, 0, 1)); // Rotated: (0, 1, 0)
------------------------------------------------------------------------------------
// Transform으로 Vector를 변환.
FTransform Transform = FTransform(FRotator(0, 90, 0));
FVector A(1, 0, 0);
FVector Transformed = Transform.TransformPosition(A); // Transformed: (0, 1, 0)
2. Rotator
2-1. Disatnce
FRotator A(45, 90, 0);
FRotator B(30, 100, 10);
float Distance = A.GetManhattanDistanceTo(B); // Distance: 25 (|45-30| + |90-100| + |0-10|)
float Distance = A.GetEuclideanDistanceTo(B); // 14.142 (approx.)
2-2. Synthesis
FRotator A(45, 90, 0);
FRotator B(10, 15, 5);
FRotator Sum = A + B; // Sum: (55, 105, 5)
FRotator Difference = A - B; // Difference: (35, 75, -5)
-------------------------------------------------------------
// A를 적용 후의, 회전 좌표계에서 B 회전 적용
FRotator A(45, 0, 0);
FRotator B(0, 90, 0);
FQuat QuatA = A.Quaternion(); // FQuat QuatA(A);
FQuat QuatB = B.Quaternion();
FQuat CombinedQuat = QuatA * QuatB;
FRotator CombinedRotator = CombinedQuat.Rotator(); // Result: (45, 90, 0)
// B To A 회전 크기
FRotator A(90, 0, 0);
FRotator B(45, 0, 0);
FQuat QuatA = A.Quaternion();
FQuat QuatB = B.Quaternion();
FQuat Difference = QuatA * QuatB.Inverse();
FRotator RelativeRotation = Difference.Rotator(); // Result: (45, 0, 0)
3. Vector To Rotator
3-1. Vector To Rotator
Roll은 항상 0이다. Vector에서 Roll을 계산할 수는 없다.
FVector Direction = FVector(1, 0, 0);
FRotator Rotation = Direction.Rotation();
3-2. 주어진 Vector가 ?축과 일치하도록 회전시키는 Rotator
MakeFromX,Y,Z
- 위에서 Vector하나의 정보로는 Roll을 계산할 수 없다고 했지만, 여기서는 기본 기준축이라는 UnrealEngine에서 기본 설정된 축을 추가로 Y축으로 설정하여, Roll에 대한 정보도 계산한다.
- 명시적으로 좌표계를 지정하고 싶다면, MakeFromXY / YZ/ ZX... 에서 추가 기준 축을 설정할 수 있다.
FVector Direction = FVector(1, 0, 0);
FRotator Rotation = FRotationMatrix::MakeFromX(Direction).Rotator();
4. Rotator To Vector
4-1. Rotator To Vector
FRotator Rotation = FRotator(0, 90, 0);
FVector Direction = Rotation.Vector();
-------------------------------------------------
FQuat Quat(FRotator(0,90,0));
FVector Direction = Quat.Vector();