Jump to content

World rotation


FuriouZ

Recommended Posts

local m = getElementMatrix( theVehicle ) 
local pitch = math.deg( math.asin( m[2][3] ) ) -- (-90,90) 
local roll = math.deg( math.atan2( m[1][3], m[3][3] ) ) --(-180,180) - You should only need this one (roll) 
local yaw = math.deg( math.atan2( m[2][1], m[2][2] ) ) --(-180,180) 

You can do the rest.

Link to comment
local m = getElementMatrix( theVehicle ) 
local pitch = math.deg( math.asin( m[2][3] ) ) -- (-90,90) 
local roll = math.deg( math.atan2( m[1][3], m[3][3] ) ) --(-180,180) - You should only need this one (roll) 
local yaw = math.deg( math.atan2( m[2][1], m[2][2] ) ) --(-180,180) 

You can do the rest.

Jeez, I am dumb in Matrix functions...

So I have to set the matrix to the plane ?

I don't get it, I need to use only roll, right ?

but the damn script needs 3rd argument, then 4,5 ...

Code makes no sense

addEventHandler("onClientVehicleEnter", root,  
    function(theVehicle, seat, jacked) 
        local m = getElementMatrix( theVehicle ) 
        local pitch = math.deg( math.asin( m[2][3] ) ) -- (-90,90) 
        local roll = math.deg( math.atan2( m[1][3], m[3][3] ) ) --(-180,180) - You should only need this one (roll) 
        local yaw = math.deg( math.atan2( m[2][1], m[2][2] ) ) --(-180,180) 
            if (isPedInVehicle( getLocalPlayer() )) then 
                    setElementMatrix ( theVehicle, roll) 
            end 
    end) 

Link to comment
SetCameraMatrix

Use the calculated roll variable and pass it to the camera matrix (get the other values from getCameraMatrix)

What I am doing wrong.. Code is messed up

I do not neeed x, y, z or or only z ?

Damn I am dumb

mta-screen_2014-08-05_10-11-48.png

addEventHandler("onClientVehicleEnter", root,  
    function(theVehicle, seat, jacked) 
        local eM = getElementMatrix( theVehicle ) 
        local x, y, z, lx, ly, lz = getCameraMatrix( theVehicle ) 
        local pitch = math.deg( math.asin( eM[2][3] ) ) -- (-90,90) 
        local roll = math.deg( math.atan2( eM[1][3], eM[3][3] ) ) --(-180,180) - You should only need this one (roll) 
        local yaw = math.deg( math.atan2( eM[2][1], eM[2][2] ) ) --(-180,180) 
            if (isPedInVehicle( getLocalPlayer() )) then 
                if ( getVehicleType( source ) == "Plane" ) then 
                    --setElementMatrix ( theVehicle, roll, y, z, lx, ly, lz) 
                    setCameraMatrix ( theVehicle, roll, x, y, z, lx, ly, lz) 
                end 
            end 
    end) 

Link to comment
  
addEventHandler("onClientVehicleEnter", root, 
    function(theVehicle, seat, jacked) 
        local eM = getElementMatrix( theVehicle ) 
        local x, y, z, lx, ly, lz = getCameraMatrix( theVehicle ) 
        local pitch = math.deg( math.asin( eM[2][3] ) ) -- (-90,90) 
        local roll = math.deg( math.atan2( eM[1][3], eM[3][3] ) ) --(-180,180) - You should only need this one (roll) 
        local yaw = math.deg( math.atan2( eM[2][1], eM[2][2] ) ) --(-180,180) 
            if (isPedInVehicle( getLocalPlayer() )) then 
                if ( getVehicleType( source ) == "Plane" ) then 
                    --setElementMatrix ( theVehicle, roll, y, z, lx, ly, lz) 
                    setCameraMatrix ( x, y, z, lx, ly, lz, roll) 
                end 
            end 
    end) 
  

Link to comment

