Advertisement
MicycleBichael

moveShip.lua

Jun 23rd, 2024 (edited)
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local args = {...}
  2. local pos = ship.getWorldspacePosition()
  3.  
  4. maxSpeed = 13
  5.  
  6. cruisingAltitude = 330
  7. if args[4] ~= nil then
  8.     cruisingAltitude = tonumber(args[4])
  9. end
  10. mass = ship.getMass()
  11. accelAmount = 30
  12.  
  13. count = 0
  14. tolerance = 0.2
  15. toleranceHorizontal = 1
  16. multConst = (44.6650051949/math.pi)*180
  17.  
  18. function toY(y)
  19.     yVec = 1
  20.     while(math.abs(yVec) > 0.1)
  21.     do
  22.         pos = ship.getWorldspacePosition()
  23.         vel = ship.getVelocity()
  24.         yVec = y-pos.y
  25.         yVecNorm = yVec/math.abs(yVec)
  26.         targetVel = yVecNorm*math.min(math.abs(yVec),maxSpeed)
  27.         ship.applyInvariantForce(0,(targetVel-vel.y)*60*ship.getMass(),0)
  28.         sleep(0.01)
  29.     end
  30. end
  31.  
  32. function toXZ(x,z)
  33.     vecMagnitude = 1
  34.     while(vecMagnitude > 0.1)
  35.     do
  36.         pos = ship.getWorldspacePosition()
  37.         vel = ship.getVelocity()
  38.         xVec = x-pos.x
  39.         zVec = z-pos.z
  40.         vecMagnitude = math.sqrt(math.pow(xVec,2)+math.pow(zVec,2))
  41.         xNormVec = xVec/vecMagnitude
  42.         zNormVec = zVec/vecMagnitude
  43.         targetVelX = xNormVec*math.min(vecMagnitude,maxSpeed)
  44.         targetVelZ = zNormVec*math.min(vecMagnitude,maxSpeed)
  45.         ship.applyInvariantForce((targetVelX-vel.x)*60*ship.getMass(),0,(targetVelZ-vel.z)*60*ship.getMass())
  46.         sleep(0.01)
  47.     end
  48. end
  49.  
  50. function findAngle(x,z)
  51.     yaw = 0
  52.     pos = ship.getWorldspacePosition()
  53.     x = tonumber(x) - pos.x
  54.     z = tonumber(z) - pos.z
  55.     mag = math.sqrt(math.pow(x,2)+math.pow(z,2))
  56.     x = x/mag
  57.     z = z/mag
  58.     if z >= 0 then
  59.         yaw  = math.acos(x)
  60.     else
  61.         yaw = 2*math.pi - math.acos(x)
  62.     end
  63.     yaw = (yaw+math.pi) % (math.pi*2)
  64.     return yaw
  65. end
  66.  
  67. function rotShip(angle)
  68.     ship.applyInvariantTorque(0,-multConst*angle*ship.getMass(),0)
  69. end
  70.  
  71. function rotToAngle(angle)
  72.     currentShipRot = ship.getYaw()
  73.     if currentShipRot < 0 then
  74.         currentShipRot = currentShipRot + math.pi*2
  75.     end
  76.     if ship.getYaw() < 0 then
  77.         currentShipRot = math.pi*2 + ship.getYaw()
  78.     end
  79.     angleA = angle - currentShipRot
  80.     if math.abs(angleA) > math.pi then
  81.         angleB = -(angleA/math.abs(angleA))*(2*math.pi - math.abs(angleA))
  82.         rotShip(angleB)
  83.     else
  84.         rotShip(angleA)
  85.     end
  86. end
  87.  
  88. toY(cruisingAltitude)
  89. print("Cruising altitude reached.")
  90. targetAngle = findAngle(args[1],args[3])
  91. if targetAngle ~= targetAngle then
  92.     print("NaN Error!")
  93. else
  94.     rotToAngle(targetAngle)
  95.     print("Orientation Locked")
  96.     toXZ(args[1],args[3])
  97.     print("Horizontal position reached.")
  98.     toY(args[2])
  99.     print("You've reached your destination.")
  100. end
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement