Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not ship then
- error("No ship API found. Put the computer on a VS ship.")
- end
- local running = true
- local anchored = false
- local anchorPos = nil
- local status = "Ready"
- local CFG = {
- gravityBias = 9.81,
- -- жёсткость возврата к точке
- Pxy = 1.8,
- Py = 2.4,
- -- демпфирование скорости
- Dxy = 3.2,
- Dy = 4.5,
- -- ограничение ускорения
- maxAccelXY = 8.0,
- maxAccelY = 10.0,
- fallbackDt = 0.05,
- }
- local last = {
- pos = vector.new(0,0,0),
- vel = vector.new(0,0,0),
- mass = 0,
- err = vector.new(0,0,0),
- accel = vector.new(0,0,0),
- totalForce = vector.new(0,0,0),
- }
- local function clamp(v, mn, mx)
- if v < mn then return mn end
- if v > mx then return mx end
- return v
- end
- local function clear()
- term.setCursorPos(1, 1)
- term.clear()
- end
- local function draw()
- clear()
- print("== Hard Anchor ==")
- print("Anchored : " .. tostring(anchored))
- if anchorPos then
- print(("Anchor X : %.2f"):format(anchorPos.x))
- print(("Anchor Y : %.2f"):format(anchorPos.y))
- print(("Anchor Z : %.2f"):format(anchorPos.z))
- else
- print("Anchor : nil")
- end
- print(("Pos X : %.2f"):format(last.pos.x))
- print(("Pos Y : %.2f"):format(last.pos.y))
- print(("Pos Z : %.2f"):format(last.pos.z))
- print(("Vel X : %.3f"):format(last.vel.x))
- print(("Vel Y : %.3f"):format(last.vel.y))
- print(("Vel Z : %.3f"):format(last.vel.z))
- print(("Mass : %.1f"):format(last.mass))
- print("")
- print("A = toggle anchor here")
- print("Q = quit")
- print("")
- print("Status: " .. status)
- end
- local function physicsStep()
- local pos = ship.getWorldspacePosition()
- local vel = ship.getVelocity()
- local mass = ship.getMass()
- last.pos = pos
- last.vel = vel
- last.mass = mass
- if not anchored or not anchorPos then
- last.err = vector.new(0,0,0)
- last.accel = vector.new(0,0,0)
- last.totalForce = vector.new(0,0,0)
- return
- end
- local err = anchorPos - pos
- -- PD по позициям
- local ax = (CFG.Pxy * err.x) - (CFG.Dxy * vel.x)
- local ay = (CFG.Py * err.y) - (CFG.Dy * vel.y)
- local az = (CFG.Pxy * err.z) - (CFG.Dxy * vel.z)
- ax = clamp(ax, -CFG.maxAccelXY, CFG.maxAccelXY)
- ay = clamp(ay, -CFG.maxAccelY, CFG.maxAccelY)
- az = clamp(az, -CFG.maxAccelXY, CFG.maxAccelXY)
- -- по Y добавляем компенсацию гравитации
- local force = vector.new(
- mass * ax,
- mass * (CFG.gravityBias + ay),
- mass * az
- )
- ship.applyWorldForce(force)
- last.err = err
- last.accel = vector.new(ax, ay, az)
- last.totalForce = force
- end
- local function physicsLoop()
- while running do
- local ok = pcall(function()
- ship.pullPhysicsTicks()
- end)
- if not ok then
- sleep(CFG.fallbackDt)
- end
- local ok2, err = pcall(physicsStep)
- if not ok2 then
- status = "Physics error: " .. tostring(err)
- end
- draw()
- end
- end
- local function inputLoop()
- while running do
- local _, key = os.pullEvent("key")
- if key == keys.q then
- running = false
- status = "Stopping..."
- elseif key == keys.a then
- if not anchored then
- anchorPos = ship.getWorldspacePosition()
- anchored = true
- status = ("Anchor set at %.2f / %.2f / %.2f"):format(anchorPos.x, anchorPos.y, anchorPos.z)
- else
- anchored = false
- anchorPos = nil
- status = "Anchor released"
- end
- end
- end
- end
- draw()
- parallel.waitForAny(physicsLoop, inputLoop)
- clear()
- print("Hard anchor stopped.")
Advertisement
Add Comment
Please, Sign In to add comment