美文网首页
UE4 CharacterMovementComponent 之

UE4 CharacterMovementComponent 之

作者: 我真的不知道该起什么名字了 | 来源:发表于2019-06-14 17:59 被阅读0次
void UCharacterMovementComponent::PerformMovement(float DeltaSeconds)
{
  .....
        // 2162行, 如果通过LaunchCharacter 进行了速度设定, 直接替换成该速度, 
        // 并且不会进行最大速度限制的检测, 即你输入的是什么, 就设置为什么.
        // Character::LaunchCharacter() has been deferred until now.
        HandlePendingLaunch();


        // change position  2302行, 这里进行位置的更新
        StartNewPhysics(DeltaSeconds, 0);

               //  2314行, 这里进行旋转的更新.
        if ((bAllowPhysicsRotationDuringAnimRootMotion || !HasAnimRootMotion()) && !CharacterOwner->IsMatineeControlled())
        {
            PhysicsRotation(DeltaSeconds);
        }
}

整个旋转计算过程, 这里有个bug, 当加速度Acceleration为(0, 0, z)格式的时候, 这里会把Character朝向X轴方向. 所以加了修正代码

void UCharacterMovementComponent::PhysicsRotation(float DeltaTime)
{
    if (!(bOrientRotationToMovement || bUseControllerDesiredRotation))
    {
        return;
    }

    if (!HasValidData() || (!CharacterOwner->Controller && !bRunPhysicsWithNoController))
    {
        return;
    }

    FRotator CurrentRotation = UpdatedComponent->GetComponentRotation(); // Normalized
    CurrentRotation.DiagnosticCheckNaN(TEXT("CharacterMovementComponent::PhysicsRotation(): CurrentRotation"));

    FRotator DeltaRot = GetDeltaRotation(DeltaTime);
    DeltaRot.DiagnosticCheckNaN(TEXT("CharacterMovementComponent::PhysicsRotation(): GetDeltaRotation"));

    FRotator DesiredRotation = CurrentRotation;
    if (bOrientRotationToMovement)
    {
        DesiredRotation = ComputeOrientToMovementRotation(CurrentRotation, DeltaTime, DeltaRot);
    }
    else if (CharacterOwner->Controller && bUseControllerDesiredRotation)
    {
        DesiredRotation = CharacterOwner->Controller->GetDesiredRotation();
    }
    else
    {
        return;
    }

    if (ShouldRemainVertical())
    {
        DesiredRotation.Pitch = 0.f;
        DesiredRotation.Yaw = FRotator::NormalizeAxis(DesiredRotation.Yaw);
        DesiredRotation.Roll = 0.f;
    }
    else
    {
        if (Acceleration.X==0.f && Acceleration.Y==0.f) {
           // 当加速度格式为(0, 0, z)的时候, Character朝向不变
           DesiredRotation = CurrentRotation ;
        }
        DesiredRotation.Normalize();
    }
    
    // Accumulate a desired new rotation.
    const float AngleTolerance = 1e-3f;

    if (!CurrentRotation.Equals(DesiredRotation, AngleTolerance))
    {
        // PITCH
        if (!FMath::IsNearlyEqual(CurrentRotation.Pitch, DesiredRotation.Pitch, AngleTolerance))
        {
            DesiredRotation.Pitch = FMath::FixedTurn(CurrentRotation.Pitch, DesiredRotation.Pitch, DeltaRot.Pitch);
        }

        // YAW
        if (!FMath::IsNearlyEqual(CurrentRotation.Yaw, DesiredRotation.Yaw, AngleTolerance))
        {
            DesiredRotation.Yaw = FMath::FixedTurn(CurrentRotation.Yaw, DesiredRotation.Yaw, DeltaRot.Yaw);
        }

        // ROLL
        if (!FMath::IsNearlyEqual(CurrentRotation.Roll, DesiredRotation.Roll, AngleTolerance))
        {
            DesiredRotation.Roll = FMath::FixedTurn(CurrentRotation.Roll, DesiredRotation.Roll, DeltaRot.Roll);
        }

        // Set the new rotation.
        DesiredRotation.DiagnosticCheckNaN(TEXT("CharacterMovementComponent::PhysicsRotation(): DesiredRotation"));
        MoveUpdatedComponent( FVector::ZeroVector, DesiredRotation, /*bSweep*/ false );
    }
}

相关文章

网友评论

      本文标题:UE4 CharacterMovementComponent 之

      本文链接:https://www.haomeiwen.com/subject/yqapfctx.html