Guest User

Untitled

a guest
Apr 22nd, 2026
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. if not ship then
  2. error("No ship API found. Put the computer on a VS ship.")
  3. end
  4.  
  5. local running = true
  6. local anchored = false
  7. local anchorPos = nil
  8. local status = "Ready"
  9.  
  10. local CFG = {
  11. gravityBias = 9.81,
  12.  
  13. -- жёсткость возврата к точке
  14. Pxy = 1.8,
  15. Py = 2.4,
  16.  
  17. -- демпфирование скорости
  18. Dxy = 3.2,
  19. Dy = 4.5,
  20.  
  21. -- ограничение ускорения
  22. maxAccelXY = 8.0,
  23. maxAccelY = 10.0,
  24.  
  25. fallbackDt = 0.05,
  26. }
  27.  
  28. local last = {
  29. pos = vector.new(0,0,0),
  30. vel = vector.new(0,0,0),
  31. mass = 0,
  32. err = vector.new(0,0,0),
  33. accel = vector.new(0,0,0),
  34. totalForce = vector.new(0,0,0),
  35. }
  36.  
  37. local function clamp(v, mn, mx)
  38. if v < mn then return mn end
  39. if v > mx then return mx end
  40. return v
  41. end
  42.  
  43. local function clear()
  44. term.setCursorPos(1, 1)
  45. term.clear()
  46. end
  47.  
  48. local function draw()
  49. clear()
  50. print("== Hard Anchor ==")
  51. print("Anchored : " .. tostring(anchored))
  52.  
  53. if anchorPos then
  54. print(("Anchor X : %.2f"):format(anchorPos.x))
  55. print(("Anchor Y : %.2f"):format(anchorPos.y))
  56. print(("Anchor Z : %.2f"):format(anchorPos.z))
  57. else
  58. print("Anchor : nil")
  59. end
  60.  
  61. print(("Pos X : %.2f"):format(last.pos.x))
  62. print(("Pos Y : %.2f"):format(last.pos.y))
  63. print(("Pos Z : %.2f"):format(last.pos.z))
  64. print(("Vel X : %.3f"):format(last.vel.x))
  65. print(("Vel Y : %.3f"):format(last.vel.y))
  66. print(("Vel Z : %.3f"):format(last.vel.z))
  67. print(("Mass : %.1f"):format(last.mass))
  68. print("")
  69. print("A = toggle anchor here")
  70. print("Q = quit")
  71. print("")
  72. print("Status: " .. status)
  73. end
  74.  
  75. local function physicsStep()
  76. local pos = ship.getWorldspacePosition()
  77. local vel = ship.getVelocity()
  78. local mass = ship.getMass()
  79.  
  80. last.pos = pos
  81. last.vel = vel
  82. last.mass = mass
  83.  
  84. if not anchored or not anchorPos then
  85. last.err = vector.new(0,0,0)
  86. last.accel = vector.new(0,0,0)
  87. last.totalForce = vector.new(0,0,0)
  88. return
  89. end
  90.  
  91. local err = anchorPos - pos
  92.  
  93. -- PD по позициям
  94. local ax = (CFG.Pxy * err.x) - (CFG.Dxy * vel.x)
  95. local ay = (CFG.Py * err.y) - (CFG.Dy * vel.y)
  96. local az = (CFG.Pxy * err.z) - (CFG.Dxy * vel.z)
  97.  
  98. ax = clamp(ax, -CFG.maxAccelXY, CFG.maxAccelXY)
  99. ay = clamp(ay, -CFG.maxAccelY, CFG.maxAccelY)
  100. az = clamp(az, -CFG.maxAccelXY, CFG.maxAccelXY)
  101.  
  102. -- по Y добавляем компенсацию гравитации
  103. local force = vector.new(
  104. mass * ax,
  105. mass * (CFG.gravityBias + ay),
  106. mass * az
  107. )
  108.  
  109. ship.applyWorldForce(force)
  110.  
  111. last.err = err
  112. last.accel = vector.new(ax, ay, az)
  113. last.totalForce = force
  114. end
  115.  
  116. local function physicsLoop()
  117. while running do
  118. local ok = pcall(function()
  119. ship.pullPhysicsTicks()
  120. end)
  121.  
  122. if not ok then
  123. sleep(CFG.fallbackDt)
  124. end
  125.  
  126. local ok2, err = pcall(physicsStep)
  127. if not ok2 then
  128. status = "Physics error: " .. tostring(err)
  129. end
  130.  
  131. draw()
  132. end
  133. end
  134.  
  135. local function inputLoop()
  136. while running do
  137. local _, key = os.pullEvent("key")
  138.  
  139. if key == keys.q then
  140. running = false
  141. status = "Stopping..."
  142.  
  143. elseif key == keys.a then
  144. if not anchored then
  145. anchorPos = ship.getWorldspacePosition()
  146. anchored = true
  147. status = ("Anchor set at %.2f / %.2f / %.2f"):format(anchorPos.x, anchorPos.y, anchorPos.z)
  148. else
  149. anchored = false
  150. anchorPos = nil
  151. status = "Anchor released"
  152. end
  153. end
  154. end
  155. end
  156.  
  157. draw()
  158. parallel.waitForAny(physicsLoop, inputLoop)
  159.  
  160. clear()
  161. print("Hard anchor stopped.")
Advertisement
Add Comment
Please, Sign In to add comment