Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

World Space to Local Space 본문

Unreal 4/기타

World Space to Local Space

scarecrow1992 2020. 7. 18. 01:32

actor의 GetVelocity() 함수를 이용하면 이동중인 방향을 알 수 있으나 world 좌표계 기준이다.

그러므로 이 좌표를 local 좌표를 기준으로 얻을 수 있어야 한다.

 

원리는 간단하다.

우선 GetActorRotation().Vector()로 액터가 바라보는 방향을 구한다.

FMath::Atan2(vector.Y, vector.X) 함수를 이용해서 방향벡터를 각도로 변환한다.

그리고 아래식을 참고하여 벡터 회전 변환을 실시한다

$$x'=x\cos\theta-y\sin\theta\\ 
y'=x\sin\theta+y\cos\theta$$

 

언리얼에서는 정면이 X축, 오른쪽이 Y축임을 주의하고 코드를 작성하면 된다.

 

아래 코드는 캐릭터가 이동중인 방향벡터를 월드좌표계에서 로컬좌표계로 변환하는 코드이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void AFighterCharacter::Tick(float DeltaTime)
{    
    Super::Tick(DeltaTime);
 
    FVector velocity = GetVelocity();
    FVector look = GetActorRotation().Vector();
 
    //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("X : %f         , Y : %f"), look.X, look.Y));
 
    float X_, Y_, theta, X ,Y;
 
    X = GetVelocity().X;
    Y = GetVelocity().Y;
    theta = FMath::Atan2(look.Y, look.X);
    Y_ = Y * FMath::Cos(theta) - X * FMath::Sin(theta);
    X_ = Y * FMath::Sin(theta) + X * FMath::Cos(theta);
    //GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%f"), theta));
 
    GEngine->AddOnScreenDebugMessage(-15.f, FColor::Red, FString::Printf(TEXT("X : %f         , Y : %f"), X_, Y_));
    
}
cs

 

함수 호출 몇번으로 변환이 가능한

언리얼에서 제공하는 더 간편한 방법이 있을거란 생각은 들지만 아직은 잘 모르겠다

 

 

 

'Unreal 4 > 기타' 카테고리의 다른 글

펀치 - 애니메이션 몽타주  (0) 2020.07.18
애님 에셋 리타게팅  (0) 2020.07.18
언리얼 디버그 로그 출력  (0) 2020.07.18
Spring Arm  (0) 2020.07.16
입력 매핑  (0) 2020.04.29
Comments