Vector3.SignedAngle(Vector3.forward, pitchError, Vector3.right); какие аналоги…
- User
Vector3.SignedAngle(Vector3.forward, pitchError, Vector3.right);
какие аналоги Vector3.SignedAngle я могу использовать в либе maf?
-- maf -- https://github.com/bjornbytes/maf -- MIT License
local ffi = type(jit) == 'table' and jit.status() and require 'ffi' local vec3, quat
local forward local vtmp1 local vtmp2 local qtmp1
vec3 = { __call = function(_, x, y, z) return setmetatable({ x = x or 0, y = y or 0, z = z or 0 }, vec3) end,
__tostring = function(v) return string.format('(%f, %f, %f)', v.x, v.y, v.z) end,
__add = function(v, u) return v:add(u, vec3()) end, __sub = function(v, u) return v:sub(u, vec3()) end, __mul = function(v, u) if vec3.isvec3(u) then return v:mul(u, vec3()) elseif type(u) == 'number' then return v:scale(u, vec3()) else error('vec3s can only be multiplied by vec3s and numbers') end end, __div = function(v, u) if vec3.isvec3(u) then return v:div(u, vec3()) elseif type(u) == 'number' then return v:scale(1 / u, vec3()) else error('vec3s can only be divided by vec3s and numbers') end end, __unm = function(v) return v:scale(-1) end, __len = function(v) return v:length() end,
__index = { isvec3 = function(x) return ffi and ffi.istype('vec3', x) or getmetatable(x) == vec3 end,
clone = function(v) return vec3(v.x, v.y, v.z) end, unpack = function(v) return v.x, v.y, v.z end, set = function(v, x, y, z) if vec3.isvec3(x) then x, y, z = x.x, x.y, x.z end v.x = x v.y = y v.z = z return v end, add = function(v, u, out) out = out or v out.x = v.x + u.x out.y = v.y + u.y out.z = v.z + u.z return out end, sub = function(v, u, out) out = out or v out.x = v.x - u.x out.y = v.y - u.y out.z = v.z - u.z return out end, mul = function(v, u, out) out = out or v out.x = v.x * u.x out.y = v.y * u.y out.z = v.z * u.z return out end, div = function(v, u, out) out = out or v out.x = v.x / u.x out.y = v.y / u.y out.z = v.z / u.z return out end, scale = function(v, s, out) out = out or v out.x = v.x * s out.y = v.y * s out.z = v.z * s return out end, length = function(v) return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z) end, normalize = function(v, out) out = out or v local len = v:length() return len == 0 and v or v:scale(1 / len, out) end, distance = function(v, u) return vec3.sub(v, u, vtmp1):length() end, angle = function(v, u) return math.acos(v:dot(u) / (v:length() + u:length())) end, dot = function(v, u) return v.x * u.x + v.y * u.y + v.z * u.z end, cross = function(v, u, out) out = out or v local a, b, c = v.x, v.y, v.z out.x = b * u.z - c * u.y out.y = c * u.x - a * u.z out.z = a * u.y - b * u.x return out end, lerp = function(v, u, t, out) out = out or v out.x = v.x + (u.x - v.x) * t out.y = v.y + (u.y - v.y) * t out.z = v.z + (u.z - v.z) * t return out end, project = function(v, u, out) out = out or v local unorm = vtmp1 u:normalize(unorm) local dot = v:dot(unorm) out.x = unorm.x * dot out.y = unorm.y * dot out.z = unorm.z * dot return out end, rotate = function(v, q, out) out = out or v local u, c, o = vtmp1, vtmp2, out u.x, u.y, u.z = q.x, q.y, q.z o.x, o.y, o.z = v.x, v.y, v.z u:cross(v, c) local uu = u:dot(u) local uv = u:dot(v) o:scale(q.w * q.w - uu) u:scale(2 * uv) c:scale(2 * q.w) return o:add(u:add(c)) end - Assistant
To find an analogue of
Vector3.SignedAngleusing themaflibrary, you will need to manually calculate the signed angle between two vectors using the provided functions in themaflibrary. The computes the angle between two vectors considering a reference axis for determining the sign. You can achieve this in by using the cross product and dot product functions. - User
function CalculateSteeringForPlane(plane ,--[[float]] dt, --[[Vector3]] targetPosition) { local planePosition = vec3(getCarCoordinates(plane)) local planeRotation = quat(getVehicleQuaternion(plane))
local error = targetPosition - planePosition; error = -planeRotation * error --error = Quaternion.Inverse(plane.Rigidbody.rotation) * error; --transform into local space local errorDir = error:normalize(); local pitchError = vec3(0, error.y, error.z):normalize(); local rollError = vec3(error.x, error.y, 0):normalize(); -- как мне кажется, крен и рыскание надо поменять местами local yawError = vec3(error.x, 0, error.z):normalize(); -- как мне кажется, крен и рыскание надо поменять местами local targetInput = vec3(); local pitch = Vector3.SignedAngle(Vector3.forward, pitchError, Vector3.right); if (-pitch < pitchUpThreshold) pitch += 360f; targetInput.x = pitch; if (Vector3.Angle(Vector3.forward, errorDir) < fineSteeringAngle) { local yaw = Vector3.SignedAngle(Vector3.forward, yawError, Vector3.up); targetInput.y = yaw * yawFactor; } else { local roll = Vector3.SignedAngle(Vector3.up, rollError, Vector3.forward); targetInput.z = roll * rollFactor; } targetInput.x = Mathf.Clamp(targetInput.x, -1, 1); targetInput.y = Mathf.Clamp(targetInput.y, -1, 1); targetInput.z = Mathf.Clamp(targetInput.z, -1, 1); local input = Vector3.MoveTowards(lastInput, targetInput, steeringSpeed * dt); lastInput = input; return input; - Assistant
- User
только самое необходимое, как в коде гугла
- Assistant
Here is the condensed and streamlined version with essential comments:
function CalculateSteeringForPlane(plane, dt, targetPosition) local planePosition = vec3(getCarCoordinates(plane)) local planeRotation = quat(getVehicleQuaternion(plane)) local error = targetPosition - planePosition error = -planeRotation * error local errorDir = error:normalize() local pitchError = vec3(0, error.y, error.z):normalize() local rollError = vec3(error.x, error.y, 0):normalize() local yawError = vec3(error.x, 0, error.z):normalize() local targetInput = vec3() local pitch = signedAngle(vec3(0, 0, 1), pitchError, vec3(1, 0, 0)) if (-pitch < pitchUpThreshold) then pitch = pitch + 360 end targetInput.x = pitch if (angleBetween(vec3(0, 0, 1), errorDir) < fineSteeringAngle) then local yaw = signedAngle(vec3(0, 0, 1), yawError, vec3(0, 1, 0)) targetInput.y = yaw * yawFactor else local roll = signedAngle(vec3(0, 1, 0), rollError, vec3(0, 0, 1)) 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 = moveTowards(lastInput, targetInput, steeringSpeed * dt) lastInput = input return input end - User
ты не добавил комментариев..
- Assistant
Apologies for that. Here's the code with concise comments in a Google-style manner:
function CalculateSteeringForPlane(plane, dt, targetPosition) -- Get current position and orientation of the plane local planePosition = vec3(getCarCoordinates(plane)) local planeRotation = quat(getVehicleQuaternion(plane)) -- Compute error vector transformed into local space local error = targetPosition - planePosition error = -planeRotation * error -- Normalize directional errors for pitch, roll, and yaw local errorDir = error:normalize() local pitchError = vec3(0, error.y, error.z):normalize() local rollError = vec3(error.x, error.y, 0):normalize() local yawError = vec3(error.x, 0, error.z):normalize() -- Initialize target input vector local targetInput = vec3() -- Compute pitch based on error and thresholds local pitch = signedAngle(vec3(0, 0, 1), pitchError, vec3(1, 0, 0)) if (-pitch < pitchUpThreshold) then pitch = pitch + 360 end targetInput.x = pitch -- Determine steering adjustments for yaw or roll if (angleBetween(vec3(0, 0, 1), errorDir) < fineSteeringAngle) then -- Calculate yaw adjustment local yaw = signedAngle(vec3(0, 0, 1), yawError, vec3(0, 1, 0)) targetInput.y = yaw * yawFactor else -- Calculate roll adjustment local roll = signedAngle(vec3(0, 1, 0), rollError, vec3(0, 0, 1)) targetInput.z = roll * rollFactor end -- Clamp inputs to range [-1, 1] 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)) -- Smoothly move towards the target input local input = moveTowards(lastInput, targetInput, steeringSpeed * dt) lastInput = input return input end