Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Auto-Orientation script
- -- by HugePinball ( [email protected] )
- -- object script - when the object is dropped, it will auto-rotate
- -- to the nearest angle of the angles specified by the config values
- -- !! NOTE !!
- -- some math needs fixing, this will likely not behave as expected for
- -- config values other than those shown, or with angle_shift = 30.
- -- functionality is fine for those options
- -- comment or delete log commands for performance
- -- config values
- --================
- local num_angles = 6
- local range_start = 0
- local range_end = 360
- local angle_shift = 0
- --================
- -- returns true if all are "null", false if any are not "null"
- local null =
- function(...)
- local vars = {...}
- for _,v in pairs(vars) do
- if not
- ((type(v)=="table" and next(v)==nil) or (v=="" or v==0 or not v))
- then return false
- end
- end
- return true
- end
- local interval
- local angles = {}
- function onLoad()
- interval = (range_end - range_start)/num_angles
- local i = 1
- for theta=range_start + angle_shift, range_end + angle_shift - interval, interval do
- angles[i] = theta
- i = i + 1
- end
- log(string.format("%s : %s", tostring(self), self.getName()))
- log(string.format("Description: %s", self.getDescription()))
- log("Valid angles:")
- log(angles)
- log("")
- endw
- function onDrop()
- local curr_angle = self.getRotation().y
- local nearest = {}
- log("\n\nDropped")
- log(string.format("current angle = %.2f\n",curr_angle))
- for _,angle in pairs(angles) do
- local delta = math.abs(angle - curr_angle)
- delta = math.abs(((delta > 180) and delta - 360 or delta))
- log(string.format("angle = %.2f, delta = %.2f", angle, delta))
- if null(nearest) or delta < nearest.delta then
- nearest = { angle = angle, delta = delta }
- end
- end
- log("")
- log("turning to nearest => " .. nearest.angle)
- self.setRotationSmooth({0,nearest.angle, (self.is_face_down and 180 or 0)})
- log("")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement