I am doing First Person Perspective. When camera roll = 0, everything works fine. But if the camera roll is not equal to 0, then the camera also rotates as if the camera roll is 0.
principle:
addEventHandler ('onClientCursorMove',root, freecamMouse)
function freecamMouse (cX,cY,aX,aY)
-- how far have we moved the mouse from the screen center?
local width, height = guiGetScreenSize()
aX = aX - width / 2
aY = aY - height / 2
rotX = rotX + aX * mouseSensitivity * 0.01745
rotY = rotY - aY * mouseSensitivity * 0.01745
end
addEventHandler ('onClientPreRender', root, updateCamera)
function updateCamera()
local camPosXr, camPosYr, camPosZr = getPedBonePosition (localPlayer, 6)
local camPosXl, camPosYl, camPosZl = getPedBonePosition (localPlayer, 7)
local camPosX, camPosY, camPosZ = (camPosXr + camPosXl) / 2, (camPosYr + camPosYl) / 2, (camPosZr + camPosZl) / 2
-- note the vehicle rotation
if inVehicle then
local rx,ry,rz = getElementRotation(getPedOccupiedVehicle(localPlayer))
roll = -ry
if rx > 90 and rx < 270 then
roll = ry - 180
end
if not wasInVehicle then
rotX = rotX + math.rad(rz)
if rotY > -PI/15 then
rotY = -PI/15
end
end
cameraAngleX = rotX - math.rad(rz)
cameraAngleY = rotY + math.rad(rx)
else
roll = 0
cameraAngleX = rotX
cameraAngleY = rotY
end
--Taken from the freecam resource made by eAi
-- work out an angle in radians based on the number of pixels the cursor has moved (ever)
local freeModeAngleZ = math.sin(cameraAngleY)
local freeModeAngleY = math.cos(cameraAngleY) * math.cos(cameraAngleX)
local freeModeAngleX = math.cos(cameraAngleY) * math.sin(cameraAngleX)
-- calculate a target based on the current position and an offset based on the angle
local camTargetX = camPosX + freeModeAngleX * 100
local camTargetY = camPosY + freeModeAngleY * 100
local camTargetZ = camPosZ + freeModeAngleZ * 100
-- Work out the distance between the target and the camera (should be 100 units)
local camAngleX = camPosX - camTargetX
local camAngleY = camPosY - camTargetY
local camAngleZ = 0 -- we ignore this otherwise our vertical angle affects how fast you can strafe
-- Calulcate the length of the vector
local angleLength = math.sqrt(camAngleX*camAngleX+camAngleY*camAngleY+camAngleZ*camAngleZ)
-- Normalize the vector, ignoring the Z axis, as the camera is stuck to the XY plane (it can't roll)
local camNormalizedAngleX = camAngleX / angleLength
local camNormalizedAngleY = camAngleY / angleLength
local camNormalizedAngleZ = 0
-- We use this as our rotation vector
local normalAngleX = 0
local normalAngleY = 0
local normalAngleZ = 1
-- Perform a cross product with the rotation vector and the normalzied angle
local normalX = (camNormalizedAngleY * normalAngleZ - camNormalizedAngleZ * normalAngleY)
local normalY = (camNormalizedAngleZ * normalAngleX - camNormalizedAngleX * normalAngleZ)
local normalZ = (camNormalizedAngleX * normalAngleY - camNormalizedAngleY * normalAngleX)
-- Update the target based on the new camera position (again, otherwise the camera kind of sways as the target is out by a frame)
camTargetX = camPosX + freeModeAngleX * 100
camTargetY = camPosY + freeModeAngleY * 100
camTargetZ = camPosZ + freeModeAngleZ * 100
-- Set the new camera position and target
setCameraMatrix (camPosX, camPosY, camPosZ, camTargetX, camTargetY, camTargetZ, roll, fov)
end
I think need something like that.
but I don’t know how to do this, because the parameter "roll" in setCameraMatrix rotates only “visually” and not the coordinate system