Almost works, but camera stays in one positsion, I tryed setCameraTarget to the localPlayer and theVehicle, but if I used localPlayer, then world didn't rotate and if theVehicle, then camera stayed in one pos like without setCameraTarget..

mta-screen_2014-08-05_15-23-28.png

mta-screen_2014-08-05_15-23-39.png

addEventHandler("onClientPreRender", root, 
    function(thePlayer) 
        local theVehicle = getPedOccupiedVehicle ( localPlayer ) 
            if not ( theVehicle ) then return end 
                local eM = getElementMatrix( theVehicle ) 
                local x, y, z, lx, ly, lz = getCameraMatrix() 
  
                local pitch = math.deg( math.asin( eM[2][3] ) ) -- (-90,90) 
                local roll = math.deg( math.atan2( eM[1][3], eM[3][3] ) ) --(-180,180) 
                local yaw = math.deg( math.atan2( eM[2][1], eM[2][2] ) ) --(-180,180) 
                   -- if ( getVehicleType( source ) == "Plane" ) then 
                        setCameraMatrix ( x, y, z, lx, ly, lz, roll) 
                        --setCameraTarget( theVehicle ) 
                    --end 
            end) 

Link to comment
  • MTA Team

*Edit, now tested & works

Video: Dropbox

  
addEventHandler("onClientPreRender", root, 
    function() 
        local theVehicle = getPedOccupiedVehicle(localPlayer) 
        if (not theVehicle) then return end 
  
        local eM = getElementMatrix( theVehicle ) 
        local vx, vy, vz = getElementVelocity( theVehicle ) 
        local speed = math.sqrt(math.pow(vx, 2) + math.pow(vy, 2) + math.pow(vz, 2)) + 0.5 
        local distance = 5 * speed + 12 
        local heightAboveVehicle = 2.5 
        local roll = math.deg(math.atan2(eM[1][3], eM[3][3])) 
        local x = -distance * eM[2][1] + eM[4][1] 
        local y = -distance * eM[2][2] + eM[4][2] 
        local z = -distance * eM[2][3] + heightAboveVehicle * eM[3][3] + eM[4][3] 
        local lx = 4 * eM[2][1] + eM[4][1] 
        local ly = 4 * eM[2][2] + eM[4][2] 
        local lz = 4 * eM[2][3] + eM[4][3] 
  
        if ( getVehicleType( theVehicle ) == "Plane" ) then 
            setCameraMatrix( x, y, z, lx, ly, lz, roll) 
        end 
    end 
) 
  

Link to comment
  • 2 months later...

Holy hell that's overly complicated... I made this as a test one. Works fairly well IIRC.

-- Changes the camera position on pre-render (to avoid stuttering) 
local function clientPreRender() 
        if not isPilotingCompatibleVehicle() then 
                return 
        end 
        
        local vehicle = getPedOccupiedVehicle(localPlayer) 
        local cameraX, cameraY, cameraZ = getPositionFromElementOffset(vehicle, 0, -7, 4) 
        local lookAtX, lookAtY, lookAtZ = getPositionFromElementOffset(vehicle, 0, 0, 2) 
        local _, rotY= getElementRotation(vehicle) 
        setCameraMatrix(cameraX, cameraY, cameraZ, lookAtX, lookAtY, lookAtZ, (rotY*-1)) 
end 
addEventHandler("onClientPreRender", root, clientPreRender) 

Link to comment
Holy hell that's overly complicated... I made this as a test one. Works fairly well IIRC.
-- Changes the camera position on pre-render (to avoid stuttering) 
local function clientPreRender() 
        if not isPilotingCompatibleVehicle() then 
                return 
        end 
        
        local vehicle = getPedOccupiedVehicle(localPlayer) 
        local cameraX, cameraY, cameraZ = getPositionFromElementOffset(vehicle, 0, -7, 4) 
        local lookAtX, lookAtY, lookAtZ = getPositionFromElementOffset(vehicle, 0, 0, 2) 
        local _, rotY= getElementRotation(vehicle) 
        setCameraMatrix(cameraX, cameraY, cameraZ, lookAtX, lookAtY, lookAtZ, (rotY*-1)) 
