Jump to content

Following Cam


Atton

Recommended Posts

How would it be possible to create a cam that follows on the front of the truck. Without it messing up like if you stuck a cam on the bumper of a car.

  
car = createVehicle(578,x,y,z) 
function getPositionInfrontOfElement ( element , meters ) 
    if not element or not isElement ( element ) then 
        return false 
    end 
    if not meters then 
       meters = 3 
    end 
    local posX , posY , posZ = getElementPosition ( element ) 
    local _ , _ , rotation = getElementRotation ( element ) 
    posX = posX - math.sin ( math.rad ( rotation ) ) * meters 
    posY = posY + math.cos ( math.rad ( rotation ) ) * meters 
    return posX , posY , posZ 
end 
  
function cam () 
local x,y,z = getPositionInfrontOfElement(car,4) 
    setCameraTarget(car) 
    --setCameraTarget(x,y,z) 
    setCameraMatrix(x,y,z+2,x,y,z,0,0) 
end 
  

Link to comment
  • Moderators

https://wiki.multitheftauto.com/wiki/GetElementMatrix

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 

Get your offset and put your camera on it. Be creative.

Link to comment
https://wiki.multitheftauto.com/wiki/GetElementMatrix
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 

Get your offset and put your camera on it. Be creative.

It does not really seem to be really helpful.

Link to comment
wut? :?:

If this isn't what you want, explain better.

If you do not understand what these lines do, ask me how they work.

and anyway explain it better, I am at the moment only gambling how you want this cam.

I want to glue a cam onto the bumper of a car. My attempts to use the cam functions did not work.

  
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 
  
function cam () 
    local offX,offY,offZ = getElementPosition(car) 
    local x,y,z = getPositionFromElementOffset(car,offX,offY,offZ) 
    setCameraMatrix(x,y,z) 
    setCameraTarget(x,y,z) 
end 

Link to comment
  • Moderators

offset = not the car position.......

offset is the offset you want to set from the car it self.

The car position is already within the matrix of the car.

function cam () 
    local x,y,z = getElementPosition(car) 
    local offX,offY,offZ = getPositionFromElementOffset(car,5,0,0) -- 5 units offset at the X as. 
    setCameraMatrix(offX,offY,offZ,x,y,z) -- facing the car. 
end 

Link to comment
offset = not the car position.......

offset is the offset you want to set from the car it self.

The car position is already within the matrix of the car.

function cam () 
    local x,y,z = getElementPosition(car) 
    local offX,offY,offZ = getPositionFromElementOffset(car,5,0,0) -- 5 units offset at the X as. 
    setCameraMatrix(offX,offY,offZ,x,y,z) -- facing the car. 
end 

It's a bit off but thanks.

Link to comment
  • Moderators

well you have to change the offset to what you want.

You can also do this:

local offX,offY,offZ = getPositionFromElementOffset(car,5,0,0)  
  
local offX2,offY2,offZ2 = getPositionFromElementOffset(car,0,5,0)  
  
setCameraMatrix(offX,offY,offZ,offX2,offY2,offZ2) 

Which makes it possible to attach the camera in every position you want.

and if you want to rotate the camera when it is attached to the vehicle.

See wiki example: https://wiki.multitheftauto.com/wiki/GetCamera

cam = getCamera() 
setElementPosition( cam, 0,0,0 )  -- Clear camera target 
myVehicle = getPedOccupiedVehicle(localPlayer) 
attachElements( cam, myVehicle, 0,-4,2, -20,0,0 ) 

Link to comment

@Atton: You can use getVehicleComponentPosition + getPositionFromElementOffset (provided by IIYAMA)

And set the camera's position and the look at with that.

Example using the front bumper:

local cx, cy, cz = getVehicleComponentPosition(theVehicle, "bump_front_dummy") 
  
local lookAtX, lookAtY, lookAtZ = getPositionFromElementOffset(theVehicle, cx - .5, cy, cz) 
-- Gets the center of the bumper component 
local camX, camY, camZ = getPositionFromElementOffset(theVehicle, cx - .5, cy + 2.3, cz + .3) 
-- Gets the positions for the camera 
  
