Advertisement
C0BRA

Current head track filter

Oct 22nd, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. -- This is the one that works best for me, you may beed to change the variables below to suit you and your phone.
  2.  
  3. -- Smoothing is broken!
  4. local smoothing = 5 -- Applied before it is processed, but creates input lag
  5.  
  6. local movement_threshold_min = 0.0003 -- 0.04
  7. local movement_threshold_max = 0.002 -- 0.04
  8. local movement_threshold_change_ceof = 400
  9. local movement_threshold_change_ceof_down = 0.01
  10.  
  11. local max_desync_threshold = 1 -- will "resync" when out of this range
  12. local force_resync_threshold = 20 -- force it now, it's too far
  13.  
  14.  
  15. -- !!! do not change below unless you know what you're doing !!! --
  16.  
  17. include("lib/angle.lua")
  18. include("lib/vector2.lua")
  19.  
  20. local movement_threshold = movement_threshold_max
  21. local stilltime = 0
  22.  
  23. local out = Angle(0, 0, 0)
  24. local last = Angle(0, 0, 0)
  25. local offset = Angle(0, 0, 0)
  26. local lasttime = gettime()
  27.  
  28. function scale(x, from_min, from_max, to_min, to_max)
  29.     local from_diff = from_max - from_min
  30.     local to_diff = to_max - to_min
  31.    
  32.     local y = (x - from_min) / from_diff
  33.     return to_min + to_diff * y
  34. end
  35.  
  36.  
  37. local avg = {}
  38. for i = 1, smoothing do
  39.     table.insert(avg, Angle())
  40. end
  41.  
  42. function clean(p, y, r)
  43.     local cur = Angle(p, y, r)
  44.    
  45.     if smoothing ~= 0 then
  46.         table.remove(avg, 1)   
  47.         table.insert(avg, cur)
  48.        
  49.         local vec_p = Vector2()
  50.         local vec_y = Vector2()
  51.         local vec_r = Vector2()
  52.        
  53.         for k,v in pairs(avg) do
  54.             vec_p = vec_p + Vector2(v.p)
  55.             vec_y = vec_y + Vector2(v.y)
  56.             vec_r = vec_r + Vector2(v.r)
  57.         end
  58.        
  59.         vec_p:Normalize()
  60.         vec_y:Normalize()
  61.         vec_r:Normalize()
  62.        
  63.         cur.p = vec_p:ToAngle()
  64.         cur.y = vec_y:ToAngle()
  65.         cur.r = vec_r:ToAngle()
  66.     end
  67.    
  68.     local t = gettime() - lasttime
  69.     lasttime = gettime()
  70.     local dist = cur:Distance(last) * t
  71.    
  72.     print(dist)
  73.    
  74.     -- If we've moved quickly, we don't want to take small movements into consideration...
  75.     if dist > movement_threshold_min then
  76.         stilltime = stilltime + (dist - movement_threshold_min) * t * movement_threshold_change_ceof
  77.     else
  78.         stilltime = stilltime - t * movement_threshold_change_ceof * movement_threshold_change_ceof_down
  79.     end
  80.     stilltime = math.min(1, math.max(0, stilltime))
  81.    
  82.     movement_threshold = scale(stilltime, 0, 1, movement_threshold_min, movement_threshold_max)
  83.    
  84.     -- Slowly reset the offset
  85.     local offset_amm = offset:Distance(Angle(0, 0, 0))
  86.     if dist > movement_threshold or offset_amm > force_resync_threshold then
  87.         if offset_amm > max_desync_threshold then
  88.             offset = offset:Approach(Angle(0, 0, 0), t)
  89.         end
  90.        
  91.         out = cur + offset
  92.     else
  93.         offset = out - cur
  94.     end
  95.    
  96.     last = cur
  97.     return out:Unpack()
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement