Jump to content

setCameraMatrix Issue


Overkillz

Recommended Posts

Hello dear community. Im having a big issue with setCameraMatrix on the clientside.

Well, I needed to set a custom camera target to an object (This script is an adaptation of @Sasu)

Spoiler

local rotX, rotY, rotRadius, maxRadius = 0, -math.pi/2, 10, 20 
local isCustomCamera = false 
local cameraTarget = nil 
local sX, sY = guiGetScreenSize() 
  
addEventHandler("onClientPreRender", root, 
    function() 
        if isCustomCamera and isElement(cameraTarget) then 
  
            local x, y, z = getElementPosition( cameraTarget ) 
            local cx, cy, cz 
             
            cx = x + rotRadius * math.sin(rotY) * math.cos(rotX) 
            cy = y + rotRadius * math.sin(rotY) * math.sin(rotX) 
            cz = z + rotRadius * math.cos(rotY) 
             
            local hit, hitX, hitY, hitZ = processLineOfSight(x, y, z, cx, cy, cz, _, false, _, _, _, _, _, _, cameraTarget) 
            if hit then 
                cx, cy, cz = hitX, hitY, hitZ 
            end 
  
            setCameraMatrix( cx, cy, cz, x, y, z ) 
        end 
    end 
) 
  
addEventHandler("onClientKey", root, 
    function(button) 
        if button == "mouse_wheel_up" then 
            rotRadius = math.max(2, rotRadius - 1) 
        elseif button == "mouse_wheel_down" then 
            rotRadius = math.min(maxRadius, rotRadius + 1) 
        end 
    end 
) 
  
addEventHandler("onClientCursorMove", root, 
    function(cX,cY,aX,aY) 
        if isCursorShowing() or isMTAWindowActive() then return end 
                 
        aX = aX - sX/2  
        aY = aY - sY/2 
  
        rotX = rotX - aX * 0.01745 * 0.3 
        rotY = math.min(-0.02, math.max( rotY + aY * 0.01745 * 0.3, -3.11 ) ) 
         
    end 
) 
  
function setCustomCameraTarget(element) 
    if isElement(element) then 
        cameraTarget = element 
        isCustomCamera = true 
        return true 
    else 
        cameraTarget = nil 
        isCustomCamera = false 
    end 
end 

 

Well, my problem is that when Im using that custom camera target and I want to restore the camera to the player. It doesn't work. I tried several ways and events like onClientRender, onClientPreRender ...

I tried to spawn again the player, setCameraTarget (The default one) and later use the setCameraMatrix and to re-force it. add the setCameraMatrix inside a function handled by onClientRender.

Before I've used setCameraMatrix I removed the events used for the custom camera target and set the varaibles (cameraTarget & isCustomCamera) as nil & false.

I hope you can bring me a hand with this issue. Any information that u need just comment below and I will bring you more details.

Best regards.

 

Edited by Overkillz
Link to comment
  • Moderators
22 hours ago, Overkillz said:

Yes, but it is not working. Idk if there are other ways to simulate or force the function setCameraMatrix.

Might attaching a cam, but Im not sure if you know how to deal with this weird issue.

Regards.

 

The issue is caused by not keeping an eye on state changing. The camera status can be changed by resources, but also by MTA conditions (for example dying).

If you want to force setCameraMatrix you will need to repeat that process every frame to prevent any interruption.

So you could create a resource which will manage all camera statuses and prevent any interruption. And even add extra custom ones. Like moving a camera from position to position.(cinematic effect)

 

 

 

Link to comment

@IIYAMA

Thanks for your answer, but I didn't get u at all. Sorry ?

Practically Im managing the camera by a single resource. I will tell you step by step what do I do ?

The fisrt thing I do is join to the arena, in this case is the garage arena.

Inside the arena I only do is to spawn the player and setCameraTarget.

Later use the custom camera script aimming to a vehicle.

When I leave the arena (I try to set the Default MTA Camera using setCameraTarget) In this case it doesn't work, so I kill the player, spawn it again and set again the default mta camera.

Obyously I have removed the custom camera events and restore the varaibles previously but nothing.

As I said before, even using onClientPreRender, onClientRender or whatever the camera doesn't work.

Thanks for reading and best Regards.