end 
addEventHandler("onClientPreRender", root, clientPreRender) 

Error: attempt to call global 'isPilotingCompatibleVehicle' (a nil value)

Link to comment

Woovie's code

-- List of vehicle models that are compatible with the jetHUD 
local compatibleModels = { 
    592, 
    566, 
    511, 
    548, 
    512, 
    593, 
    425, 
    520, 
    417, 
    487, 
    553, 
    488, 
    497, 
    563, 
    476, 
    447, 
    519, 
    460, 
    469, 
    513 
} 
  
local sw, sh = guiGetScreenSize() 
local iw, ih = 514, 514 
local lineW = 2 
local jetRender = dxCreateRenderTarget ( iw, ih, true ) 
local jetRender2 = dxCreateRenderTarget ( iw, ih, true ) 
local color = tocolor(255, 0, 0) 
  
-- Returns true if the local player is piloting a vehicle compatible with the jetHUD, false otherwise 
-- TODO: Check that the player is in the pilot's seat (no MTA function available?) 
function isPilotingCompatibleVehicle() 
    local vehicle = getPedOccupiedVehicle(localPlayer) 
    if not vehicle then 
        return false 
    end 
     
    local vehicleModel = getElementModel(vehicle) 
    for _, compatibleModel in ipairs(compatibleModels) do 
        if vehicleModel == compatibleModel then 
            return true 
        end 
    end 
     
    return false 
end 
  
-- Changes the camera position on pre-render (to avoid stuttering) 
local function clientPreRender() 
    if not isPilotingCompatibleVehicle() then 
        return 
    end 
     
    local vehicle = getPedOccupiedVehicle(localPlayer) 
    local cameraX, cameraY, cameraZ = getPositionFromElementOffset(vehicle, 0, -7, 4) 
    local lookAtX, lookAtY, lookAtZ = getPositionFromElementOffset(vehicle, 0, 0, 2) 
    local _, rotY= getElementRotation(vehicle) 
    setCameraMatrix(cameraX, cameraY, cameraZ, lookAtX, lookAtY, lookAtZ, (rotY*-1)) 
end 
addEventHandler("onClientPreRender", root, clientPreRender) 
  
