Advertisement
Guest User

Untitled

a guest
Apr 19th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. //@todo there must be some way of directly getting the mouse sampling rate from directinput
  2. if ( ( Key == EKeys::MouseX ) && KeyState->RawValue.X != 0.f )
  3. {
  4. // calculate sampling time
  5. // make sure not first non-zero sample
  6. if ( SmoothedMouse[0] != 0 )
  7. {
  8. // not first non-zero
  9. MouseSamplingTotal += FApp::GetDeltaTime();
  10. MouseSamples += KeyState->SampleCountAccumulator;
  11. }
  12. }
  13.  
  14. float UPlayerInput::SmoothMouse(float aMouse, uint8& SampleCount, int32 Index)
  15. {
  16. check(Index >= 0);
  17. check(Index < UE_ARRAY_COUNT(ZeroTime));
  18.  
  19. UWorld* World = GetWorld();
  20. if (World)
  21. {
  22. check(World->GetWorldSettings());
  23. const float EffectiveTimeDilation = World->GetWorldSettings()->GetEffectiveTimeDilation();
  24. if (EffectiveTimeDilation != LastTimeDilation)
  25. {
  26. LastTimeDilation = EffectiveTimeDilation;
  27. ClearSmoothing();
  28. }
  29. }
  30.  
  31. const float DeltaTime = FApp::GetDeltaTime();
  32.  
  33. if (DeltaTime < 0.25f)
  34. {
  35. check(MouseSamples > 0);
  36.  
  37. // this is seconds/sample
  38. const float MouseSamplingTime = MouseSamplingTotal/MouseSamples;
  39. check(MouseSamplingTime > 0.0f);
  40.  
  41. if ( aMouse == 0 )
  42. {
  43. // no mouse movement received
  44. ZeroTime[Index] += DeltaTime; // increment length of time we've been at zero
  45. if ( ZeroTime[Index] < MouseSamplingTime )
  46. {
  47. // zero mouse movement is possibly because less than the mouse sampling interval has passed
  48. aMouse = SmoothedMouse[Index] * DeltaTime/MouseSamplingTime;
  49. }
  50. else
  51. {
  52. SmoothedMouse[Index] = 0;
  53. }
  54. }
  55. else
  56. {
  57. ZeroTime[Index] = 0;
  58. if ( SmoothedMouse[Index] != 0 )
  59. {
  60. // this isn't the first tick with non-zero mouse movement
  61. if ( DeltaTime < MouseSamplingTime * (SampleCount + 1) )
  62. {
  63. check(SampleCount > 0);
  64.  
  65. // smooth mouse movement so samples/tick is constant
  66. aMouse = aMouse * DeltaTime/(MouseSamplingTime * SampleCount);
  67. }
  68. else
  69. {
  70. // fewer samples, so going slow
  71. // use number of samples we should have had for sample count
  72. SampleCount = DeltaTime/MouseSamplingTime;
  73. }
  74. }
  75.  
  76. check(SampleCount >= 0);
  77. if (SampleCount == 0)
  78. {
  79. SmoothedMouse[Index] = 0;
  80. }
  81. else
  82. {
  83. SmoothedMouse[Index] = aMouse/SampleCount;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. // if we had an abnormally long frame, clear everything so it doesn't distort the results
  90. ClearSmoothing();
  91. }
  92.  
  93. SampleCount = 0;
  94.  
  95. return aMouse;
  96. }
  97.  
  98. void UPlayerInput::ClearSmoothing()
  99. {
  100. for ( int32 i=0; i<2; i++ )
  101. {
  102. ZeroTime[i] = 0.f;
  103. SmoothedMouse[i] = 0.f;
  104. }
  105.  
  106. const UPlayerInput* DefaultPlayerInput = GetDefault<UPlayerInput>();
  107.  
  108. MouseSamplingTotal = DefaultPlayerInput->MouseSamplingTotal;
  109. MouseSamples = DefaultPlayerInput->MouseSamples;
  110. }
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement