OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/vr_controller.h" | 5 #include "chrome/browser/android/vr_shell/vr_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "chrome/browser/android/vr_shell/vr_math.h" | 13 #include "device/vr/vr_math.h" |
14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" |
15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_controller.h" | 15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_controller.h" |
16 | 16 |
17 namespace vr_shell { | 17 namespace vr_shell { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 constexpr float kDisplacementScaleFactor = 300.0f; | 21 constexpr float kDisplacementScaleFactor = 300.0f; |
22 | 22 |
23 // A slop represents a small rectangular region around the first touch point of | 23 // A slop represents a small rectangular region around the first touch point of |
(...skipping 10 matching lines...) Expand all Loading... |
34 // not equal. Also, minimum time distance needed to call two timestamps | 34 // not equal. Also, minimum time distance needed to call two timestamps |
35 // not equal. | 35 // not equal. |
36 constexpr float kDelta = 1.0e-7f; | 36 constexpr float kDelta = 1.0e-7f; |
37 | 37 |
38 constexpr float kCutoffHz = 10.0f; | 38 constexpr float kCutoffHz = 10.0f; |
39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); | 39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); |
40 constexpr float kNanoSecondsPerSecond = 1.0e9f; | 40 constexpr float kNanoSecondsPerSecond = 1.0e9f; |
41 | 41 |
42 constexpr int kMaxNumOfExtrapolations = 2; | 42 constexpr int kMaxNumOfExtrapolations = 2; |
43 | 43 |
44 static constexpr gvr::Vec3f kControllerPosition = {0.2f, -0.5f, -0.15f}; | 44 static constexpr gfx::Point3F kControllerPosition = {0.2f, -0.5f, -0.15f}; |
45 | 45 |
46 class Vector { | 46 void ClampTouchpadPosition(gfx::Vector2dF* position) { |
47 public: | 47 position->set_x(std::min(std::max(0.0f, position->x()), 1.0f)); |
48 static inline void ClampTouchpadPosition(gvr::Vec2f* position) { | 48 position->set_y(std::min(std::max(0.0f, position->y()), 1.0f)); |
49 position->x = std::min(std::max(0.0f, position->x), 1.0f); | 49 } |
50 position->y = std::min(std::max(0.0f, position->y), 1.0f); | |
51 } | |
52 | |
53 static inline void SetZero(gvr::Vec2f* v) { | |
54 v->x = 0; | |
55 v->y = 0; | |
56 } | |
57 | |
58 static inline gvr::Vec2f Subtract(gvr::Vec2f v1, gvr::Vec2f v2) { | |
59 gvr::Vec2f result; | |
60 result.x = v1.x - v2.x; | |
61 result.y = v1.y - v2.y; | |
62 return result; | |
63 } | |
64 | |
65 static inline gvr::Vec2f Add(gvr::Vec2f v1, gvr::Vec2f v2) { | |
66 gvr::Vec2f result; | |
67 result.x = v1.x + v2.x; | |
68 result.y = v1.y + v2.y; | |
69 return result; | |
70 } | |
71 | |
72 static inline bool Equal(const gvr::Vec2f v1, const gvr::Vec2f v2) { | |
73 return (std::abs(v1.x - v2.x) < kDelta) && (std::abs(v1.y - v2.y) < kDelta); | |
74 } | |
75 | |
76 static inline gvr::Vec2f ScalarMult(gvr::Vec2f v, float scalar) { | |
77 gvr::Vec2f vect_prod; | |
78 vect_prod.x = v.x * scalar; | |
79 vect_prod.y = v.y * scalar; | |
80 return vect_prod; | |
81 } | |
82 | |
83 }; // Vector | |
84 | 50 |
85 } // namespace | 51 } // namespace |
86 | 52 |
87 VrController::VrController(gvr_context* vr_context) { | 53 VrController::VrController(gvr_context* vr_context) { |
88 DVLOG(1) << __FUNCTION__ << "=" << this; | 54 DVLOG(1) << __FUNCTION__ << "=" << this; |
89 Initialize(vr_context); | 55 Initialize(vr_context); |
90 Reset(); | 56 Reset(); |
91 } | 57 } |
92 | 58 |
93 VrController::~VrController() { | 59 VrController::~VrController() { |
94 DVLOG(1) << __FUNCTION__ << "=" << this; | 60 DVLOG(1) << __FUNCTION__ << "=" << this; |
95 } | 61 } |
96 | 62 |
97 void VrController::OnResume() { | 63 void VrController::OnResume() { |
98 if (controller_api_) | 64 if (controller_api_) |
99 controller_api_->Resume(); | 65 controller_api_->Resume(); |
100 } | 66 } |
101 | 67 |
102 void VrController::OnPause() { | 68 void VrController::OnPause() { |
103 if (controller_api_) | 69 if (controller_api_) |
104 controller_api_->Pause(); | 70 controller_api_->Pause(); |
105 } | 71 } |
106 | 72 |
107 device::GvrGamepadData VrController::GetGamepadData() { | 73 device::GvrGamepadData VrController::GetGamepadData() { |
108 device::GvrGamepadData pad; | 74 device::GvrGamepadData pad; |
109 | 75 |
110 pad.timestamp = controller_state_->GetLastOrientationTimestamp(); | 76 pad.timestamp = controller_state_->GetLastOrientationTimestamp(); |
111 pad.touch_pos = controller_state_->GetTouchPos(); | 77 pad.touch_pos.set_x(TouchPosX()); |
112 pad.orientation = controller_state_->GetOrientation(); | 78 pad.touch_pos.set_y(TouchPosY()); |
| 79 pad.orientation = Orientation(); |
113 | 80 |
114 // Use orientation to rotate acceleration/gyro into seated space. | 81 // Use orientation to rotate acceleration/gyro into seated space. |
115 gvr::Mat4f pose_mat = QuatToMatrix(pad.orientation); | 82 vr::Mat4f pose_mat; |
116 pad.accel = MatrixVectorMul(pose_mat, controller_state_->GetAccel()); | 83 vr::QuatToMatrix(pad.orientation, &pose_mat); |
117 pad.gyro = MatrixVectorMul(pose_mat, controller_state_->GetGyro()); | 84 const gvr::Vec3f& accel = controller_state_->GetAccel(); |
| 85 const gvr::Vec3f& gyro = controller_state_->GetGyro(); |
| 86 pad.accel = |
| 87 vr::MatrixVectorMul(pose_mat, gfx::Vector3dF(accel.x, accel.y, accel.z)); |
| 88 pad.gyro = |
| 89 vr::MatrixVectorMul(pose_mat, gfx::Vector3dF(gyro.x, gyro.y, gyro.z)); |
118 | 90 |
119 pad.is_touching = controller_state_->IsTouching(); | 91 pad.is_touching = controller_state_->IsTouching(); |
120 pad.controller_button_pressed = | 92 pad.controller_button_pressed = |
121 controller_state_->GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); | 93 controller_state_->GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); |
122 pad.right_handed = handedness_ == GVR_CONTROLLER_RIGHT_HANDED; | 94 pad.right_handed = handedness_ == GVR_CONTROLLER_RIGHT_HANDED; |
123 | 95 |
124 return pad; | 96 return pad; |
125 } | 97 } |
126 | 98 |
127 bool VrController::IsTouching() { | 99 bool VrController::IsTouching() { |
128 return controller_state_->IsTouching(); | 100 return controller_state_->IsTouching(); |
129 } | 101 } |
130 | 102 |
131 float VrController::TouchPosX() { | 103 float VrController::TouchPosX() { |
132 return controller_state_->GetTouchPos().x; | 104 return controller_state_->GetTouchPos().x; |
133 } | 105 } |
134 | 106 |
135 float VrController::TouchPosY() { | 107 float VrController::TouchPosY() { |
136 return controller_state_->GetTouchPos().y; | 108 return controller_state_->GetTouchPos().y; |
137 } | 109 } |
138 | 110 |
139 gvr::Quatf VrController::Orientation() const { | 111 vr::Quatf VrController::Orientation() const { |
140 return controller_state_->GetOrientation(); | 112 const gvr::Quatf& orientation = controller_state_->GetOrientation(); |
| 113 return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation)); |
141 } | 114 } |
142 | 115 |
143 gvr::Mat4f VrController::GetTransform() const { | 116 void VrController::GetTransform(vr::Mat4f* out) const { |
144 // TODO(acondor): Position and orientation needs to be obtained | 117 // TODO(acondor): Position and orientation needs to be obtained |
145 // from an elbow model. | 118 // from an elbow model. |
146 // Placing the controller in a fixed position for now. | 119 // Placing the controller in a fixed position for now. |
147 gvr::Mat4f mat; | 120 vr::SetIdentityM(out); |
148 SetIdentityM(mat); | |
149 // Changing rotation point. | 121 // Changing rotation point. |
150 TranslateM(mat, mat, 0, 0, 0.05); | 122 vr::TranslateM(*out, gfx::Vector3dF(0, 0, 0.05), out); |
151 mat = MatrixMul(QuatToMatrix(Orientation()), mat); | 123 vr::Mat4f quat_to_matrix; |
152 TranslateM(mat, mat, kControllerPosition.x, kControllerPosition.y, | 124 vr::QuatToMatrix(Orientation(), &quat_to_matrix); |
153 kControllerPosition.z - 0.05); | 125 vr::MatrixMul(quat_to_matrix, *out, out); |
154 return mat; | 126 gfx::Vector3dF translation(kControllerPosition.x(), kControllerPosition.y(), |
| 127 kControllerPosition.z() - 0.05); |
| 128 vr::TranslateM(*out, translation, out); |
155 } | 129 } |
156 | 130 |
157 VrControllerModel::State VrController::GetModelState() const { | 131 VrControllerModel::State VrController::GetModelState() const { |
158 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) | 132 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) |
159 return VrControllerModel::TOUCHPAD; | 133 return VrControllerModel::TOUCHPAD; |
160 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) | 134 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) |
161 return VrControllerModel::APP; | 135 return VrControllerModel::APP; |
162 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) | 136 if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) |
163 return VrControllerModel::SYSTEM; | 137 return VrControllerModel::SYSTEM; |
164 return VrControllerModel::IDLE; | 138 return VrControllerModel::IDLE; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 VLOG(1) << "Controller Connection status: " | 173 VLOG(1) << "Controller Connection status: " |
200 << gvr_controller_connection_state_to_string( | 174 << gvr_controller_connection_state_to_string( |
201 controller_state_->GetConnectionState()); | 175 controller_state_->GetConnectionState()); |
202 } | 176 } |
203 } | 177 } |
204 | 178 |
205 void VrController::UpdateTouchInfo() { | 179 void VrController::UpdateTouchInfo() { |
206 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; | 180 CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; |
207 if (IsTouching() && state_ == SCROLLING && | 181 if (IsTouching() && state_ == SCROLLING && |
208 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || | 182 (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || |
209 (Vector::Equal(cur_touch_point_->position, | 183 (cur_touch_point_->position == prev_touch_point_->position && |
210 prev_touch_point_->position) && | |
211 extrapolated_touch_ < kMaxNumOfExtrapolations))) { | 184 extrapolated_touch_ < kMaxNumOfExtrapolations))) { |
212 extrapolated_touch_++; | 185 extrapolated_touch_++; |
213 touch_position_changed_ = true; | 186 touch_position_changed_ = true; |
214 // Fill the touch_info | 187 // Fill the touch_info |
215 float duration = | 188 float duration = |
216 (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - | 189 (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - |
217 last_timestamp_nanos_) / | 190 last_timestamp_nanos_) / |
218 kNanoSecondsPerSecond; | 191 kNanoSecondsPerSecond; |
219 touch_info_->touch_point.position.x = | 192 touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() + |
220 cur_touch_point_->position.x + overall_velocity_.x * duration; | 193 overall_velocity_.x() * duration); |
221 touch_info_->touch_point.position.y = | 194 touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() + |
222 cur_touch_point_->position.y + overall_velocity_.y * duration; | 195 overall_velocity_.y() * duration); |
223 } else { | 196 } else { |
224 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { | 197 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { |
225 Vector::SetZero(&overall_velocity_); | 198 overall_velocity_ = {0, 0}; |
226 } | 199 } |
227 extrapolated_touch_ = 0; | 200 extrapolated_touch_ = 0; |
228 } | 201 } |
229 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); | 202 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); |
230 last_timestamp_nanos_ = | 203 last_timestamp_nanos_ = |
231 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | 204 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |
232 } | 205 } |
233 | 206 |
234 void VrController::Initialize(gvr_context* gvr_context) { | 207 void VrController::Initialize(gvr_context* gvr_context) { |
235 CHECK(gvr_context != nullptr) << "invalid gvr_context"; | 208 CHECK(gvr_context != nullptr) << "invalid gvr_context"; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 ButtonUpHappened(gvr::kControllerButtonClick)) { | 245 ButtonUpHappened(gvr::kControllerButtonClick)) { |
273 gesture->SetType(WebInputEvent::kGestureTapDown); | 246 gesture->SetType(WebInputEvent::kGestureTapDown); |
274 gesture->x = 0; | 247 gesture->x = 0; |
275 gesture->y = 0; | 248 gesture->y = 0; |
276 } | 249 } |
277 gesture->source_device = blink::kWebGestureDeviceTouchpad; | 250 gesture->source_device = blink::kWebGestureDeviceTouchpad; |
278 gesture_list.push_back(std::move(gesture)); | 251 gesture_list.push_back(std::move(gesture)); |
279 | 252 |
280 if (gesture_list.back()->GetType() == WebInputEvent::kGestureScrollEnd) { | 253 if (gesture_list.back()->GetType() == WebInputEvent::kGestureScrollEnd) { |
281 if (!ButtonDownHappened(gvr::kControllerButtonClick) && | 254 if (!ButtonDownHappened(gvr::kControllerButtonClick) && |
282 (last_velocity_.x != 0.0 || last_velocity_.y != 0.0)) { | 255 (last_velocity_.x() != 0.0 || last_velocity_.y() != 0.0)) { |
283 std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( | 256 std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( |
284 WebInputEvent::kGestureFlingStart, WebInputEvent::kNoModifiers, | 257 WebInputEvent::kGestureFlingStart, WebInputEvent::kNoModifiers, |
285 gesture_list.back()->TimeStampSeconds())); | 258 gesture_list.back()->TimeStampSeconds())); |
286 fling->source_device = blink::kWebGestureDeviceTouchpad; | 259 fling->source_device = blink::kWebGestureDeviceTouchpad; |
287 if (IsHorizontalGesture()) { | 260 if (IsHorizontalGesture()) { |
288 fling->data.fling_start.velocity_x = | 261 fling->data.fling_start.velocity_x = |
289 last_velocity_.x * kDisplacementScaleFactor; | 262 last_velocity_.x() * kDisplacementScaleFactor; |
290 } else { | 263 } else { |
291 fling->data.fling_start.velocity_y = | 264 fling->data.fling_start.velocity_y = |
292 last_velocity_.y * kDisplacementScaleFactor; | 265 last_velocity_.y() * kDisplacementScaleFactor; |
293 } | 266 } |
294 gesture_list.push_back(std::move(fling)); | 267 gesture_list.push_back(std::move(fling)); |
295 } | 268 } |
296 Reset(); | 269 Reset(); |
297 } | 270 } |
298 | 271 |
299 return gesture_list; | 272 return gesture_list; |
300 } | 273 } |
301 | 274 |
302 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { | 275 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 318 |
346 // Touch position is changed, the touch point moves outside of slop, | 319 // Touch position is changed, the touch point moves outside of slop, |
347 // and the Controller's button is not down. | 320 // and the Controller's button is not down. |
348 if (touch_position_changed_ && touch_info_->is_touching && | 321 if (touch_position_changed_ && touch_info_->is_touching && |
349 !InSlop(touch_info_->touch_point.position) && | 322 !InSlop(touch_info_->touch_point.position) && |
350 !ButtonDownHappened(gvr::kControllerButtonClick)) { | 323 !ButtonDownHappened(gvr::kControllerButtonClick)) { |
351 state_ = SCROLLING; | 324 state_ = SCROLLING; |
352 gesture->SetType(WebInputEvent::kGestureScrollBegin); | 325 gesture->SetType(WebInputEvent::kGestureScrollBegin); |
353 UpdateGestureParameters(); | 326 UpdateGestureParameters(); |
354 gesture->data.scroll_begin.delta_x_hint = | 327 gesture->data.scroll_begin.delta_x_hint = |
355 displacement_.x * kDisplacementScaleFactor; | 328 displacement_.x() * kDisplacementScaleFactor; |
356 gesture->data.scroll_begin.delta_y_hint = | 329 gesture->data.scroll_begin.delta_y_hint = |
357 displacement_.y * kDisplacementScaleFactor; | 330 displacement_.y() * kDisplacementScaleFactor; |
358 gesture->data.scroll_begin.delta_hint_units = | 331 gesture->data.scroll_begin.delta_hint_units = |
359 blink::WebGestureEvent::ScrollUnits::kPrecisePixels; | 332 blink::WebGestureEvent::ScrollUnits::kPrecisePixels; |
360 } | 333 } |
361 } | 334 } |
362 | 335 |
363 void VrController::HandleScrollingState(WebGestureEvent* gesture) { | 336 void VrController::HandleScrollingState(WebGestureEvent* gesture) { |
364 if (touch_info_->touch_up || !(touch_info_->is_touching) || | 337 if (touch_info_->touch_up || !(touch_info_->is_touching) || |
365 ButtonDownHappened(gvr::kControllerButtonClick)) { | 338 ButtonDownHappened(gvr::kControllerButtonClick)) { |
366 // Gesture ends. | 339 // Gesture ends. |
367 gesture->SetType(WebInputEvent::kGestureScrollEnd); | 340 gesture->SetType(WebInputEvent::kGestureScrollEnd); |
368 UpdateGestureParameters(); | 341 UpdateGestureParameters(); |
369 } else if (touch_position_changed_) { | 342 } else if (touch_position_changed_) { |
370 // User continues scrolling and there is a change in touch position. | 343 // User continues scrolling and there is a change in touch position. |
371 gesture->SetType(WebInputEvent::kGestureScrollUpdate); | 344 gesture->SetType(WebInputEvent::kGestureScrollUpdate); |
372 UpdateGestureParameters(); | 345 UpdateGestureParameters(); |
373 if (IsHorizontalGesture()) { | 346 if (IsHorizontalGesture()) { |
374 gesture->data.scroll_update.delta_x = | 347 gesture->data.scroll_update.delta_x = |
375 displacement_.x * kDisplacementScaleFactor; | 348 displacement_.x() * kDisplacementScaleFactor; |
376 } else { | 349 } else { |
377 gesture->data.scroll_update.delta_y = | 350 gesture->data.scroll_update.delta_y = |
378 displacement_.y * kDisplacementScaleFactor; | 351 displacement_.y() * kDisplacementScaleFactor; |
379 } | 352 } |
380 last_velocity_ = overall_velocity_; | 353 last_velocity_ = overall_velocity_; |
381 } | 354 } |
382 } | 355 } |
383 | 356 |
384 bool VrController::IsHorizontalGesture() { | 357 bool VrController::IsHorizontalGesture() { |
385 return std::abs(last_velocity_.x) > std::abs(last_velocity_.y); | 358 return std::abs(last_velocity_.x()) > std::abs(last_velocity_.y()); |
386 } | 359 } |
387 | 360 |
388 bool VrController::InSlop(const gvr::Vec2f touch_position) { | 361 bool VrController::InSlop(const gfx::Vector2dF touch_position) { |
389 return (std::abs(touch_position.x - init_touch_point_->position.x) < | 362 return (std::abs(touch_position.x() - init_touch_point_->position.x()) < |
390 kSlopHorizontal) && | 363 kSlopHorizontal) && |
391 (std::abs(touch_position.y - init_touch_point_->position.y) < | 364 (std::abs(touch_position.y() - init_touch_point_->position.y()) < |
392 kSlopVertical); | 365 kSlopVertical); |
393 } | 366 } |
394 | 367 |
395 void VrController::Reset() { | 368 void VrController::Reset() { |
396 // Reset state. | 369 // Reset state. |
397 state_ = WAITING; | 370 state_ = WAITING; |
398 | 371 |
399 // Reset the pointers. | 372 // Reset the pointers. |
400 prev_touch_point_.reset(new TouchPoint); | 373 prev_touch_point_.reset(new TouchPoint); |
401 cur_touch_point_.reset(new TouchPoint); | 374 cur_touch_point_.reset(new TouchPoint); |
402 init_touch_point_.reset(new TouchPoint); | 375 init_touch_point_.reset(new TouchPoint); |
403 touch_info_.reset(new TouchInfo); | 376 touch_info_.reset(new TouchInfo); |
404 Vector::SetZero(&overall_velocity_); | 377 overall_velocity_ = {0, 0}; |
405 Vector::SetZero(&last_velocity_); | 378 last_velocity_ = {0, 0}; |
406 } | 379 } |
407 | 380 |
408 void VrController::UpdateGestureParameters() { | 381 void VrController::UpdateGestureParameters() { |
409 displacement_ = Vector::Subtract(touch_info_->touch_point.position, | 382 displacement_ = |
410 prev_touch_point_->position); | 383 touch_info_->touch_point.position - prev_touch_point_->position; |
411 } | 384 } |
412 | 385 |
413 bool VrController::UpdateCurrentTouchpoint() { | 386 bool VrController::UpdateCurrentTouchpoint() { |
414 touch_info_->touch_up = TouchUpHappened(); | 387 touch_info_->touch_up = TouchUpHappened(); |
415 touch_info_->touch_down = TouchDownHappened(); | 388 touch_info_->touch_down = TouchDownHappened(); |
416 touch_info_->is_touching = IsTouching(); | 389 touch_info_->is_touching = IsTouching(); |
417 touch_info_->touch_point.position.x = TouchPosX(); | 390 touch_info_->touch_point.position.set_x(TouchPosX()); |
418 touch_info_->touch_point.position.y = TouchPosY(); | 391 touch_info_->touch_point.position.set_y(TouchPosY()); |
419 Vector::ClampTouchpadPosition(&touch_info_->touch_point.position); | 392 ClampTouchpadPosition(&touch_info_->touch_point.position); |
420 touch_info_->touch_point.timestamp = | 393 touch_info_->touch_point.timestamp = |
421 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | 394 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |
422 | 395 |
423 if (IsTouching() || TouchUpHappened()) { | 396 if (IsTouching() || TouchUpHappened()) { |
424 // Update the touch point when the touch position has changed. | 397 // Update the touch point when the touch position has changed. |
425 if (!Vector::Equal(cur_touch_point_->position, | 398 if (cur_touch_point_->position != touch_info_->touch_point.position) { |
426 touch_info_->touch_point.position)) { | |
427 prev_touch_point_.swap(cur_touch_point_); | 399 prev_touch_point_.swap(cur_touch_point_); |
428 cur_touch_point_.reset(new TouchPoint); | 400 cur_touch_point_.reset(new TouchPoint); |
429 cur_touch_point_->position = touch_info_->touch_point.position; | 401 cur_touch_point_->position = touch_info_->touch_point.position; |
430 cur_touch_point_->timestamp = touch_info_->touch_point.timestamp; | 402 cur_touch_point_->timestamp = touch_info_->touch_point.timestamp; |
431 return true; | 403 return true; |
432 } | 404 } |
433 } | 405 } |
434 return false; | 406 return false; |
435 } | 407 } |
436 | 408 |
437 void VrController::UpdateOverallVelocity() { | 409 void VrController::UpdateOverallVelocity() { |
438 float duration = | 410 float duration = |
439 (touch_info_->touch_point.timestamp - prev_touch_point_->timestamp) / | 411 (touch_info_->touch_point.timestamp - prev_touch_point_->timestamp) / |
440 kNanoSecondsPerSecond; | 412 kNanoSecondsPerSecond; |
441 | 413 |
442 // If the timestamp does not change, do not update velocity. | 414 // If the timestamp does not change, do not update velocity. |
443 if (duration < kDelta) | 415 if (duration < kDelta) |
444 return; | 416 return; |
445 | 417 |
446 gvr::Vec2f displacement = Vector::Subtract(touch_info_->touch_point.position, | 418 const gfx::Vector2dF& displacement = |
447 prev_touch_point_->position); | 419 touch_info_->touch_point.position - prev_touch_point_->position; |
448 | 420 |
449 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); | 421 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); |
450 | 422 |
451 float weight = duration / (kRC + duration); | 423 float weight = duration / (kRC + duration); |
452 | 424 |
453 overall_velocity_ = | 425 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + |
454 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), | 426 ScaleVector2d(velocity, weight); |
455 Vector::ScalarMult(velocity, weight)); | |
456 } | 427 } |
457 | 428 |
458 } // namespace vr_shell | 429 } // namespace vr_shell |
OLD | NEW |