Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. --[[
  2. Made by DasEtwas
  3. All rights reserved
  4. ]]--
  5.  
  6. -- global variables --
  7.  
  8. airDensity = 2.75
  9. updateSleep = 80
  10. maxAcceleration = 100 -- m/s² max acceleration
  11. movementSleep = 10 -- how many ticks it takes for previously completely still wings to activate when velocity:length() > sleepVel
  12. sleepVel = 0.5 -- m/s
  13.  
  14. -- utils --
  15.  
  16. function vecString(vec)
  17. return "["..tostring(math.floor((sm.vec3.getX(vec) * 1000) + 0.5) / 1000)..",".. tostring(math.floor((sm.vec3.getY(vec) * 1000) + 0.5) / 1000)..",".. tostring(math.floor((sm.vec3.getZ(vec) * 1000) + 0.5) / 1000).."]"
  18. end
  19.  
  20. function form(num)
  21. return math.floor((num * 100) + 0.5) / 100
  22. end
  23.  
  24. function sign(num)
  25. if num < 0 then
  26. return -1
  27. elseif num > 0 then
  28. return 1
  29. else
  30. return 0
  31. end
  32. end
  33.  
  34. function equals(vec1, vec2)
  35. return sm.vec3.getX(vec1) == sm.vec3.getX(vec2) and sm.vec3.getY(vec1) == sm.vec3.getY(vec2) and sm.vec3.getZ(vec1) == sm.vec3.getZ(vec2)
  36. end
  37.  
  38. function doAirfoilStuff( self, timeStep )
  39. -- check if lastPosition was set AND
  40. -- check if after taking the creation from the lift the shape's worldpos isnt wrong AND
  41. -- check if something was built onto the creation, which would screw up the shape's position
  42.  
  43. if self.lastPosition then
  44. local currentPos = sm.shape.getWorldPosition(self.shape)
  45.  
  46. if equals(currentPos, self.lastBodyPos) or (self.parentBodyMass ~= sm.body.getMass(self.shape:getBody())) then
  47. self.enabled = -updateSleep
  48. end
  49.  
  50. --print("currentPos: " .. vecString(currentPos) .. ", lastBodyPos: " .. vecString(self.lastBodyPos) .. ", enabled: " .. self.enabled)
  51.  
  52. local globalVel = (currentPos - self.lastPosition) / timeStep
  53. local globalVelL = globalVel:length()
  54.  
  55. if (globalVelL < sleepVel) then
  56. self.sleep = movementSleep
  57. elseif self.sleep > 0 then
  58. self.sleep = self.sleep - 1
  59. end
  60.  
  61. local acceleration = globalVelL
  62.  
  63. if self.lastVelocity then
  64. acceleration = (globalVelL - self.lastVelocity) / timeStep
  65. end
  66.  
  67. self.lastVelocity = globalVelL
  68.  
  69. if self.enabled >= 0 and self.sleep == 0 then
  70. if math.abs(acceleration) < maxAcceleration then
  71. --print("lastPos: " .. vecString(self.lastPosition) .. ", currentPos: " .. vecString(sm.shape.getWorldPosition(self.shape)))
  72. --print("parent body pos: " .. vecString(sm.body.getPosition(self.shape:getBody())))
  73.  
  74. local localX = sm.shape.getRight(self.shape)
  75. local localY = sm.shape.getUp(self.shape)
  76. local localZ = localX:cross(localY) -- normal vector
  77. self.normalVel = globalVel:dot(localZ)
  78. self.lift = airDensity * self.normalVel * self.normalVel * 0.5 * self.area * sign(self.normalVel)
  79.  
  80. --print("mass: " .. tostring(sm.body.getMass(self.shape:getBody())))
  81. --print("velocity: " .. vecString(globalVel) .. ", enabled: " .. tostring(self.enabled))
  82.  
  83. sm.physics.applyImpulse(self.shape, sm.vec3.new(0, self.lift, 0)) -- minus for other direction than velocity
  84. else
  85. print("too much acceleration: " .. tostring(form(acceleration)))
  86. --print("globalVelL: " .. tostring(form(globalVelL)))
  87. end
  88. --print("acceleration: " .. tostring(form(acceleration)))
  89. else
  90. self.enabled = self.enabled + 1
  91. end
  92.  
  93. end
  94.  
  95. self.lastBodyPos = sm.body.getPosition(self.shape:getBody())
  96. self.lastPosition = sm.shape.getWorldPosition(self.shape)
  97. self.parentBodyMass = sm.body.getMass(self.shape:getBody())
  98. end
  99.  
  100. -- Airfoil1 --
  101.  
  102. Airfoil1 = class( nil )
  103. Airfoil1.area = 1 -- m², in this case 4 * 4 blocks / 16 (conversion unit)
  104. Airfoil1.enabled = -updateSleep -- prevent jitter from false lastPosition
  105. Airfoil1.sleep = movementSleep
  106.  
  107. function Airfoil1.server_onCreate( self )
  108. self:server_init()
  109. end
  110.  
  111. function Airfoil1.server_init( self )
  112. self.enabled = -updateSleep
  113. end
  114.  
  115. function Airfoil1.server_onRefresh( self )
  116. self:server_init()
  117. end
  118.  
  119. function Airfoil1.server_onFixedUpdate( self, timeStep )
  120. doAirfoilStuff(self, timeStep)
  121. end
  122.  
  123. -- Airfoil2 --
  124.  
  125. Airfoil2 = class( nil )
  126. Airfoil2.area = 0.25 -- m², in this case 4 * 4 blocks / 16 (conversion unit)
  127. Airfoil2.enabled = -updateSleep -- prevent jitter from false lastPosition
  128. Airfoil2.sleep = movementSleep
  129.  
  130. function Airfoil2.server_onCreate( self )
  131. self:server_init()
  132. end
  133.  
  134. function Airfoil2.server_init( self )
  135. self.enabled = -updateSleep
  136. end
  137.  
  138. function Airfoil2.server_onRefresh( self )
  139. self:server_init()
  140. end
  141.  
  142. function Airfoil2.server_onFixedUpdate( self, timeStep )
  143. doAirfoilStuff(self, timeStep)
  144. end
  145.  
  146. -- end of script --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement