[TIP][TUT] How to smoothly turn your character

#1
I'm a beginner. I find it hard to turn player smoothly at first. At first, I just set character's current rotation to the desired rotation, thus it makes the turn look unreal when the turn angle reach greater than 90 degree. Then I discover the power of Quaternion Slerp.
Here I want to share how I smoothly turn character with Slerp.

Code: Select all

if (m_pCharacterController->IsWalking())//Third person camera, character turn when he moves.
{
//Get the face direction the character's supposed to face
Vec2 faceDir = m_pCharacterController->GetVelocity().GetNormalized();

//Convert face direction to Yaw Pitch Roll
Ang3 ypr;
ypr.x = -atan2(faceDir.x, faceDir.y);
ypr.y = 0;
ypr.z = 0;

// Set the rotation of character use Slerp
m_pEntity->SetRotation(
Quat::CreateSlerp(m_pEntity->GetRotation(), //Current rotation
Quat(CCamera::CreateOrientationYPR(ypr)),//Target rotation
frameTime * ROTATE_SPEED)//Rotate speed multiplies with frametime to make sure frame time independent
);
}
If you think this code can be optimized even more, please post it here. Thanks :)
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Who is online

Users browsing this forum: No registered users and 0 guests

cron