Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1.  
  2. localPlayer = getLocalPlayer()
  3.  
  4. local function freecamFrame ()
  5. -- work out an angle in radians based on the number of pixels the cursor has moved (ever)
  6. local cameraAngleX = rotX
  7. local cameraAngleY = rotY
  8.  
  9. local freeModeAngleZ = math.sin(cameraAngleY)
  10. local freeModeAngleY = math.cos(cameraAngleY) * math.cos(cameraAngleX)
  11. local freeModeAngleX = math.cos(cameraAngleY) * math.sin(cameraAngleX)
  12. local camPosX, camPosY, camPosZ = getCameraMatrix()
  13.  
  14. -- calculate a target based on the current position and an offset based on the angle
  15. local camTargetX = camPosX + freeModeAngleX * 100
  16. local camTargetY = camPosY + freeModeAngleY * 100
  17. local camTargetZ = camPosZ + freeModeAngleZ * 100
  18.  
  19. -- Calculate what the maximum speed that the camera should be able to move at.
  20. local mspeed = options.normalMaxSpeed
  21. if getKeyState ( options.key_fastMove ) then
  22. mspeed = options.fastMaxSpeed
  23. elseif getKeyState ( options.key_slowMove ) then
  24. mspeed = options.slowMaxSpeed
  25. end
  26.  
  27. if options.smoothMovement then
  28. local acceleration = options.acceleration
  29. local decceleration = options.decceleration
  30.  
  31. -- Check to see if the forwards/backwards keys are pressed
  32. local speedKeyPressed = false
  33. if getKeyState ( options.key_forward ) then
  34. speed = speed + acceleration
  35. speedKeyPressed = true
  36. end
  37. if getKeyState ( options.key_backward ) then
  38. speed = speed - acceleration
  39. speedKeyPressed = true
  40. end
  41.  
  42. -- Check to see if the strafe keys are pressed
  43. local strafeSpeedKeyPressed = false
  44. if getKeyState ( options.key_right ) then
  45. if strafespeed > 0 then -- for instance response
  46. strafespeed = 0
  47. end
  48. strafespeed = strafespeed - acceleration / 2
  49. strafeSpeedKeyPressed = true
  50. end
  51. if getKeyState ( options.key_left ) then
  52. if strafespeed < 0 then -- for instance response
  53. strafespeed = 0
  54. end
  55. strafespeed = strafespeed + acceleration / 2
  56. strafeSpeedKeyPressed = true
  57. end
  58.  
  59. -- If no forwards/backwards keys were pressed, then gradually slow down the movement towards 0
  60. if speedKeyPressed ~= true then
  61. if speed > 0 then
  62. speed = speed - decceleration
  63. elseif speed < 0 then
  64. speed = speed + decceleration
  65. end
  66. end
  67.  
  68. -- If no strafe keys were pressed, then gradually slow down the movement towards 0
  69. if strafeSpeedKeyPressed ~= true then
  70. if strafespeed > 0 then
  71. strafespeed = strafespeed - decceleration
  72. elseif strafespeed < 0 then
  73. strafespeed = strafespeed + decceleration
  74. end
  75. end
  76.  
  77. -- Check the ranges of values - set the speed to 0 if its very close to 0 (stops jittering), and limit to the maximum speed
  78. if speed > -decceleration and speed < decceleration then
  79. speed = 0
  80. elseif speed > mspeed then
  81. speed = mspeed
  82. elseif speed < -mspeed then
  83. speed = -mspeed
  84. end
  85.  
  86. if strafespeed > -(acceleration / 2) and strafespeed < (acceleration / 2) then
  87. strafespeed = 0
  88. elseif strafespeed > mspeed then
  89. strafespeed = mspeed
  90. elseif strafespeed < -mspeed then
  91. strafespeed = -mspeed
  92. end
  93. else
  94. speed = 0
  95. strafespeed = 0
  96. if getKeyState ( options.key_forward ) then speed = mspeed end
  97. if getKeyState ( options.key_backward ) then speed = -mspeed end
  98. if getKeyState ( options.key_left ) then strafespeed = mspeed end
  99. if getKeyState ( options.key_right ) then strafespeed = -mspeed end
  100. end
  101.  
  102. -- Work out the distance between the target and the camera (should be 100 units)
  103. local camAngleX = camPosX - camTargetX
  104. local camAngleY = camPosY - camTargetY
  105. local camAngleZ = 0 -- we ignore this otherwise our vertical angle affects how fast you can strafe
  106.  
  107. -- Calulcate the length of the vector
  108. local angleLength = math.sqrt(camAngleX*camAngleX+camAngleY*camAngleY+camAngleZ*camAngleZ)
  109.  
  110. -- Normalize the vector, ignoring the Z axis, as the camera is stuck to the XY plane (it can't roll)
  111. local camNormalizedAngleX = camAngleX / angleLength
  112. local camNormalizedAngleY = camAngleY / angleLength
  113. local camNormalizedAngleZ = 0
  114.  
  115. -- We use this as our rotation vector
  116. local normalAngleX = 0
  117. local normalAngleY = 0
  118. local normalAngleZ = 1
  119.  
  120. -- Perform a cross product with the rotation vector and the normalzied angle
  121. local normalX = (camNormalizedAngleY * normalAngleZ - camNormalizedAngleZ * normalAngleY)
  122. local normalY = (camNormalizedAngleZ * normalAngleX - camNormalizedAngleX * normalAngleZ)
  123. local normalZ = (camNormalizedAngleX * normalAngleY - camNormalizedAngleY * normalAngleX)
  124.  
  125. -- Update the camera position based on the forwards/backwards speed
  126. camPosX = camPosX + freeModeAngleX * speed
  127. camPosY = camPosY + freeModeAngleY * speed
  128. camPosZ = camPosZ + freeModeAngleZ * speed
  129.  
  130. -- Update the camera position based on the strafe speed
  131. camPosX = camPosX + normalX * strafespeed
  132. camPosY = camPosY + normalY * strafespeed
  133. camPosZ = camPosZ + normalZ * strafespeed
  134.  
  135. --Store the velocity
  136. velocityX = (freeModeAngleX * speed) + (normalX * strafespeed)
  137. velocityY = (freeModeAngleY * speed) + (normalY * strafespeed)
  138. velocityZ = (freeModeAngleZ * speed) + (normalZ * strafespeed)
  139.  
  140. -- Update the target based on the new camera position (again, otherwise the camera kind of sways as the target is out by a frame)
  141. camTargetX = camPosX + freeModeAngleX * 100
  142. camTargetY = camPosY + freeModeAngleY * 100
  143. camTargetZ = camPosZ + freeModeAngleZ * 100
  144.  
  145. -- Set the new camera position and target
  146. setCameraMatrix ( camPosX, camPosY, camPosZ, camTargetX, camTargetY, camTargetZ )
  147. setElementPosition ( localPlayer, camPosX, camPosY, camPosZ )
  148. end
  149.  
  150. local function freecamMouse (cX,cY,aX,aY)
  151. --ignore mouse movement if the cursor or MTA window is on
  152. --and do not resume it until at least 5 frames after it is toggled off
  153. --(prevents cursor mousemove data from reaching this handler)
  154. if isCursorShowing() or isMTAWindowActive() then
  155. mouseFrameDelay = 5
  156. return
  157. elseif mouseFrameDelay > 0 then
  158. mouseFrameDelay = mouseFrameDelay - 1
  159. return
  160. end
  161.  
  162. -- how far have we moved the mouse from the screen center?
  163. local width, height = guiGetScreenSize()
  164. aX = aX - width / 2
  165. aY = aY - height / 2
  166.  
  167. --invert the mouse look if specified
  168. if options.invertMouseLook then
  169. aY = -aY
  170. end
  171.  
  172. rotX = rotX + aX * options.mouseSensitivity * 0.01745
  173. rotY = rotY - aY * options.mouseSensitivity * 0.01745
  174.  
  175. local PI = math.pi
  176. if rotX > PI then
  177. rotX = rotX - 2 * PI
  178. elseif rotX < -PI then
  179. rotX = rotX + 2 * PI
  180. end
  181.  
  182. if rotY > PI then
  183. rotY = rotY - 2 * PI
  184. elseif rotY < -PI then
  185. rotY = rotY + 2 * PI
  186. end
  187. -- limit the camera to stop it going too far up or down - PI/2 is the limit, but we can't let it quite reach that or it will lock up
  188. -- and strafeing will break entirely as the camera loses any concept of what is 'up'
  189. if rotY < -PI / 2.05 then
  190. rotY = -PI / 2.05
  191. elseif rotY > PI / 2.05 then
  192. rotY = PI / 2.05
  193. end
  194. end
  195.  
  196.  
  197. startX, startY, startZ = getElementPosition(localPlayer)
  198. addEventHandler("onClientRender", rootElement, freecamFrame)
  199. addEventHandler("onClientCursorMove",rootElement, freecamMouse)
  200. setElementData(localPlayer, "freecam:state", true, false)
  201. setPedWeaponSlot(localPlayer, 0)
  202. toggleAllControls(false, true, false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement