bovine3dom Posted June 17, 2010 Share Posted June 17, 2010 I'm trying to create a cockpit camera (like http://www.ps3informer.com/playstation- ... t-view.jpg ) for a tram. Here's the code so far: (This is all clientside btw) function trainCam() local theVehicle = getPedOccupiedVehicle(getLocalPlayer()) local x, y, z = getElementPosition(getLocalPlayer()) local rx, ry, rz = getElementRotation(getLocalPlayer()) local ry = y+1 local rx = x+(math.tan(math.deg(rz))) setCameraMatrix(x, y, z+1, rx, ry, z+1) end function trainStart(theVehicle, seat) if ((seat == 0) and (getElementModel(theVehicle) == 449)) then addEventHandler("onClientRender", getRootElement(), trainCam) else end end addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), trainStart) function trainStop(theVehicle, seat) if ((seat == 0) and (getElementModel(theVehicle) == 449)) then removeEventHandler("onClientRender", getRootElement(), trainCam) setCameraTarget(getLocalPlayer()) else end end addEventHandler("onClientPlayerVehicleExit", getLocalPlayer(), trainStop) It kind of works along 0* and 90* lines (although it seems to go in fairly random directions), but when you go around a corner, it spins around crazyily. I can't see why though The logic behind the maths: http://j.imagehost.org/0403/MTA.png Where the point (a,b) is the x and y position of the tram, and (a+tan(Z), b+1) is a second point along the direction of travel. I know I haven't made any allowances for instances where the direction of travel is 90* Any ideas/solutions? Thanks. Link to comment
DiSaMe Posted June 18, 2010 Share Posted June 18, 2010 Try changing local rx = x+(math.tan(math.deg(rz))) to local rx = x+math.tan(math.rad(rz)) Link to comment
bovine3dom Posted June 18, 2010 Author Share Posted June 18, 2010 I think I'll skip most of the maths and just use a 'fly on the nose' (i.e. create an invisible object, attach it to the train and have the camera look at that). That way I don't have to worry about the angle of elevation either, and it should be less resource hungry. Btw, the solution to this problem is to use tan(Z) to find the x co-ordinate for -45 Cheers to Doomed_Space_Marine, I didn't realise that the function worked that way. Code I'm using now (for those interested) function trainCam() local x, y, z = getElementPosition(gnat) local cx, cy, cz = getElementPosition(fly) setCameraMatrix(x, y, z, cx, cy, cz) end function trainStart(theVehicle, seat) if ((seat == 0) and (getElementModel(theVehicle) == 449)) then local x, y, z = getElementPosition(getLocalPlayer()) fly = createObject(3803, x, y, z) setElementAlpha(fly, 0) gnat = createObject(3803, x, y, z) setElementAlpha(gnat, 0) attachElements(fly, theVehicle, 0.7, 5, 0.6) attachElements(gnat, theVehicle, 0.7, 3.5, 0.6) addEventHandler("onClientPreRender", getRootElement(), trainCam) else end end addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), trainStart) function trainStop(theVehicle, seat) if ((seat == 0) and (getElementModel(theVehicle) == 449)) then removeEventHandler("onClientPreRender", getRootElement(), trainCam) setCameraTarget(getLocalPlayer()) destroyElement(fly) destroyElement(gnat) else end end addEventHandler("onClientPlayerVehicleExit", getLocalPlayer(), trainStop) 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