Jump to content

Please help me with camera


Seddo

Recommended Posts

Hello, please help to correct one mistake.

Here the script, the idea was to make 1st person camera in car. But if i try to move the camera on the X or Y axis there is some bag.

For example if i set the value tarX = tarX + 0.37 then when i turn the car left or right in game, point of view is rotated around the zero coordinates (around the center of the vehicle). Can i fix it?

Untitled.png

function toggleCockpitView () 
  if (not isEnabled) then 
    isEnabled = true 
    addEventHandler ("onClientPreRender", root, camera) 
  else 
    isEnabled = false 
    setCameraTarget (localPlayer, localPlayer) 
    removeEventHandler ("onClientPreRender", root, camera) 
  end 
end 
addCommandHandler("racecam", toggleCockpitView) 
  
function camera() 
  if (isEnabled) then 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    if not vehicle then return end 
    local tarX, tarY, tarZ = getElementPosition(vehicle) 
    tarX = tarX 
    tarY = tarY 
    tarZ = tarZ 
    local velX, velY, velZ = getElementVelocity(vehicle) 
    local rotX, rotY, rotZ = getElementRotation(vehicle) 
    local vel3D = (velX^2+velY^2+velZ^2)^0.5 
      if(vel3D == 0)then 
        local camX, camY, camZ = getRotatedPosition(vehicle, 0, 0, 0) 
        return 
      end 
    local relX = velX / vel3D 
    local relY = velY / vel3D 
    local relZ = velZ / vel3D 
    local camX, camY, camZ = tarX, tarY, tarZ 
    camX, camY, camZ = camX-relX, camY-relY, camZ-relZ 
    local hit, hitX, hitY, hitZ = processLineOfSight(tarX, tarY, tarZ, camX, camY, camZ, true, false, false, true, false, true, true, true) 
    local rot = 0 
      if hit and (rotX >= 45 and rotX <= 360-45)then 
        rot = 180 
        camX, camY, camZ = getRotatedPosition(vehicle, 0, 0, 0) 
        camX, camY, camZ = camX-relX, camY-relY, camZ-relZ 
      else 
        rot = -rotY 
      end 
    setCameraMatrix(camX, camY, camZ+0.45, tarX, tarY, tarZ+0.45) 
  end 
end 
  
function getRotatedPosition(element, distX, distY, distZ) 
    if not element or not isElement(element) then return end 
    local matrix = getElementMatrix(element) 
    if not matrix then return end 
    local offX = distX * matrix[1][1] + distY * matrix[2][1] + distZ * matrix[3][1] + 1 * matrix[4][1] 
    local offY = distX * matrix[1][2] + distY * matrix[2][2] + distZ * matrix[3][2] + 1 * matrix[4][2] 
    local offZ = distX * matrix[1][3] + distY * matrix[2][3] + distZ * matrix[3][3] + 1 * matrix[4][3] 
    return offX, offY, offZ 
end 

Link to comment

I attached object to the vehicle, but i have the same problem... What is wrong?

function toggleCockpitView () 
  if (not isEnabled) then 
    isEnabled = true 
    addEventHandler ("onClientPreRender", root, camera) 
  else 
    isEnabled = false 
    setCameraTarget (localPlayer, localPlayer) 
    removeEventHandler ("onClientPreRender", root, camera) 
  end 
end 
addCommandHandler("racecam", toggleCockpitView) 
  
function camera() 
  if (isEnabled) then 
    local x, y, z = getElementPosition(getLocalPlayer()) 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    gnat = createObject(3803, x+0.37, y+0.6, z+0.6)  
    setElementAlpha(gnat, 0) 
    if not vehicle then return end 
    local tarX, tarY, tarZ = getElementPosition(gnat) 
    local velX, velY, velZ = getElementVelocity(vehicle) 
    local rotX, rotY, rotZ = getElementRotation(gnat) 
    local vel3D = (velX^2+velY^2+velZ^2)^0.5 
      if(vel3D == 0)then 
        local camX, camY, camZ = getRotatedPosition(gnat, 0, 0, 0) 
        return 
      end 
    local relX = velX / vel3D 
    local relY = velY / vel3D 
    local relZ = velZ / vel3D 
    local camX, camY, camZ = tarX, tarY, tarZ 
    camX, camY, camZ = camX-relX, camY-relY, camZ-relZ 
    local hit, hitX, hitY, hitZ = processLineOfSight(tarX, tarY, tarZ, camX, camY, camZ, true, false, false, true, false, true, true, true) 
    local rot = 0 
      if hit and (rotX >= 45 and rotX <= 360-45)then 
        rot = 180 
        camX, camY, camZ = getRotatedPosition(gnat, 0, 0, 0) 
        camX, camY, camZ = camX-relX, camY-relY, camZ-relZ 
      else 
        rot = -rotY 
      end 
    setCameraMatrix(camX, camY, camZ, tarX, tarY, tarZ) 
  end 
end 
  
function getRotatedPosition(element, distX, distY, distZ) 
    if not element or not isElement(element) then return end 
    local matrix = getElementMatrix(element) 
    if not matrix then return end 
    local offX = distX * matrix[1][1] + distY * matrix[2][1] + distZ * matrix[3][1] + 1 * matrix[4][1] 
    local offY = distX * matrix[1][2] + distY * matrix[2][2] + distZ * matrix[3][2] + 1 * matrix[4][2] 
    local offZ = distX * matrix[1][3] + distY * matrix[2][3] + distZ * matrix[3][3] + 1 * matrix[4][3] 
    return offX, offY, offZ 
end 
  

Link to comment
  • 3 weeks later...

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...