Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@todo there must be some way of directly getting the mouse sampling rate from directinput
- if ( ( Key == EKeys::MouseX ) && KeyState->RawValue.X != 0.f )
- {
- // calculate sampling time
- // make sure not first non-zero sample
- if ( SmoothedMouse[0] != 0 )
- {
- // not first non-zero
- MouseSamplingTotal += FApp::GetDeltaTime();
- MouseSamples += KeyState->SampleCountAccumulator;
- }
- }
- float UPlayerInput::SmoothMouse(float aMouse, uint8& SampleCount, int32 Index)
- {
- check(Index >= 0);
- check(Index < UE_ARRAY_COUNT(ZeroTime));
- UWorld* World = GetWorld();
- if (World)
- {
- check(World->GetWorldSettings());
- const float EffectiveTimeDilation = World->GetWorldSettings()->GetEffectiveTimeDilation();
- if (EffectiveTimeDilation != LastTimeDilation)
- {
- LastTimeDilation = EffectiveTimeDilation;
- ClearSmoothing();
- }
- }
- const float DeltaTime = FApp::GetDeltaTime();
- if (DeltaTime < 0.25f)
- {
- check(MouseSamples > 0);
- // this is seconds/sample
- const float MouseSamplingTime = MouseSamplingTotal/MouseSamples;
- check(MouseSamplingTime > 0.0f);
- if ( aMouse == 0 )
- {
- // no mouse movement received
- ZeroTime[Index] += DeltaTime; // increment length of time we've been at zero
- if ( ZeroTime[Index] < MouseSamplingTime )
- {
- // zero mouse movement is possibly because less than the mouse sampling interval has passed
- aMouse = SmoothedMouse[Index] * DeltaTime/MouseSamplingTime;
- }
- else
- {
- SmoothedMouse[Index] = 0;
- }
- }
- else
- {
- ZeroTime[Index] = 0;
- if ( SmoothedMouse[Index] != 0 )
- {
- // this isn't the first tick with non-zero mouse movement
- if ( DeltaTime < MouseSamplingTime * (SampleCount + 1) )
- {
- check(SampleCount > 0);
- // smooth mouse movement so samples/tick is constant
- aMouse = aMouse * DeltaTime/(MouseSamplingTime * SampleCount);
- }
- else
- {
- // fewer samples, so going slow
- // use number of samples we should have had for sample count
- SampleCount = DeltaTime/MouseSamplingTime;
- }
- }
- check(SampleCount >= 0);
- if (SampleCount == 0)
- {
- SmoothedMouse[Index] = 0;
- }
- else
- {
- SmoothedMouse[Index] = aMouse/SampleCount;
- }
- }
- }
- else
- {
- // if we had an abnormally long frame, clear everything so it doesn't distort the results
- ClearSmoothing();
- }
- SampleCount = 0;
- return aMouse;
- }
- void UPlayerInput::ClearSmoothing()
- {
- for ( int32 i=0; i<2; i++ )
- {
- ZeroTime[i] = 0.f;
- SmoothedMouse[i] = 0.f;
- }
- const UPlayerInput* DefaultPlayerInput = GetDefault<UPlayerInput>();
- MouseSamplingTotal = DefaultPlayerInput->MouseSamplingTotal;
- MouseSamples = DefaultPlayerInput->MouseSamples;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement