Guest User

rotation.lua

a guest
Jun 27th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1.  
  2. local quat = {
  3.  
  4. tostring = function(self)
  5. return "[x:"..self.v.x..", y:"..self.v.y..", z:"..self.v.z..", w:"..self.a.."]"
  6. end,
  7.  
  8. lengthSq = function(self)
  9. return self.v.x*self.v.x + self.v.y*self.v.y + self.v.z*self.v.z + self.a*self.a
  10. end,
  11.  
  12. length = function(self)
  13. return math.sqrt(self:lengthSq())
  14. end,
  15.  
  16. conjugate = function(self)
  17. return new(-self.v,self.a)
  18. end,
  19.  
  20. normalize = function(self)
  21. local l = self:length()
  22. return new(self.v:div(l), self.a/l)
  23. end,
  24.  
  25. inverse = function(self)
  26. if self:lengthSq() < 1e-5 then
  27. return self
  28. end
  29. local tmp = self:conjugate()
  30. return tmp:normalize()
  31. end,
  32.  
  33. mulQuat = function(self, q)
  34. return new(q.v:mul(self.a) + self.v:mul(q.a) + self.v:cross(q.v), self.a*q.a-self.v:dot(q.v))
  35. end,
  36.  
  37. mul = function(self,v)
  38. local tmp = self:mulQuat(new(v,0))
  39. tmp = tmp:mulQuat(self:inverse())
  40. return tmp.v
  41. end,
  42. }
  43.  
  44. local vmetatable = {
  45.     __index = quat,
  46.     --__add = vector.add,
  47.     --__sub = vector.sub,
  48.     --__mul = vector.mul,
  49.     --__div = vector.div,
  50.     --__unm = vector.unm,
  51.     __tostring = quat.tostring,
  52.     --__eq = vector.equals,
  53. }
  54.  
  55. function new(vec, w)
  56. return setmetatable({
  57.         v = vec or vector.new(),
  58.         a = tonumber(w) or 1,
  59.     }, vmetatable)
  60. end
  61.  
  62. function new2(quat)
  63. return setmetatable({
  64.         v = vector.new(quat.x, quat.y, quat.z),
  65.         a = quat.w,
  66.     }, vmetatable)
  67. end
  68.  
  69. local last = new2(ship.getQuaternion())
  70. local total = 0
  71. local cachedTotal = 0
  72. local counter = 0
  73.  
  74. while true do
  75.     local cur = new2(ship.getQuaternion())
  76.     local inv = cur:inverse()
  77.     inv = inv:mulQuat(last)
  78.     last = cur
  79.     local angle = 2*math.atan(inv.v:length(), inv.a)
  80.     -- convert to RPM
  81.     local rpm = angle / (2 * math.pi) * 20 * 60
  82.     --print(rmp)
  83.     total = total + rpm
  84.     counter = counter + 1
  85.     if counter == 20 then
  86.         cachedTotal = total / 20
  87.         counter = 0
  88.         total = 0
  89.     end
  90.     commands.exec(string.format("title @a actionbar {\"text\":\"RMP: %5.1f / %5.1f\"}", rpm, cachedTotal))
  91.     --sleep(0.01)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment