Как повернуть бота в сторону координат через кватернион, RakSAMP

Wer_tyn

Участник
Автор темы
65
7
Версия MoonLoader
Другое
Как в RakSAMP (с помощью Lua) вычислить кватернион поворота для бота, чтобы он смотрел на заданные координаты (например, x=1444.334432, y=1333.43243222, z=10.22)?
 
Решение
Lua:
function lookAtTarget(targetX, targetY, targetZ)
    local botX, botY, botZ = getBotPosition()
    
    local dirX = targetX - botX
    local dirY = targetY - botY
    local dirZ = targetZ - botZ
    
    local yaw = -math.atan2(dirY, dirX) + 1.5
    local horizontalDist = math.sqrt(dirX * dirX + dirY * dirY)
    local pitch = -math.atan2(dirZ, horizontalDist)
    
    local cy = math.cos(yaw * 0.5)
    local sp = math.sin(pitch * 0.5)

    local w = math.cos(yaw/2)
    local x = sp * cy
    local y = 0.15
    if math.sin(yaw/2) < 0.1 then
        y = 0
    elseif math.sin(yaw/2) < 0 then
        y = -y
    end
    local z = math.sin(yaw/2)

    setBotQuaternion(w, x, y, z)
end

fokichevskiy

Известный
505
305
Lua:
function lookAtTarget(targetX, targetY)
    local botX, botY = getBotPosition()
    -- вектор
    local dirX = targetX - botX
    local dirY = targetY - botY
    --угол
    local angle = -math.atan2(dirY, dirX) + 1.5
    -- преобразование угла в кватернион
    local halfAngle = angle / 2
    local w = math.cos(halfAngle)
    local x = 0.0
    local y = 0.0
    local z = math.sin(halfAngle)
    setBotQuaternion(w, x, y, z)
end

а координата z не нужна
 

Wer_tyn

Участник
Автор темы
65
7
Lua:
function lookAtTarget(targetX, targetY)
    local botX, botY = getBotPosition()
    -- вектор
    local dirX = targetX - botX
    local dirY = targetY - botY
    --угол
    local angle = -math.atan2(dirY, dirX) + 1.5
    -- преобразование угла в кватернион
    local halfAngle = angle / 2
    local w = math.cos(halfAngle)
    local x = 0.0
    local y = 0.0
    local z = math.sin(halfAngle)
    setBotQuaternion(w, x, y, z)
end

а координата z не нужна
Мне нужна ещё z, тк скрипт будет для машины и корды будут на высоте
 

fokichevskiy

Известный
505
305
Lua:
function lookAtTarget(targetX, targetY, targetZ)
    local botX, botY, botZ = getBotPosition()
    
    local dirX = targetX - botX
    local dirY = targetY - botY
    local dirZ = targetZ - botZ
    
    local yaw = -math.atan2(dirY, dirX) + 1.5
    local horizontalDist = math.sqrt(dirX * dirX + dirY * dirY)
    local pitch = -math.atan2(dirZ, horizontalDist)
    
    local cy = math.cos(yaw * 0.5)
    local sp = math.sin(pitch * 0.5)

    local w = math.cos(yaw/2)
    local x = sp * cy
    local y = 0.15
    if math.sin(yaw/2) < 0.1 then
        y = 0
    elseif math.sin(yaw/2) < 0 then
        y = -y
    end
    local z = math.sin(yaw/2)

    setBotQuaternion(w, x, y, z)
end
 

Wer_tyn

Участник
Автор темы
65
7
Lua:
function lookAtTarget(targetX, targetY, targetZ)
    local botX, botY, botZ = getBotPosition()
   
    local dirX = targetX - botX
    local dirY = targetY - botY
    local dirZ = targetZ - botZ
   
    local yaw = -math.atan2(dirY, dirX) + 1.5
    local horizontalDist = math.sqrt(dirX * dirX + dirY * dirY)
    local pitch = -math.atan2(dirZ, horizontalDist)
   
    local cy = math.cos(yaw * 0.5)
    local sp = math.sin(pitch * 0.5)

    local w = math.cos(yaw/2)
    local x = sp * cy
    local y = 0.15
    if math.sin(yaw/2) < 0.1 then
        y = 0
    elseif math.sin(yaw/2) < 0 then
        y = -y
    end
    local z = math.sin(yaw/2)

    setBotQuaternion(w, x, y, z)
end
Сяб, слушай не знаешь курд какой-то под раксамп с эмуляцией скорости и плавным передвижением?