local maf = require 'maf' local vec3 = maf.vector local quat = maf.rotation -- Порог для…
- User
local maf = require 'maf' local vec3 = maf.vector local quat = maf.rotation
-- Порог для поднятия тангажа, используется для корректировки тангажа local pitchUpThreshold = -15 -- значение в градусах, извлечено из MonoBehaviour с fileID: 3737572916587997159
-- Угол для тонкой корректировки рулевого управления, -- определяет, когда следует выполнять корректировки рысканья или крена local fineSteeringAngle = 5 -- значение в градусах, извлечено из MonoBehaviour с fileID: 3737572916587997159
-- Фактор для корректировки рысканья, множитель для настройки управления по рысканию local yawFactor = 1 -- множитель, используется для масштабирования корректировки рысканья, извлечено из MonoBehaviour с fileID: 3737572916587997159
-- Фактор для корректировки крена, множитель для настройки управления по крену local rollFactor = 0.01 -- множитель, используется для масштабирования корректировки крена, извлечено из MonoBehaviour с fileID: 3737572916587997159
-- Скорость рулевого управления, определяет, насколько быстро следует двигаться к целевому вводу local steeringSpeed = 15.0 -- единиц в секунду, скорость перемещения к целевым значениям управления, извлечено из MonoBehaviour с fileID: 3737572916587997159
local dir = { forward = vec3(0, 1, 0), -- вперед backward = vec3(0, -1, 0), -- назад right = vec3(1, 0, 0), -- вправо left = vec3(-1, 0, 0), -- влево up = vec3(0, 0, 1), -- вверх down = vec3(0, 0, -1) -- вниз }
function signedAngle(from, to, axis) -- Step 1: Normalize the both vectors local fromNormalized = from:clone():normalize() local toNormalized = to:clone():normalize()
-- Step 2: Calculate the unsigned angle local dotProduct = fromNormalized:dot(toNormalized) local angle = math.acos(math.max(-1, math.min(1, dotProduct))) -- Clamp acos input
-- Step 3: Determine the sign using the cross product and reference axis local crossProduct = fromNormalized:cross(toNormalized) local sign = crossProduct:dot(axis) < 0 and -1 or 1
-- Step 4: Return the signed angle return angle * sign end
function CalculateSteeringForPlane(plane, dt, targetPosition) -- Assuming there's a way to check health or other properties similar to your reference
local planePosition = vec3(getCarCoordinates(plane)) local planeRotation = quat(getVehicleQuaternion(plane)) local error = targetPosition - planePosition local errorLocal = conjugateQuaternion(planeRotation) * error local errorDir = errorLocal:normalize() local pitchError = vec3(0, errorLocal.y, errorLocal.z):normalize() local rollError = vec3(errorLocal.x, errorLocal.y, 0):normalize() local yawError = vec3(errorLocal.x, 0, errorLocal.z):normalize() local targetInput = vec3() local pitch = signedAngle(dir.forward, pitchError, dir.right) if -pitch < pitchUpThreshold then pitch = (pitch + 360) % 360 end targetInput.x = pitch if calculateAngleBetweenVectors(dir.forward, errorDir) < fineSteeringAngle then local yaw = signedAngle(dir.forward, yawError, dir.up) targetInput.y = yaw * yawFactor else local roll = signedAngle(dir.up, rollError, dir.forward) targetInput.z = roll * rollFactor end targetInput.x = math.max(-1, math.min(1, targetInput.x)) targetInput.y = math.max(-1, math.min(1, targetInput.y)) targetInput.z = math.max(-1, math.min(1, targetInput.z)) local input = vec3() -- Assuming there is some last input stored somewhere local steeringSpeed = 1 -- Define your steering speed value input.x = math.max(-1, math.min(1, input.x + (targetInput.x - input.x) * steeringSpeed * dt)) input.y = math.max(-1, math.min(1, input.y + (targetInput.y - input.y) * steeringSpeed * dt)) input.z = math.max(-1, math.min(1, input.z + (targetInput.z - input.z) * steeringSpeed * dt)) lastInput = input -- Commented out `lastInput = input` assuming `lastInput` is defined elsewhere return input - Assistant
- User
напиши на луа реализацию функции math.sign
- Assistant
Certainly! The
math.signfunction is not built-in, but you can easily implement it in Lua. This function typically returns -1, 0, or 1, depending on whether the input number is negative, zero, or positive, respectively. Here's a simple implementation:function math.sign(x) if x > 0 then return 1 elseif x < 0 then return -1 else return 0 end endUsage:
math.sign(10)would return1.math.sign(-5)would return-1.math.sign(0)would return0.
This function evaluates the input
xand returns the appropriate sign, which can be useful in various mathematical calculations and algorithms.