Advertisement
HugePinball

auto_orientation.lua

Mar 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. -- Auto-Orientation script
  2. -- by HugePinball ( [email protected] )
  3.  
  4. -- object script - when the object is dropped, it will auto-rotate
  5. -- to the nearest angle of the angles specified by the config values
  6.  
  7. -- !! NOTE !!
  8. -- some math needs fixing, this will likely not behave as expected for
  9. -- config values other than those shown, or with angle_shift = 30.
  10. -- functionality is fine for those options
  11.  
  12. -- comment or delete log commands for performance
  13.  
  14. -- config values
  15.  
  16. --================
  17. local num_angles = 6
  18. local range_start = 0
  19. local range_end = 360
  20. local angle_shift = 0
  21. --================
  22.  
  23. -- returns true if all are "null", false if any are not "null"
  24. local null =
  25.     function(...)
  26.         local vars = {...}
  27.         for _,v in pairs(vars) do
  28.             if not
  29.                 ((type(v)=="table" and next(v)==nil) or (v=="" or v==0 or not v))
  30.             then return false
  31.             end
  32.         end
  33.         return true
  34.     end
  35.  
  36. local interval
  37. local angles = {}
  38.  
  39. function onLoad()
  40.     interval = (range_end - range_start)/num_angles
  41.     local i = 1
  42.     for theta=range_start + angle_shift, range_end + angle_shift - interval, interval do
  43.         angles[i] = theta
  44.         i = i + 1
  45.     end
  46.     log(string.format("%s : %s", tostring(self), self.getName()))
  47.     log(string.format("Description: %s", self.getDescription()))
  48.     log("Valid angles:")
  49.     log(angles)
  50.     log("")
  51. endw
  52.  
  53. function onDrop()
  54.     local curr_angle = self.getRotation().y
  55.     local nearest = {}
  56.     log("\n\nDropped")
  57.     log(string.format("current angle = %.2f\n",curr_angle))
  58.     for _,angle in pairs(angles) do
  59.         local delta = math.abs(angle - curr_angle)
  60.         delta = math.abs(((delta > 180) and delta - 360 or delta))
  61.         log(string.format("angle = %.2f, delta = %.2f", angle, delta))
  62.         if null(nearest) or delta < nearest.delta then
  63.             nearest = { angle = angle, delta = delta }
  64.         end
  65.     end
  66.     log("")
  67.     log("turning to nearest => " .. nearest.angle)
  68.    
  69.     self.setRotationSmooth({0,nearest.angle, (self.is_face_down and 180 or 0)})
  70.     log("")
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement