Atton Posted October 23, 2014 Share Posted October 23, 2014 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 IIYAMA Posted October 23, 2014 Moderators Share Posted October 23, 2014 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
Atton Posted October 23, 2014 Author Share Posted October 23, 2014 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
Moderators IIYAMA Posted October 23, 2014 Moderators Share Posted October 23, 2014 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. Link to comment
Atton Posted October 23, 2014 Author Share Posted October 23, 2014 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 IIYAMA Posted October 23, 2014 Moderators Share Posted October 23, 2014 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
Atton Posted October 23, 2014 Author Share Posted October 23, 2014 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 IIYAMA Posted October 23, 2014 Moderators Share Posted October 23, 2014 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
ixjf Posted October 23, 2014 Share Posted October 23, 2014 You can probably simplify that code a lot with the Matrix class introduced in MTA 1.4. Link to comment
Moderators IIYAMA Posted October 23, 2014 Moderators Share Posted October 23, 2014 There are not examples for that on the wiki, only that they do exist. Link to comment
ixjf Posted October 23, 2014 Share Posted October 23, 2014 There are a few in the OOP introduction articles, and you can find the whole Matrix class members in the MTA's source code (MTA10/mods/shared_logic/lua/CLuaMain.cpp). Link to comment
Moderators IIYAMA Posted October 24, 2014 Moderators Share Posted October 24, 2014 I found one example: https://wiki.multitheftauto.com/wiki/Ma ... g_Matrices and I already had found the classes: https://wiki.multitheftauto.com/wiki/Matrix But mta source code, I have never looked in to that. Where can I place this location of yours? Link to comment
ixjf Posted October 24, 2014 Share Posted October 24, 2014 The page in the second link was created by me, probably outdated by now. You can find the whole MTA source code here: https://code.google.com/p/mtasa-blue/so ... vn%2Ftrunk Link to comment
DNL291 Posted October 25, 2014 Share Posted October 25, 2014 @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
Atton Posted October 26, 2014 Author Share Posted October 26, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now