首页 > 代码库 > 14072202(带IK的Recoil)

14072202(带IK的Recoil)

【目标】

带IK的Recoil

【思路】

1 继承于USkelControlLimb和UGameSkelCtrl_Recoil 

2 效果对比

技术分享 以这个骨骼为例


Recoil

技术分享


Limb

技术分享


可见,Recoil只影响一根骨骼
如果两个结合就是,Recoil产生的冲击位移作为Limb的控制点,由控制点带动三根骨骼的移动



【步骤】

1 在\ue3\Development\Src\Engine\Classes中新建SkelControlLimbRecoil.uc

先复制\ue3\Development\Src\GameFramework\Classes\GameSkelCtrl_Recoil.uc的内容

make 后 编译代码会出现结构体 枚举 重命名

需要将\GameSkelCtrl_Recoil.uc的结构体 枚举注释掉


2 在UnSkelControl.cpp 添加类实现

  1. IMPLEMENT_CLASS(USkelControlLimbRecoil)
  2. ...
  3. /*-----------------------------------------------------------------------------
  4. USkelControlLimbRecoil
  5. -----------------------------------------------------------------------------*/
  6. FVector2D USkelControlLimbRecoil::GetAim(USkeletalMeshComponent* InSkelComponent)
  7. {
  8. return Aim;
  9. }
  10. /** Is skeleton currently mirrored */
  11. UBOOL USkelControlLimbRecoil::IsMirrored(USkeletalMeshComponent* InSkelComponent)
  12. {
  13. return FALSE;
  14. }
  15. // USkelControlBase interface
  16. void USkelControlLimbRecoil::TickSkelControl(FLOAT DeltaSeconds, USkeletalMeshComponent* SkelComp)
  17. {
  18. bApplyControl = FALSE;
  19. if( ControlStrength > ZERO_ANIMWEIGHT_THRESH )
  20. {
  21. // if willing to play recoil, reset its state
  22. if( bPlayRecoil != bOldPlayRecoil )
  23. {
  24. bPlayRecoil = bOldPlayRecoil;
  25. Recoil.TimeToGo = Recoil.TimeDuration;
  26. // ERS_Random == Start at random position along sine wave,
  27. // ERS_Zero == Start at 0
  28. const FLOAT TWO_PI = 2.f * (FLOAT)PI;
  29. Recoil.RotSinOffset.X = Recoil.RotParams.X == ERS_Random ? appFrand() * TWO_PI : 0.f;
  30. Recoil.RotSinOffset.Y = Recoil.RotParams.Y == ERS_Random ? appFrand() * TWO_PI : 0.f;
  31. Recoil.RotSinOffset.Z = Recoil.RotParams.Z == ERS_Random ? appFrand() * TWO_PI : 0.f;
  32. Recoil.LocSinOffset.X = Recoil.LocParams.X == ERS_Random ? appFrand() * TWO_PI : 0.f;
  33. Recoil.LocSinOffset.Y = Recoil.LocParams.Y == ERS_Random ? appFrand() * TWO_PI : 0.f;
  34. Recoil.LocSinOffset.Z = Recoil.LocParams.Z == ERS_Random ? appFrand() * TWO_PI : 0.f;
  35. Recoil.RotOffset = FRotator(0,0,0);
  36. Recoil.LocOffset = FVector(0.f);
  37. }
  38. if( Recoil.TimeToGo > DeltaSeconds )
  39. {
  40. Recoil.TimeToGo -= DeltaSeconds;
  41. if( Recoil.TimeToGo > 0.f )
  42. {
  43. bApplyControl = TRUE;
  44. // Smooth fade out
  45. const FLOAT TimePct = Clamp<FLOAT>(Recoil.TimeToGo / Recoil.TimeDuration, 0.f, 1.f);
  46. const FLOAT Alpha = TimePct*TimePct*(3.f - 2.f*TimePct);
  47. const FLOAT AlphaTimesDelta = Alpha * DeltaSeconds;
  48. // Recoil Bone Rotation, compute sin wave value for each component
  49. if( !Recoil.RotAmplitude.IsZero() )
  50. {
  51. if( Recoil.RotAmplitude.X != 0.f )
  52. {
  53. Recoil.RotSinOffset.X += AlphaTimesDelta * Recoil.RotFrequency.X;
  54. Recoil.RotOffset.Pitch = appTrunc(Alpha * Recoil.RotAmplitude.X * appSin(Recoil.RotSinOffset.X));
  55. }
  56. if( Recoil.RotAmplitude.Y != 0.f )
  57. {
  58. Recoil.RotSinOffset.Y += AlphaTimesDelta * Recoil.RotFrequency.Y;
  59. Recoil.RotOffset.Yaw = appTrunc(Alpha * Recoil.RotAmplitude.Y * appSin(Recoil.RotSinOffset.Y));
  60. }
  61. if( Recoil.RotAmplitude.Z != 0.f )
  62. {
  63. Recoil.RotSinOffset.Z += AlphaTimesDelta * Recoil.RotFrequency.Z;
  64. Recoil.RotOffset.Roll = appTrunc(Alpha * Recoil.RotAmplitude.Z * appSin(Recoil.RotSinOffset.Z));
  65. }
  66. }
  67. // Recoil Bone Location, compute sin wave value for each component
  68. if( !Recoil.LocAmplitude.IsZero() )
  69. {
  70. if( Recoil.LocAmplitude.X != 0.f )
  71. {
  72. Recoil.LocSinOffset.X += AlphaTimesDelta * Recoil.LocFrequency.X;
  73. Recoil.LocOffset.X = Alpha * Recoil.LocAmplitude.X * appSin(Recoil.LocSinOffset.X);
  74. }
  75. if( Recoil.LocAmplitude.Y != 0.f )
  76. {
  77. Recoil.LocSinOffset.Y += AlphaTimesDelta * Recoil.LocFrequency.Y;
  78. Recoil.LocOffset.Y = Alpha * Recoil.LocAmplitude.Y * appSin(Recoil.LocSinOffset.Y);
  79. }
  80. if( Recoil.LocAmplitude.Z != 0.f )
  81. {
  82. Recoil.LocSinOffset.Z += AlphaTimesDelta * Recoil.LocFrequency.Z;
  83. Recoil.LocOffset.Z = Alpha * Recoil.LocAmplitude.Z * appSin(Recoil.LocSinOffset.Z);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. Super::TickSkelControl(DeltaSeconds, SkelComp);
  90. }
  91. void USkelControlLimbRecoil::CalculateNewBoneTransforms(INT BoneIndex, USkeletalMeshComponent* SkelComp, TArray<FBoneAtom>& OutBoneTransforms)
  92. {
  93. Super::CalculateNewBoneTransforms(BoneIndex, SkelComp, OutBoneTransforms);
  94. }

现在需要将USkelControlLimb.EffectorLocation 设置为Recoil影响的骨骼的更新的位置

3



4

技术分享





来自为知笔记(Wiz)


14072202(带IK的Recoil)