Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/client/fling_tracker.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 namespace remoting { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Stop flinging if the speed drops below this. | |
| 14 // 1px per 16ms. i.e. 1px/frame. | |
| 15 const float kMinTrackSpeed = 0.0625f; | |
| 16 | |
| 17 // The minimum fling duration (ms) needed to trigger the fling animation. This | |
| 18 // is to prevent unintentional fling with low velocity. | |
| 19 const float kMinFlingTime = 500.f; | |
| 20 | |
| 21 // Multiplied with the speed. This is used to prevent abruptness at the | |
| 22 // beginning. | |
| 23 // TODO(yuweih): This may need to be normalized with the screen DPI. | |
| 24 const float kAmplituteFactor = 0.75f; | |
| 25 | |
| 26 float GetDisplacement(float initial_speed_rate, | |
| 27 float time_constant, | |
| 28 float time_delta) { | |
| 29 // x = v0 * (1 - e^(-t / T)) | |
| 30 // This comes from a solution to the linear drag equation F=-kv | |
| 31 float exp_deceleration = std::exp(-time_delta / time_constant); | |
| 32 return initial_speed_rate * (1.f - exp_deceleration); | |
| 33 } | |
| 34 | |
| 35 float GetSpeed(float initial_speed_rate, | |
| 36 float time_constant, | |
| 37 float time_delta) { | |
| 38 // v = (1 / T) * v0 * e^(-t / T). | |
| 39 // Derivative of the displacement. | |
| 40 float exp_deceleration = std::exp(-time_delta / time_constant); | |
| 41 return (1.f / time_constant) * initial_speed_rate * exp_deceleration; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 FlingTracker::FlingTracker(float time_constant) | |
| 47 : time_constant_(time_constant) {} | |
| 48 | |
| 49 FlingTracker::~FlingTracker() {} | |
| 50 | |
| 51 void FlingTracker::StartFling(float velocity_x, float velocity_y) { | |
| 52 start_time_ = base::TimeTicks::Now(); | |
| 53 | |
| 54 initial_speed_rate_ = | |
| 55 std::sqrt(velocity_x * velocity_x + velocity_y * velocity_y); | |
| 56 | |
| 57 if (GetSpeed(initial_speed_rate_, time_constant_, kMinFlingTime) < | |
| 58 kMinTrackSpeed) { | |
| 59 StopFling(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 velocity_ratio_x_ = velocity_x / initial_speed_rate_; | |
| 64 velocity_ratio_y_ = velocity_y / initial_speed_rate_; | |
| 65 | |
| 66 initial_speed_rate_ *= kAmplituteFactor; | |
|
nicholss
2017/05/10 19:34:05
Why start with attenuation?
Yuwei
2017/05/10 23:28:24
I was basically inspired by this article:
https:/
| |
| 67 } | |
| 68 | |
| 69 void FlingTracker::StopFling() { | |
| 70 initial_speed_rate_ = 0.f; | |
| 71 } | |
| 72 | |
| 73 bool FlingTracker::IsFlingInProgress() const { | |
| 74 return initial_speed_rate_ > 0; | |
| 75 } | |
| 76 | |
| 77 bool FlingTracker::TrackPositions(base::TimeTicks time, float* x, float* y) { | |
| 78 if (!IsFlingInProgress()) { | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 float time_delta_ms = (time - start_time_).InMilliseconds(); | |
| 83 | |
| 84 float speed = GetSpeed(initial_speed_rate_, time_constant_, time_delta_ms); | |
| 85 | |
| 86 if (speed < kMinTrackSpeed) { | |
| 87 StopFling(); | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 float displacement = | |
| 92 GetDisplacement(initial_speed_rate_, time_constant_, time_delta_ms); | |
| 93 | |
| 94 *x = displacement * velocity_ratio_x_; | |
| 95 *y = displacement * velocity_ratio_y_; | |
| 96 | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 } // namespace remoting | |
| OLD | NEW |