首页 > 代码库 > 14073102(CCDIKRecoil)

14073102(CCDIKRecoil)

【目标】

CCDIKRecoil

 

【思路】

1 CCDIK和Recoil的结合

2 Recoil的回弹机制,逐渐回到原来位置

3 添加一个Recoil基类

 

 

【步骤】

1 将\Src\GameFramework\Classes\GameSkelCtrl_Recoil.uc复制到\Src\Engine\Classes\SkelControlRecoilBase.uc

修改一下名称 作为基类

 

2 其他相关类继承于SkelControlRecoilBase, \Engine\Classes\SkelControlLimbRecoil.uc

技术分享 

make一下

编译不通过

技术分享 

原因是USkeletalControlRecoilBase是在后面声明的

方案:

采取将派生类放GameFrameWork工程中

编译后发现

技术分享 

这里有语法错误

技术分享 

 

最后还是没有用继承方式,脚本目前很难使用双基类

所以目前还是重复写相同代码

 

3 添加一个继承类GameSkelCtrl_CCD_IK_Recoil_CCD_IK_Recoil.uc,放到GameFramework中,否则又牵扯到那些结构体声明的先后顺序问题

 

 

4 在GameSkelControls.cpp 中添加实现宏,及函数实现

 

  1. IMPLEMENT_CLASS(UGameSkelCtrl_CCD_IK_Recoil);
 
  1. /*-----------------------------------------------------------------------------
  2. UGameSkelCtrl_CCD_IK_Recoil
  3. -----------------------------------------------------------------------------*/
  4. FVector2DUGameSkelCtrl_CCD_IK_Recoil::GetAim(USkeletalMeshComponent*InSkelComponent)
  5. {
  6. returnAim;
  7. }
  8. /** Is skeleton currently mirrored */
  9. UBOOL UGameSkelCtrl_CCD_IK_Recoil::IsMirrored(USkeletalMeshComponent*InSkelComponent)
  10. {
  11. return FALSE;
  12. }
  13. // USkelControlBase interface
  14. voidUGameSkelCtrl_CCD_IK_Recoil::TickSkelControl(FLOAT DeltaSeconds,USkeletalMeshComponent*SkelComp)
  15. {
  16. bApplyControl = FALSE;
  17. if(ControlStrength> ZERO_ANIMWEIGHT_THRESH )
  18. {
  19. // if willing to play recoil, reset its state
  20. if( bPlayRecoil != bOldPlayRecoil )
  21. {
  22. bPlayRecoil = bOldPlayRecoil;
  23. Recoil.TimeToGo=Recoil.TimeDuration;
  24. // ERS_Random == Start at random position along sine wave,
  25. // ERS_Zero == Start at 0
  26. const FLOAT TWO_PI =2.f*(FLOAT)PI;
  27. Recoil.RotSinOffset.X =Recoil.RotParams.X == ERS_Random ? appFrand()* TWO_PI :0.f;
  28. Recoil.RotSinOffset.Y =Recoil.RotParams.Y == ERS_Random ? appFrand()* TWO_PI :0.f;
  29. Recoil.RotSinOffset.Z =Recoil.RotParams.Z == ERS_Random ? appFrand()* TWO_PI :0.f;
  30. Recoil.LocSinOffset.X =Recoil.LocParams.X == ERS_Random ? appFrand()* TWO_PI :0.f;
  31. Recoil.LocSinOffset.Y =Recoil.LocParams.Y == ERS_Random ? appFrand()* TWO_PI :0.f;
  32. Recoil.LocSinOffset.Z =Recoil.LocParams.Z == ERS_Random ? appFrand()* TWO_PI :0.f;
  33. Recoil.RotOffset=FRotator(0,0,0);
  34. Recoil.LocOffset=FVector(0.f);
  35. }
  36. if(Recoil.TimeToGo>DeltaSeconds)
  37. {
  38. Recoil.TimeToGo-=DeltaSeconds;
  39. if(Recoil.TimeToGo>0.f)
  40. {
  41. bApplyControl = TRUE;
  42. // Smooth fade out
  43. const FLOAT TimePct=Clamp<FLOAT>(Recoil.TimeToGo/Recoil.TimeDuration,0.f,1.f);
  44. const FLOAT Alpha=TimePct*TimePct*(3.f-2.f*TimePct);
  45. const FLOAT AlphaTimesDelta=Alpha*DeltaSeconds;
  46. // Recoil Bone Rotation, compute sin wave value for each component
  47. if(!Recoil.RotAmplitude.IsZero())
  48. {
  49. if(Recoil.RotAmplitude.X !=0.f)
  50. {
  51. Recoil.RotSinOffset.X +=AlphaTimesDelta*Recoil.RotFrequency.X;
  52. Recoil.RotOffset.Pitch= appTrunc(Alpha*Recoil.RotAmplitude.X * appSin(Recoil.RotSinOffset.X));
  53. }
  54. if(Recoil.RotAmplitude.Y !=0.f)
  55. {
  56. Recoil.RotSinOffset.Y +=AlphaTimesDelta*Recoil.RotFrequency.Y;
  57. Recoil.RotOffset.Yaw= appTrunc(Alpha*Recoil.RotAmplitude.Y * appSin(Recoil.RotSinOffset.Y));
  58. }
  59. if(Recoil.RotAmplitude.Z !=0.f)
  60. {
  61. Recoil.RotSinOffset.Z +=AlphaTimesDelta*Recoil.RotFrequency.Z;
  62. Recoil.RotOffset.Roll= appTrunc(Alpha*Recoil.RotAmplitude.Z * appSin(Recoil.RotSinOffset.Z));
  63. }
  64. }
  65. // Recoil Bone Location, compute sin wave value for each component
  66. if(!Recoil.LocAmplitude.IsZero())
  67. {
  68. if(Recoil.LocAmplitude.X !=0.f)
  69. {
  70. Recoil.LocSinOffset.X +=AlphaTimesDelta*Recoil.LocFrequency.X;
  71. Recoil.LocOffset.X =Alpha*Recoil.LocAmplitude.X * appSin(Recoil.LocSinOffset.X);
  72. }
  73. if(Recoil.LocAmplitude.Y !=0.f)
  74. {
  75. Recoil.LocSinOffset.Y +=AlphaTimesDelta*Recoil.LocFrequency.Y;
  76. Recoil.LocOffset.Y =Alpha*Recoil.LocAmplitude.Y * appSin(Recoil.LocSinOffset.Y);
  77. }
  78. if(Recoil.LocAmplitude.Z !=0.f)
  79. {
  80. Recoil.LocSinOffset.Z +=AlphaTimesDelta*Recoil.LocFrequency.Z;
  81. Recoil.LocOffset.Z =Alpha*Recoil.LocAmplitude.Z * appSin(Recoil.LocSinOffset.Z);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. Super::TickSkelControl(DeltaSeconds,SkelComp);
  88. }
  89. voidUGameSkelCtrl_CCD_IK_Recoil::GetAffectedBones(INT BoneIndex,USkeletalMeshComponent*SkelComp,TArray<INT>&OutBoneIndices)
  90. {
  91. check(OutBoneIndices.Num()==0);
  92. // Only process bone if there is something to do
  93. if( bApplyControl )
  94. Super::GetAffectedBones(BoneIndex,SkelComp,OutBoneIndices);
  95. }
  96. voidUGameSkelCtrl_CCD_IK_Recoil::CalculateNewBoneTransforms(INT BoneIndex,USkeletalMeshComponent*SkelComp,TArray<FBoneAtom>&OutBoneTransforms)
  97. {
  98. check(OutBoneTransforms.Num()==0);
  99. // Current bone transform matrix in component space
  100. FBoneAtomNewBoneTM=SkelComp->SpaceBases(BoneIndex);
  101. if(bBoneSpaceRecoil)
  102. {
  103. if(!Recoil.LocOffset.IsZero()||!Recoil.RotOffset.IsZero())
  104. {
  105. FBoneAtomRecoilTM(Recoil.RotOffset,Recoil.LocOffset);
  106. NewBoneTM=RecoilTM*NewBoneTM;
  107. OutBoneTransforms.AddItem(NewBoneTM);
  108. }
  109. return;
  110. }
  111. // Extract Aim
  112. Aim=GetAim(SkelComp);
  113. // Actor to Aim transform matrix
  114. constFRotatorAimRotOffset( appTrunc(Aim.Y*16384), appTrunc(Aim.X*16384),0);
  115. FBoneAtomActorToAim(AimRotOffset,FVector::ZeroVector);
  116. ActorToAim.RemoveScaling();
  117. ActorToAim.SetOrigin(FVector(0.f));
  118. constFBoneAtomAimToActor=ActorToAim.Inverse();
  119. // Component to Actor transform matrix
  120. FBoneAtomComponentToActor=SkelComp->CalcComponentToFrameMatrix(BoneIndex, BCS_ActorSpace, NAME_None);
  121. ComponentToActor.RemoveScaling();
  122. ComponentToActor.SetOrigin(FVector(0.f));
  123. constFBoneAtomActorToComponent=ComponentToActor.InverseSafe();
  124. // Add rotation offset in component space
  125. if(!Recoil.RotOffset.IsZero())
  126. {
  127. FRotatorRotOffset=Recoil.RotOffset;
  128. // Handle mirroring
  129. if(IsMirrored(SkelComp))
  130. {
  131. RotOffset.Yaw=-RotOffset.Yaw;
  132. }
  133. FBoneAtomNewRotTM=NewBoneTM*(ComponentToActor*(AimToActor*FBoneAtom(RotOffset,FVector::ZeroVector)*ActorToAim)*ActorToComponent);
  134. NewRotTM.SetOrigin(NewBoneTM.GetOrigin());
  135. NewBoneTM=NewRotTM;
  136. }
  137. // Add location offset in component space
  138. if(!Recoil.LocOffset.IsZero())
  139. {
  140. FVectorLocOffset=Recoil.LocOffset;
  141. // Handle mirroring
  142. if(IsMirrored(SkelComp))
  143. {
  144. LocOffset.Y =-LocOffset.Y;
  145. }
  146. constFVectorTransInWorld=ActorToAim.TransformNormal(LocOffset);
  147. constFVectorTransInComp=ActorToComponent.TransformNormal(TransInWorld);
  148. constFVectorNewOrigin=NewBoneTM.GetOrigin()+TransInComp;
  149. NewBoneTM.SetOrigin(NewOrigin);
  150. }
  151. // OutBoneTransforms.AddItem(NewBoneTM);
  152. EffectorLocation=NewBoneTM.GetOrigin();
  153. EffectorLocationSpace= BCS_WorldSpace;
  154. Super::CalculateNewBoneTransforms(BoneIndex,SkelComp,OutBoneTransforms);
  155. }
 

 

5 运行

技术分享

 

如果单独一根骨骼的Recoil

技术分享

 
 

 

 

6

 

 

 



来自为知笔记(Wiz)



14073102(CCDIKRecoil)