setCameraMatrix(camX, camY, camZ, lookAtX, lookAtY, lookAtZ) 

Note:

Some vehicles doesn't have the component: "bump_front_dummy", and some vehicles returns the position of the bumper inverted (and it should be: cx + .5 instead).

Link to comment

I do not know if I am thick in the head or something but it never want's work in a straight line.

local x,y,z = 1427,-2494,15 
car = createVehicle(578,x,y,z) 
setVehicleDamageProof(car,true) 
pedd = createPed(0,x,y,z+69) 
warpPedIntoVehicle(pedd,car) 
setPedAnalogControlState (pedd, 'accelerate', 0.01) 
createBlipAttachedTo(car,0) 
setElementStreamable(car,false) 
setElementStreamable(pedd,false) 
    a,b,c = 0,50,0 
fw = "num_8" 
bw = "num_2" 
le = "num_4" 
ri = "num_6" 
upk = "num_9" 
downk = "num_3" 
  
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    
  
  
function foward () 
    if getKeyState(fw) == true then 
        setPedControlState(pedd,"accelerate",true) 
    end 
    if getKeyState(fw) == false then 
        setPedControlState(pedd,"accelerate",false) 
    end 
end 
  
function back () 
    if getKeyState(bw) == true then 
        setPedControlState(pedd,"brake_reverse",true) 
    end 
    if getKeyState(bw) == false then 
        setPedControlState(pedd,"brake_reverse",false) 
    end 
end 
  
function left () 
    if getKeyState(le) == true then 
        setPedControlState(pedd,"vehicle_left",true) 
    end 
    if getKeyState(le) == false then 
        setPedControlState(pedd,"vehicle_left",false) 
    end 
end 
  
function right () 
    if getKeyState(ri) == true then 
        setPedControlState(pedd,"vehicle_right",true) 
    end 
    if getKeyState(ri) == false then 
        setPedControlState(pedd,"vehicle_right",false) 
    end 
end 
  
function up () 
    if getKeyState(upk) == true then 
        setPedControlState(pedd,"special_control_up",true) 
    end 
    if getKeyState(upk) == false then 
        setPedControlState(pedd,"special_control_up",false) 
    end 
end 
  
function down () 
    if getKeyState(downk) == true then 
        setPedControlState(pedd,"special_control_down",true) 
    end 
    if getKeyState(downk) == false then 
        setPedControlState(pedd,"special_control_down",false) 
    end 
end 
  
function cam () 
    local cx, cy, cz = getVehicleComponentPosition(car, "windscreen_dummy") 
--  outputChatBox(tostring(cx).." "..tostring(cy).." "..tostring(cz)) 
    local lookAtX, lookAtY, lookAtZ = getPositionFromElementOffset(car, cx - .5, cy, cz) 
    -- Gets the center of the bumper component 
    local camX, camY, camZ = getPositionFromElementOffset(car, cx - 0.5, cy + 2.3, cz + .3) 
    -- Gets the positions for the camera 
    setCameraMatrix(camX, camY, camZ, lookAtX+a, lookAtY+b, lookAtZ+c) 
end 
  
function timeSystem () 
    foward () 
    back () 
    right () 
    left () 
    up () 
    down () 
    cam () 
end 
  
dswitch = false 
function switch () 
    if not dswitch then 
        addEventHandler ("onClientRender", root, timeSystem)  
        dswitch = true 
        elseif dswitch then 
        setCameraTarget(localPlayer) 
        dswitch = false 
        removeEventHandler ("onClientRender", root, timeSystem)  
    end 
end 
addCommandHandler("onoff",switch) 
--[[ 
for k in pairs (getVehicleComponents(car)) do 
        outputChatBox(tostring(k)) 
end 
]]-- 
  
function change (nill,x,y,z) 
    a,b,c = tonumber(x),tonumber(y),tonumber(z) 
    outputChatBox(tostring(x)..tostring(y)..tostring(z)) 
end 
addCommandHandler("set",change) 

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