Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local pos = ship.getWorldspacePosition()
- maxSpeed = 13
- cruisingAltitude = 330
- if args[4] ~= nil then
- cruisingAltitude = tonumber(args[4])
- end
- mass = ship.getMass()
- accelAmount = 30
- count = 0
- tolerance = 0.2
- toleranceHorizontal = 1
- multConst = (44.6650051949/math.pi)*180
- function toY(y)
- yVec = 1
- while(math.abs(yVec) > 0.1)
- do
- pos = ship.getWorldspacePosition()
- vel = ship.getVelocity()
- yVec = y-pos.y
- yVecNorm = yVec/math.abs(yVec)
- targetVel = yVecNorm*math.min(math.abs(yVec),maxSpeed)
- ship.applyInvariantForce(0,(targetVel-vel.y)*60*ship.getMass(),0)
- sleep(0.01)
- end
- end
- function toXZ(x,z)
- vecMagnitude = 1
- while(vecMagnitude > 0.1)
- do
- pos = ship.getWorldspacePosition()
- vel = ship.getVelocity()
- xVec = x-pos.x
- zVec = z-pos.z
- vecMagnitude = math.sqrt(math.pow(xVec,2)+math.pow(zVec,2))
- xNormVec = xVec/vecMagnitude
- zNormVec = zVec/vecMagnitude
- targetVelX = xNormVec*math.min(vecMagnitude,maxSpeed)
- targetVelZ = zNormVec*math.min(vecMagnitude,maxSpeed)
- ship.applyInvariantForce((targetVelX-vel.x)*60*ship.getMass(),0,(targetVelZ-vel.z)*60*ship.getMass())
- sleep(0.01)
- end
- end
- function findAngle(x,z)
- yaw = 0
- pos = ship.getWorldspacePosition()
- x = tonumber(x) - pos.x
- z = tonumber(z) - pos.z
- mag = math.sqrt(math.pow(x,2)+math.pow(z,2))
- x = x/mag
- z = z/mag
- if z >= 0 then
- yaw = math.acos(x)
- else
- yaw = 2*math.pi - math.acos(x)
- end
- yaw = (yaw+math.pi) % (math.pi*2)
- return yaw
- end
- function rotShip(angle)
- ship.applyInvariantTorque(0,-multConst*angle*ship.getMass(),0)
- end
- function rotToAngle(angle)
- currentShipRot = ship.getYaw()
- if currentShipRot < 0 then
- currentShipRot = currentShipRot + math.pi*2
- end
- if ship.getYaw() < 0 then
- currentShipRot = math.pi*2 + ship.getYaw()
- end
- angleA = angle - currentShipRot
- if math.abs(angleA) > math.pi then
- angleB = -(angleA/math.abs(angleA))*(2*math.pi - math.abs(angleA))
- rotShip(angleB)
- else
- rotShip(angleA)
- end
- end
- toY(cruisingAltitude)
- print("Cruising altitude reached.")
- targetAngle = findAngle(args[1],args[3])
- if targetAngle ~= targetAngle then
- print("NaN Error!")
- else
- rotToAngle(targetAngle)
- print("Orientation Locked")
- toXZ(args[1],args[3])
- print("Horizontal position reached.")
- toY(args[2])
- print("You've reached your destination.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement