Jump to content

New Camera Mode


undefined

Recommended Posts

Hi guys. Im try this to create new camera mode. But it's not good work.

How to fix it?

My simple code:

local sX, sY = guiGetScreenSize() 
local dxOp = 40 
local boneX, boneY, boneZ, rot, rotat, mathSin, mathCos,  vx, vy, vz 
function setCameraMode() 
    boneX, boneY, boneZ = getPedBonePosition(localPlayer, 6) 
    rot = getPedRotation(localPlayer) 
    rotat = math.rad(rot) 
    mathSin = math.sin(rotat) 
    mathCos = math.cos(rotat) 
    vx = boneX + mathSin * mathCos * 8 
    vy = boneY + mathSin * mathCos * 8 
    vz = boneZ - mathSin * 2 
    setCameraMatrix(vx, vy, vz, boneX, boneY, boneZ, 0) 
end 
  
local state = false 
function text() 
    if state then 
        if dxOp > 0 then 
            dxOp = dxOp - 2 
        else 
            dxOp = 0 
        end 
    else 
        if dxOp < 40 then 
            dxOp = dxOp + 2 
        else 
            dxOp = 40 
        end 
    end 
    dxDrawText("Position X: "..tostring(vx).."\nPosition Y: "..tostring(vy).."\nPosition Z: "..tostring(vz).."\nlookAtX: "..tostring(boneX).."\nlookAtY: "..tostring(boneY).."\nlookAtZ: "..tostring(boneZ).."\nmath.sin: "..tostring(mathSin).."\nmath.cos: "..tostring(mathCos), 41, sY/2-55, nil, nil, tocolor(0, 0, 0, 255), 1, "default-bold", "left", "top", false, false, false, true, false, dxOp, dxOp, 0) 
    dxDrawText("#e4e4e4Position X: #00BFFF"..tostring(vx).."\n#e4e4e4Position Y: #00BFFF"..tostring(vy).."\n#e4e4e4Position Z: #00BFFF"..tostring(vz).."\n#e4e4e4lookAtX: #00BFFF"..tostring(boneX).."\n#e4e4e4lookAtY: #00BFFF"..tostring(boneY).."\n#e4e4e4lookAtZ: #00BFFF"..tostring(boneZ).."\n#e4e4e4math.sin: #00BFFF"..tostring(mathSin).."\n#e4e4e4math.cos: #00BFFF"..tostring(mathCos), 40, sY/2-56, nil, nil, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "top", false, false, false, true, false, dxOp, dxOp, 0) 
end 
addEventHandler("onClientPreRender", root, text) 
  
bindKey("b", "down", function() 
    if state then 
        setCameraTarget(localPlayer) 
        removeEventHandler("onClientPreRender", root, setCameraMode) 
    else 
        addEventHandler("onClientPreRender", root, setCameraMode) 
    end 
    state = ( not state ) 
end) 

And how i can set camera position with move mouse?

Note: Im new on math.rad/cos/sin code

Thanks :):)

Link to comment

What is the problem.

Those are basic mathematical operations. If you do not know about trigonometric functions I suggest you go to math lessons at school.

The original script works perfectly fine.

To enable mouse based rotation, simply just enable the cursor, stick it in the middle of the screen, take its position and then render the camera accordingly with the rotation, which is changed by following mouse movements. You will have to reverse the camera so the camera is in the middle instead of around a coordinate.

Link to comment

Woow! It's good, thanks. But it's have some problems. It's show object's inside. How to fix it? :(

And Im use this for mouse. But when the rotation each max, it's not blocking to set rotation. What is the problem?

(Im get this mouse code in the firstperson script.)

local rotX, rotY, rotSpeed, rotRadius, smoothWave, lastKey, visible = 0, 0, 3, 5, 0, "l", false 
  
local function render3DCamera( ) 
    local rotSpeed = rotSpeed / 10000 
     
    if ( getKeyState( "arrow_l" ) ) then 
        rotX = ( rotX - rotSpeed < 0 and rotX - rotSpeed + 360 or rotX - rotSpeed ) 
        smoothWave, lastKey = 3, "l" 
    elseif ( getKeyState( "arrow_r" ) ) then 
        rotX = ( rotX + rotSpeed > 360 and rotX + rotSpeed - 360 or rotX + rotSpeed ) 
        smoothWave, lastKey = 3, "r" 
    elseif ( not getKeyState( "arrow_l" ) ) and ( not getKeyState( "arrow_r" ) ) then 
        if ( smoothWave > 0 ) then 
            smoothWave = smoothWave - 0.07 
            local smoothWave = smoothWave / 10000 
             
            if ( lastKey == "l" ) then 
                rotX = ( rotX - smoothWave < 0 and rotX - smoothWave + 360 or rotX - smoothWave ) 
            elseif ( lastKey == "r" ) then 
                rotX = ( rotX + smoothWave > 360 and rotX + smoothWave - 360 or rotX + smoothWave ) 
            end 
        elseif ( smoothWave < 0 ) then 
            smoothWave = 0 
        end 
    end 
     
    local x, y, z = getElementPosition( localPlayer ) 
    local cx, cy, cz = getCameraMatrix( ) 
    local _cx, _cy, _cz = cx, cy, cz 
     
    if ( getKeyState( "arrow_u" ) ) then 
        _rotY = ( rotY - rotSpeed > 0 and rotY - rotSpeed or rotY ) 
        if ( isLineOfSightClear( x, y, z, _cx, _cy, math.cos( math.deg( _rotY ) ) * rotRadius + z - 0.5 ) ) then 
            rotY = ( rotY - rotSpeed > 0 and rotY - rotSpeed or rotY ) 
            smoothWave, lastKey = 3, "u" 
        end 
    elseif ( getKeyState( "arrow_d" ) ) then 
        _rotY = ( rotY + rotSpeed < 130 and rotY + rotSpeed or rotY ) 
        if ( isLineOfSightClear( x, y, z, _cx, _cy, math.cos( math.deg( _rotY ) ) * rotRadius + z - 0.5 ) ) then 
            rotY = ( rotY + rotSpeed < 130 and rotY + rotSpeed or rotY ) 
            smoothWave, lastKey = 3, "d" 
        end 
    elseif ( not getKeyState( "arrow_u" ) ) and ( not getKeyState( "arrow_d" ) ) then 
        if ( smoothWave > 0 ) then 
            smoothWave = smoothWave - 0.07 
            local smoothWave = smoothWave / 20000 
             
            if ( lastKey == "u" ) then 
                rotY = ( rotY - smoothWave > 0 and rotY - smoothWave or rotY ) 
            elseif ( lastKey == "d" ) then 
                rotY = ( rotY + smoothWave < 130 and rotY + smoothWave or rotY ) 
            end 
        elseif ( smoothWave < 0 ) then 
            smoothWave = 0 
        end 
    end 
     
    _cx = math.cos( math.deg( rotX ) ) * rotRadius + x 
    _cy = math.sin( math.deg( rotX ) ) * rotRadius + y 
    _cz = math.cos( math.deg( rotY ) ) * rotRadius + z 
     
    setCameraMatrix( _cx, _cy, _cz, x, y, z ) 
end 
  
function initialize3DCamera( ) 
    visible = not visible 
    local fn = visible and addEventHandler or removeEventHandler 
    fn("onClientPreRender", root, render3DCamera) 
    fn("onClientCursorMove", root, mouse) 
    if not visible then 
        setCameraTarget(localPlayer) 
    end 
end 
addEventHandler("onClientPlayerSpawn", gMe, initialize3DCamera) 
addEventHandler("onClientPlayerWasted", gMe, initialize3DCamera) 
  
function mouse(cX,cY,aX,aY) 
    if isCursorShowing() or isMTAWindowActive() then return end 
    local sX, sY = guiGetScreenSize() 
    aX = aX - sX/2  
    aY = aY - sY/2 
     
    rotX = rotX - aX * 0.005 * 0.01745 
    rotY = rotY + aY * 0.005 * 0.01745 
  
    local pRotX, pRotY, pRotZ = getElementRotation(localPlayer) 
    pRotZ = math.rad(pRotZ) 
     
    if rotX > 3.14 then 
        rotX = rotX - 6.28 
    elseif rotX < -3.14 then 
        rotX = rotX + 6.28 
    end 
     
    if rotY > 3.14 then 
        rotY = rotY - 6.28 
    elseif rotY < -3.14 then 
        rotY = rotY + 6.28 
    end 
     
    if isPedInVehicle(localPlayer) then 
        if rotY < -3.14 / 4 then 
            rotY = -3.14 / 4 
        elseif rotY > -3.14/15 then 
            rotY = -3.14/15 
        end 
    else 
        if rotY < -3.14 / 4 then 
            rotY = -3.14 / 4 
        elseif rotY > 3.14 / 2.1 then 
            rotY = 3.14 / 2.1 
        end 
    end 
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...