Advertisement
Guest User

Untitled

a guest
Apr 14th, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1.  
  2. /*
  3. * Kinetic movement: cursor follows classical mechanics with
  4. * fluid (but not viscous) drag and Coulombian kinetic friction.
  5. *
  6. * Parameters:
  7. *
  8. * accn: acceleration when `key_down`
  9. * drag: drag coefficient
  10. * fric: friction coefficient
  11. * maxs: max speed in `report_mouse_t` units / 15ms
  12. *
  13. * Non-linear ODE (Riccati w/ q₁ = 0) model:
  14. *
  15. * a(t) = fric + (key_down(t) ? diagonal(t) ? accn/√2 : accn : 0)
  16. * dv(t)/dt = dir(t) × a(t) - sgn(v(t)) × (fric + drag × v(t)²)
  17. *
  18. * where `v(t)` is velocity, with `dir(t)` ∈ {0,±1}, `key_down(t)`, and
  19. * `diagonal(t)` corresponding to the state of the mousekey arrows.
  20. *
  21. * Note the `fric` term in the definition of `a(t)`, such that we
  22. * are _barely_ able to overcome friction when `accn` is set to 0.
  23. * This feels like the more intuitive behaviour for the end-user,
  24. * rather than requiring `accn > fric` for any acceleration at all.
  25. *
  26. * For `v(t) > 0`, `a(t) - fric = accn`, and starting with `v(0) = 0`,
  27. * there is a closed-form solution for `v(t)`:
  28. *
  29. * v(t) = √(accn/drag) × tanh(√(accn × drag) × t)
  30. *
  31. * Numerical approximation:
  32. *
  33. * Playing fast & loose with the Euler method, we can rewrite
  34. * the above as a recurrence relation with time step `dt`:
  35. *
  36. * v(t+dt) = v(t) + a(t) × dt
  37. * - sgn(v) × (fric + drag × v(t)²) × dt
  38. *
  39. * from which we derive the displacement for the current step:
  40. *
  41. * ds(t) = ½(v(t+dt) + v(t)) × dt
  42. *
  43. * We scale this by `maxs` to give `report_mouse_t` units.
  44. *
  45. * References:
  46. *
  47. * https://en.wikipedia.org/wiki/Drag_equation
  48. * https://en.wikipedia.org/wiki/Coulomb_damping
  49. * https://en.wikipedia.org/wiki/Ordinary_differential_equation
  50. * https://en.wikipedia.org/wiki/Euler_method
  51. * https://en.wikipedia.org/wiki/Riccati_equation with q₁ = 0
  52. * https://www.wolframalpha.com/ solve dv/dt = a - d × v(t)²; v(0) = 0
  53. */
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement