[UE5 / C++] ๐Ÿ’จ ์•กํ„ฐ ์›€์ง์ด๊ธฐ (ๅฎŒ)

Singery00ยท2024๋…„ 4์›” 21์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
8/20
post-thumbnail

๊ฐœ์š”

๐Ÿ’ก C++๋กœ ์•กํ„ฐ์˜ Transform์„ ์กฐ์ž‘ํ•ด๋ณด์ž.

FRotator์˜ ํ•œ๊ณ„๋กœ ์ธํ•œ ์•กํ„ฐ์˜ ํšŒ์ „์— ๋Œ€ํ•ด์„œ ๊ฐœ์„ ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. C++ ์ฝ”๋“œ๋กœ ์•กํ„ฐ ์ƒ์„ฑ
2. ์•กํ„ฐ Transfrom ์กฐ์ •
3. ์•กํ„ฐ ํšŒ์ „ ๊ฐœ์„ 

ํ•ด๋‹น ์ด์Šˆ์— ๋Œ€ํ•œ ํฌ๋Ÿผ

๋ณธ๋ก 

FRotator

FRotator๋Š” ์˜ค์ผ๋Ÿฌ ๊ฐ๋„๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ๊ณ„์‚ฐ์„ ํ•˜์—ฌ -90 ~ 90๋„ ์‚ฌ์ด๋กœ ๋ฒ”์œ„๊ฐ€ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค.

์ด์œ 

์ด๋Š” ์ง๋ฒŒ ๋ฝ (Gimbal Lock)์œผ๋กœ ์ธํ•˜์—ฌ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ง๋ฒŒ ๋ฝ (Gimbal Lock) ํฌ์ŠคํŒ…

๊ธฐ์กด ์ฝ”๋“œ

void AMyActor::Rotate(float DeltaTime)
{
	currentRotation = GetActorRotation();

	currentRotation += rotateSpeed * DeltaTime;

	SetActorRotation(currentRotation);
}

Quaternion

3์ฐจ์› ๊ทธ๋ž˜ํ”ฝ์—์„œ ํšŒ์ „์„ ํ‘œํ˜„ํ•  ๋•Œ, ํ–‰๋ ฌ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ์ˆ˜ํ•™์  ๊ฐœ๋…์œผ๋กœ 4๊ฐœ์˜ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ณต์†Œ์ˆ˜ ์ฒด๊ณ„์ž…๋‹ˆ๋‹ค.

์‚ฌ์šฉ ์ด์œ 

ํ–‰๋ ฌ์— ๋น„ํ•ด ์—ฐ์‚ฐ ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค.
์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋˜ํ•œ ์ ์Šต๋‹ˆ๋‹ค.
์˜ค๋ฅ˜๊ฐ€ ๋‚  ํ™•๋ฅ ์ด ์ ์Šต๋‹ˆ๋‹ค.

์ง๋ฒŒ๋ฝ (Gimbal Lock)

์ฟผํ„ฐ๋‹ˆ์–ธ๋„ ์ง๋ฒŒ๋ฝ(Gimbal Lock)์„ ์™„์ „ํžˆ ํ•ด๊ฒฐํ•˜์ง„ ๋ชปํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์˜ค์ผ๋Ÿฌ ๊ฐ์„ ์‚ฌ์šฉํ•ด ํšŒ์ „์„ ํ‘œํ˜„ํ•  ๋•Œ ๋ณด๋‹ค ์ง๋ฒŒ๋ฝ ๋ฌธ์ œ๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์†Œ์Šค ์ฝ”๋“œ

FRotator๋กœ ๋ฐ›์•„์˜จ ์•กํ„ฐ์˜ ์œ„์น˜๋ฅผ FQuat()๋กœ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ ํ›„ FQuat์˜ MakeFromEuler๋ฅผ ํ†ตํ•ด DeltaRotation์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
์ด๋Š” ๋ถ€๋™ ์†Œ์ˆ˜์  ์˜ค์ผ๋Ÿฌ ๊ฐ๋„์˜ ๋ฒกํ„ฐ๋ฅผ ์ฟผํ„ฐ๋‹ˆ์–ธ์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ํšŒ์ „๊ฐ’๊ณผ ์ถ”๊ฐ€ํ•  ํšŒ์ „๊ฐ’์„ ๋”ํ•œ ํ›„ SetActorRotation์„ ํ™œ์šฉํ•˜์—ฌ ์ ์šฉํ•ด์ค๋‹ˆ๋‹ค.

void AMyActor::Rotate(float DeltaTime)
{
	currentRotation = GetActorRotation();
    
	FQuat CurrentQuat = FQuat(currentRotation);

	FQuat DeltaRotation = FQuat::MakeFromEuler(FVector(rotateSpeed.Roll, rotateSpeed.Pitch, rotateSpeed.Yaw) * DeltaTime);
	FQuat NewRotation = DeltaRotation * CurrentQuat;

	SetActorRotation(NewRotation.Rotator(), ETeleportType::None);
}

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ—ํ•œ ํ›„ ํ”Œ๋ ˆ์ด๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ํ™•์ธํ•ด๋ณด๋ฉด, ์ด๋ฒˆ์—๋Š” roll๊ฐ’๋„ 360๋„๋กœ ํšŒ์ „ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋งˆ๋ฌด๋ฆฌ

์ด๊ฒƒ์œผ๋กœ ์•กํ„ฐ์˜ Location๊ณผ Rotation์„ C++์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ์ •ํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

Scale์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์ž‘์—…์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. C++ ์ฝ”๋“œ๋กœ ์•กํ„ฐ ์ƒ์„ฑ
2. ์•กํ„ฐ Transfrom ์กฐ์ •
3. ์•กํ„ฐ ํšŒ์ „ ๊ฐœ์„ 

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€