-- Renders the HUD 
local function clientRender() 
    if not isPilotingCompatibleVehicle() or not jetRender then 
        return 
    end 
     
    local vehicle = getPedOccupiedVehicle(localPlayer) 
    dxSetRenderTarget ( jetRender, true ) 
    local x, y, z = getElementPosition ( vehicle ) 
    local rx, ry, rz = getElementRotation ( vehicle ) 
    local sx, sy, sz = getElementVelocity ( vehicle ) 
    local speed = ( sx^2 + sy^2 + sz^2 ) ^ ( 0.5 ) 
    local mps = speed * 50 
    local kmh = speed * 180 
    local mph = speed * 111.847 
     
    --speed 
    dxDrawLine ( 1, 200, 1, 220, color, lineW ) 
    dxDrawLine ( 51, 200, 51, 220, color, lineW ) 
    dxDrawLine ( 1, 200, 51, 200, color, lineW ) 
    dxDrawLine ( 1, 220, 51, 220, color, lineW ) 
    dxDrawText ( math.round(kmh, 2), 1, 201, 49, 219, color, 1, "clear", "center", "center", true, false, false, false, false ) 
     
    --altitude 
    dxDrawLine ( 463, 200, 463, 220, color, lineW ) 
    dxDrawLine ( 513, 200, 513, 220, color, lineW ) 
    dxDrawLine ( 463, 220, 513, 220, color, lineW ) 
    dxDrawLine ( 463, 200, 513, 200, color, lineW ) 
    dxDrawText ( math.round(z, 2), 464, 201, 512, 219, color, 1, "clear", "center", "center", true, false, false, false, false ) 
         
    --pitch rx 
    --yaw rz 
    --200 pixels, 40 degrees, every 5 pixels = 1 degree 
    local pixelOffset = 5 
    local rotmod = ( rz % 1 ) * pixelOffset 
    local rotcal = math.floor( 200 - rotmod ) / pixelOffset 
    for i=0,rotcal do 
        if (i % 2 == 0) then 
            dxDrawLine ( ( ( ( iw / 2 ) - 100 ) + rotmod + i * pixelOffset ), 40, ( ( ( iw / 2 ) - 100 ) + rotmod + i * pixelOffset ), 35, color, lineW ) 
        else 
            dxDrawLine ( ( ( ( iw / 2 ) - 100 ) + rotmod + i * pixelOffset ), 40, ( ( ( iw / 2 ) - 100 ) + rotmod + i * pixelOffset ), 30, color, lineW ) 
        end 
    end 
    dxDrawText ( math.round(rz, 2), (iw/2)-25, 51, (iw/2)+25, 71, color, 1, "clear", "center", "center", true, false, false, false, false ) 
    dxDrawLine ( (iw/2)-10, 50, (iw/2), 40, color, lineW ) 
    dxDrawLine ( (iw/2)+10, 50, (iw/2), 40, color, lineW ) 
    --level crosshairs 
    dxDrawLine ( (iw/2)-30, 220, (iw/2)+30, 220, color, lineW ) 
    dxDrawLine ( (iw/2), 220, (iw/2), 200, color, lineW ) 
    dxSetRenderTarget() 
    --roll ry ( for rotating pitch ) 
    --400 pixels, 40 degrees, 10 pixels = 1 degree 
    dxSetRenderTarget(jetRender2, true) 
    local pixelOffset = 10 
    local height = 300 
    local rotmod = ( rx % 1 ) * pixelOffset 
    local rotcal = math.floor( height - rotmod ) / pixelOffset 
    for i=0,rotcal do 
        if (i % 2 == 0) then 
            dxDrawLine ( 65, ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), 75, ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), color, lineW ) 
            dxDrawLine ( (iw-65), ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), (iw-75), ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), color, lineW ) 
        else 
            dxDrawLine ( 65, ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), 70, ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), color, lineW ) 
            dxDrawLine ( (iw-65), ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), (iw-70), ( ( ( iw / 2 ) - ( height / 2 ) ) + rotmod + i * pixelOffset ), color, lineW ) 
        end 
    end 
    dxSetRenderTarget() 
    dxDrawImage ( (sw/2)-(iw/2), (sh/2)-(ih/2), iw, ih, jetRender ) 
    dxDrawImage ( (sw/2)-(iw/2), (sh/2)-(ih/2), iw, ih, jetRender2,(ry*-1) ) 
end 
addEventHandler("onClientRender", root, clientRender) 
  
function math.round(number, decimals, method) 
    decimals = decimals or 0 
    local factor = 10 ^ decimals 
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor 
    else return tonumber(("%."..decimals.."f"):format(number)) end 
end 
  
function getPositionFromElementOffset(element,offX,offY,offZ) 
    local m = getElementMatrix ( element )  -- Get the matrix 
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform 
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2] 
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3] 
    return x, y, z                               -- Return the transformed point 
end 
  
addEventHandler("onClientPlayerVehicleExit", localPlayer, function() setCameraTarget(localPlayer) end) 
addEventHandler("onClientResourceStop", localPlayer, function() setCameraTarget(localPlayer) end) 

Edited by Guest
Link to comment
  • 2 weeks later...
hey i haev something like that too but when i exit the vehicle the camera matrix is not set to rthge player again...

Add this line to your script

addEventHandler("onClientPlayerVehicleExit", root, 
    function() 
        setCameraTarget(localPlayer) 
    end 
) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...