Link to comment

@IIYAMA

This one. Just using the function setCustomCameraTarget(theElement)

local rotX, rotY, rotRadius, maxRadius = 0, -math.pi/2, 10, 20 
local isCustomCamera = false 
local cameraTarget = nil 
local sX, sY = guiGetScreenSize() 
  
addEventHandler("onClientPreRender", root, 
    function() 
        if isCustomCamera and isElement(cameraTarget) then 
  
            local x, y, z = getElementPosition( cameraTarget ) 
            local cx, cy, cz 
             
            cx = x + rotRadius * math.sin(rotY) * math.cos(rotX) 
            cy = y + rotRadius * math.sin(rotY) * math.sin(rotX) 
            cz = z + rotRadius * math.cos(rotY) 
             
            local hit, hitX, hitY, hitZ = processLineOfSight(x, y, z, cx, cy, cz, _, false, _, _, _, _, _, _, cameraTarget) 
            if hit then 
                cx, cy, cz = hitX, hitY, hitZ 
            end 
  
            setCameraMatrix( cx, cy, cz, x, y, z ) 
        end 
    end 
) 
  
addEventHandler("onClientKey", root, 
    function(button) 
        if button == "mouse_wheel_up" then 
            rotRadius = math.max(2, rotRadius - 1) 
        elseif button == "mouse_wheel_down" then 
            rotRadius = math.min(maxRadius, rotRadius + 1) 
        end 
    end 
) 
  
addEventHandler("onClientCursorMove", root, 
    function(cX,cY,aX,aY) 
        if isCursorShowing() or isMTAWindowActive() then return end 
                 
        aX = aX - sX/2  
        aY = aY - sY/2 
  
        rotX = rotX - aX * 0.01745 * 0.3 
        rotY = math.min(-0.02, math.max( rotY + aY * 0.01745 * 0.3, -3.11 ) ) 
         
    end 
) 
  
function setCustomCameraTarget(element) 
    if isElement(element) then 
        cameraTarget = element 
        isCustomCamera = true 
        return true 
    else 
        cameraTarget = nil 
        isCustomCamera = false 
    end 
end 

 

Link to comment
  • Moderators
8 minutes ago, Overkillz said:

This one.

 

So you did this clientside:

setCustomCameraTarget() 

 

On serverside you did this:

setCameraTarget(player, player)

 

What is the delay between those two actions? Did you try to apply them after each other with a delay of 2 seconds?

 

 

Link to comment

I tried it.

After leaving the arena I did this

setTimer(triggerServerEvent,2000,1,"onForceSpawnLobbyPlayer",localPlayer)

And serverside

function forceSpawnLobbyPlayer()
	outputChatBox("Forcing Camera Matrix")
	spawnPlayer(source, 0, 0, 5, 90, 0,0, 30)
	setElementFrozen(source,true)
	setCameraTarget(source,source)
	setCameraMatrix(source,488.32220458984,-1607.4827880859,37.201072692871,399.98428344727,-1560.7738037109,41.033145904541)
end
addEvent("onForceSpawnLobbyPlayer",true)
addEventHandler( "onForceSpawnLobbyPlayer",getRootElement(),forceSpawnLobbyPlayer)

It is executed, but nothing. Im still not getting the camera player.

Edited by Overkillz
Link to comment
  • Moderators

 

 

 

 

17 minutes ago, Overkillz said:

It is executed, but nothing. I still not getting the camera player.

I see line 6 cancelling line 5? There is absolutely no logic in that.

 

21 minutes ago, Overkillz said:

After leaving the arena I did this

Did you call this function before you did that? (to put a stop to the rendering process)

setCustomCameraTarget() 

 

 

 

Link to comment

How that it is cancelling. Im just doing setCameraTarget just to 'lets say Restore' the default MTA Camera to later apply the camera Matrix. Do I need to restore the camera player fully before I apply the cameraatrix to a specific position ?

And yes, Im executing this function before execute the previous one mentioned.

setCustomCameraTarget()

 

Link to comment
  • Moderators

 

3 minutes ago, Overkillz said:

Do I need to restore the camera player fully before I apply the cameraatrix to a specific position ?

No, but you might need to apply a delay between line 5 and 6, if you think it is necessary.

 

 

 

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