camera.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/camera.h"
  24. #include "math/mMath.h"
  25. #include "core/stream/bitStream.h"
  26. #include "T3D/fx/cameraFXMgr.h"
  27. #include "T3D/gameBase/gameConnection.h"
  28. #include "math/mathIO.h"
  29. #include "gui/worldEditor/editor.h"
  30. #include "console/engineAPI.h"
  31. #include "console/consoleTypes.h"
  32. #include "console/engineAPI.h"
  33. #include "math/mathUtils.h"
  34. #include "math/mTransform.h"
  35. #ifdef TORQUE_EXTENDED_MOVE
  36. #include "T3D/gameBase/extended/extendedMove.h"
  37. #endif
  38. S32 Camera::smExtendedMovePosRotIndex = 0; // The ExtendedMove position/rotation index used for camera movements
  39. #define MaxPitch 1.5706f
  40. #define CameraRadius 0.05f;
  41. ImplementEnumType( CameraMotionMode,
  42. "Movement behavior type for Camera.\n\n"
  43. "@ingroup BaseCamera" )
  44. { Camera::StationaryMode, "Stationary", "Camera does not rotate or move." },
  45. { Camera::FreeRotateMode, "FreeRotate", "Camera may rotate but does not move." },
  46. { Camera::FlyMode, "Fly", "Camera may rotate and move freely." },
  47. { Camera::OrbitObjectMode, "OrbitObject", "Camera orbits about a given object. Damage flash and white out is determined by the object being orbited. See Camera::setOrbitMode() to set the orbit object and other parameters." },
  48. { Camera::OrbitPointMode, "OrbitPoint", "Camera orbits about a given point. See Camera::setOrbitMode() to set the orbit point and other parameters." },
  49. { Camera::TrackObjectMode, "TrackObject", "Camera always faces a given object. See Camera::setTrackObject() to set the object to track and a distance to remain from the object." },
  50. { Camera::OverheadMode, "Overhead", "Camera moves in the XY plane." },
  51. { Camera::EditOrbitMode, "EditOrbit", "Used by the World Editor to orbit about a point. When first activated, the camera is rotated to face the orbit point rather than move to it." }
  52. EndImplementEnumType;
  53. //=============================================================================
  54. // CameraData.
  55. //=============================================================================
  56. // MARK: ---- CameraData ----
  57. IMPLEMENT_CO_DATABLOCK_V1( CameraData );
  58. ConsoleDocClass( CameraData,
  59. "@brief A datablock that describes a camera.\n\n"
  60. "@tsexample\n"
  61. "datablock CameraData(Observer)\n"
  62. "{\n"
  63. " mode = \"Observer\";\n"
  64. "};\n"
  65. "@endtsexample\n"
  66. "@see Camera\n\n"
  67. "@ref Datablock_Networking\n"
  68. "@ingroup BaseCamera\n"
  69. "@ingroup Datablocks\n"
  70. );
  71. //-----------------------------------------------------------------------------
  72. void CameraData::initPersistFields()
  73. {
  74. Parent::initPersistFields();
  75. }
  76. //-----------------------------------------------------------------------------
  77. void CameraData::packData(BitStream* stream)
  78. {
  79. Parent::packData(stream);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void CameraData::unpackData(BitStream* stream)
  83. {
  84. Parent::unpackData(stream);
  85. }
  86. //=============================================================================
  87. // Camera.
  88. //=============================================================================
  89. // MARK: ---- Camera ----
  90. IMPLEMENT_CO_NETOBJECT_V1( Camera );
  91. ConsoleDocClass( Camera,
  92. "@brief Represents a position, direction and field of view to render a scene from.\n\n"
  93. "A camera is typically manipulated by a GameConnection. When set as the connection's "
  94. "control object, the camera handles all movement actions ($mvForwardAction, $mvPitch, etc.) "
  95. "just like a Player.\n"
  96. "@tsexample\n"
  97. "// Set an already created camera as the GameConnection's control object\n"
  98. "%connection.setControlObject(%camera);\n"
  99. "@endtsexample\n\n"
  100. "<h3>Methods of Operation</h3>\n\n"
  101. "The camera has two general methods of operation. The first is the standard mode where "
  102. "the camera starts and stops its motion and rotation instantly. This is the default operation "
  103. "of the camera and is used by most games. It may be specifically set with Camera::setFlyMode() "
  104. "for 6 DoF motion. It is also typically the method used with Camera::setOrbitMode() or one of "
  105. "its helper methods to orbit about a specific object (such as the Player's dead body) or a "
  106. "specific point.\n\n"
  107. "The second method goes under the name of Newton as it follows Newton's 2nd law of "
  108. "motion: F=ma. This provides the camera with an ease-in and ease-out feel for both movement "
  109. "and rotation. To activate this method for movement, either use Camera::setNewtonFlyMode() or set "
  110. "the Camera::newtonMode field to true. To activate this method for rotation, set the Camera::newtonRotation "
  111. "to true. This method of operation is not typically used in games, and was developed to allow "
  112. "for a smooth fly through of a game level while recording a demo video. But with the right force "
  113. "and drag settings, it may give a more organic feel to the camera to games that use an overhead view, "
  114. "such as a RTS.\n\n"
  115. "There is a third, minor method of operation but it is not generally used for games. This is when the "
  116. "camera is used with Torque's World Editor in Edit Orbit Mode. When set, this allows the camera "
  117. "to rotate about a specific point in the world, and move towards and away from this point. See "
  118. "Camera::setEditOrbitMode() and Camera::setEditOrbitPoint(). While in this mode, Camera::autoFitRadius() "
  119. "may also be used.\n\n"
  120. "@tsexample\n"
  121. "// Create a camera in the level and set its position to a given spawn point.\n"
  122. "// Note: The camera starts in the standard fly mode.\n"
  123. "%cam = new Camera() {\n"
  124. " datablock = \"Observer\";\n"
  125. "};\n"
  126. "MissionCleanup.add( %cam );\n"
  127. "%cam.setTransform( %spawnPoint.getTransform() );\n"
  128. "@endtsexample\n\n"
  129. "@tsexample\n"
  130. "// Create a camera at the given spawn point for the specified\n"
  131. "// GameConnection i.e. the client. Uses the standard\n"
  132. "// Sim::spawnObject() function to create the camera using the\n"
  133. "// defined default settings.\n"
  134. "// Note: The camera starts in the standard fly mode.\n"
  135. "function GameConnection::spawnCamera(%this, %spawnPoint)\n"
  136. "{\n"
  137. " // Set the control object to the default camera\n"
  138. " if (!isObject(%this.camera))\n"
  139. " {\n"
  140. " if (isDefined(\"$Game::DefaultCameraClass\"))\n"
  141. " %this.camera = spawnObject($Game::DefaultCameraClass, $Game::DefaultCameraDataBlock);\n"
  142. " }\n"
  143. "\n"
  144. " // If we have a camera then set up some properties\n"
  145. " if (isObject(%this.camera))\n"
  146. " {\n"
  147. " // Make sure we're cleaned up when the mission ends\n"
  148. " MissionCleanup.add( %this.camera );\n"
  149. "\n"
  150. " // Make sure the camera is always in scope for the connection\n"
  151. " %this.camera.scopeToClient(%this);\n"
  152. "\n"
  153. " // Send all user input from the connection to the camera\n"
  154. " %this.setControlObject(%this.camera);\n"
  155. "\n"
  156. " if (isDefined(\"%spawnPoint\"))\n"
  157. " {\n"
  158. " // Attempt to treat %spawnPoint as an object, such as a\n"
  159. " // SpawnSphere class.\n"
  160. " if (getWordCount(%spawnPoint) == 1 && isObject(%spawnPoint))\n"
  161. " {\n"
  162. " %this.camera.setTransform(%spawnPoint.getTransform());\n"
  163. " }\n"
  164. " else\n"
  165. " {\n"
  166. " // Treat %spawnPoint as an AngleAxis transform\n"
  167. " %this.camera.setTransform(%spawnPoint);\n"
  168. " }\n"
  169. " }\n"
  170. " }\n"
  171. "}\n"
  172. "@endtsexample\n\n"
  173. "<h3>Motion Modes</h3>\n\n"
  174. "Beyond the different operation methods, the Camera may be set to one of a number "
  175. "of motion modes. These motion modes determine how the camera will respond to input "
  176. "and may be used to constrain how the Camera moves. The CameraMotionMode enumeration "
  177. "defines the possible set of modes and the Camera's current may be obtained by using "
  178. "getMode().\n\n"
  179. "Some of the motion modes may be set using specific script methods. These often provide "
  180. "additional parameters to set up the mode in one go. Otherwise, it is always possible to "
  181. "set a Camera's motion mode using the controlMode property. Just pass in the name of the "
  182. "mode enum. The following table lists the motion modes, how to set them up, and what they offer:\n\n"
  183. "<table border='1' cellpadding='1'>"
  184. "<tr><th>Mode</th><th>Set From Script</th><th>Input Move</th><th>Input Rotate</th><th>Can Use Newton Mode?</th></tr>"
  185. "<tr><td>Stationary</td><td>controlMode property</td><td>No</td><td>No</td><td>No</td></tr>"
  186. "<tr><td>FreeRotate</td><td>controlMode property</td><td>No</td><td>Yes</td><td>Rotate Only</td></tr>"
  187. "<tr><td>Fly</td><td>setFlyMode()</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>"
  188. "<tr><td>OrbitObject</td><td>setOrbitMode()</td><td>Orbits object</td><td>Points to object</td><td>Move only</td></tr>"
  189. "<tr><td>OrbitPoint</td><td>setOrbitPoint()</td><td>Orbits point</td><td>Points to location</td><td>Move only</td></tr>"
  190. "<tr><td>TrackObject</td><td>setTrackObject()</td><td>No</td><td>Points to object</td><td>Yes</td></tr>"
  191. "<tr><td>Overhead</td><td>controlMode property</td><td>Yes</td><td>No</td><td>Yes</td></tr>"
  192. "<tr><td>EditOrbit (object selected)</td><td>setEditOrbitMode()</td><td>Orbits object</td><td>Points to object</td><td>Move only</td></tr>"
  193. "<tr><td>EditOrbit (no object)</td><td>setEditOrbitMode()</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>"
  194. "</table>\n\n"
  195. "<h3>%Trigger Input</h3>\n\n"
  196. "Passing a move trigger ($mvTriggerCount0, $mvTriggerCount1, etc.) on to a Camera performs "
  197. "different actions depending on which mode the camera is in. While in Fly, Overhead or "
  198. "EditOrbit mode, either trigger0 or trigger1 will cause a camera to move twice its normal "
  199. "movement speed. You can see this in action within the World Editor, where holding down the "
  200. "left mouse button while in mouse look mode (right mouse button is also down) causes the Camera "
  201. "to move faster.\n\n"
  202. "Passing along trigger2 will put the camera into strafe mode. While in this mode a Fly, "
  203. "FreeRotate or Overhead Camera will not rotate from the move input. Instead the yaw motion "
  204. "will be applied to the Camera's x motion, and the pitch motion will be applied to the Camera's "
  205. "z motion. You can see this in action within the World Editor where holding down the middle mouse "
  206. "button allows the user to move the camera up, down and side-to-side.\n\n"
  207. "While the camera is operating in Newton Mode, trigger0 and trigger1 behave slightly differently. "
  208. "Here trigger0 activates a multiplier to the applied acceleration force as defined by speedMultiplier. "
  209. "This has the affect of making the camera move up to speed faster. trigger1 has the opposite affect "
  210. "by acting as a brake. When trigger1 is active a multiplier is added to the Camera's drag as "
  211. "defined by brakeMultiplier.\n\n"
  212. "@see CameraData\n"
  213. "@see CameraMotionMode\n"
  214. "@see Camera::movementSpeed\n\n"
  215. "@ingroup BaseCamera\n"
  216. );
  217. F32 Camera::smMovementSpeed = 40.0f;
  218. //----------------------------------------------------------------------------
  219. Camera::Camera()
  220. {
  221. mNetFlags.clear(Ghostable);
  222. mTypeMask |= CameraObjectType;
  223. mDataBlock = 0;
  224. mDelta.pos = Point3F(0.0f, 0.0f, 100.0f);
  225. mDelta.rot = Point3F(0.0f, 0.0f, 0.0f);
  226. mDelta.posVec = mDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  227. mObjToWorld.setColumn(3, mDelta.pos);
  228. mRot = mDelta.rot;
  229. mOffset.set(0.0f, 0.0f, 0.0f);
  230. mMinOrbitDist = 0.0f;
  231. mMaxOrbitDist = 0.0f;
  232. mCurOrbitDist = 0.0f;
  233. mOrbitObject = NULL;
  234. mPosition.set(0.0f, 0.0f, 0.0f);
  235. mObservingClientObject = false;
  236. mMode = FlyMode;
  237. mLastAbsoluteYaw = 0.0f;
  238. mLastAbsolutePitch = 0.0f;
  239. mLastAbsoluteRoll = 0.0f;
  240. // For NewtonFlyMode
  241. mNewtonRotation = false;
  242. mAngularVelocity.set(0.0f, 0.0f, 0.0f);
  243. mAngularForce = 100.0f;
  244. mAngularDrag = 2.0f;
  245. mVelocity.set(0.0f, 0.0f, 0.0f);
  246. mNewtonMode = false;
  247. mMass = 10.0f;
  248. mDrag = 2.0;
  249. mFlyForce = 500.0f;
  250. mSpeedMultiplier = 2.0f;
  251. mBrakeMultiplier = 2.0f;
  252. // For EditOrbitMode
  253. mValidEditOrbitPoint = false;
  254. mEditOrbitPoint.set(0.0f, 0.0f, 0.0f);
  255. mCurrentEditOrbitDist = 2.0;
  256. mLocked = false;
  257. }
  258. //----------------------------------------------------------------------------
  259. Camera::~Camera()
  260. {
  261. }
  262. //----------------------------------------------------------------------------
  263. bool Camera::onAdd()
  264. {
  265. if(!Parent::onAdd() || !mDataBlock)
  266. return false;
  267. mObjBox.maxExtents = mObjScale;
  268. mObjBox.minExtents = mObjScale;
  269. mObjBox.minExtents.neg();
  270. resetWorldBox();
  271. addToScene();
  272. scriptOnAdd();
  273. return true;
  274. }
  275. //----------------------------------------------------------------------------
  276. void Camera::onRemove()
  277. {
  278. scriptOnRemove();
  279. removeFromScene();
  280. Parent::onRemove();
  281. }
  282. //----------------------------------------------------------------------------
  283. bool Camera::onNewDataBlock( GameBaseData *dptr, bool reload )
  284. {
  285. mDataBlock = dynamic_cast<CameraData*>(dptr);
  286. if ( !mDataBlock || !Parent::onNewDataBlock( dptr, reload ) )
  287. return false;
  288. scriptOnNewDataBlock();
  289. return true;
  290. }
  291. //----------------------------------------------------------------------------
  292. void Camera::onEditorEnable()
  293. {
  294. mNetFlags.set(Ghostable);
  295. }
  296. //----------------------------------------------------------------------------
  297. void Camera::onEditorDisable()
  298. {
  299. mNetFlags.clear(Ghostable);
  300. }
  301. //----------------------------------------------------------------------------
  302. // check if the object needs to be observed through its own camera...
  303. void Camera::getCameraTransform(F32* pos, MatrixF* mat)
  304. {
  305. // The camera doesn't support a third person mode,
  306. // so we want to override the default ShapeBase behavior.
  307. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  308. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  309. obj->getCameraTransform(pos, mat);
  310. else
  311. getRenderEyeTransform(mat);
  312. // Apply Camera FX.
  313. mat->mul( gCamFXMgr.getTrans() );
  314. }
  315. void Camera::getEyeCameraTransform(IDisplayDevice *displayDevice, U32 eyeId, MatrixF *outMat)
  316. {
  317. // The camera doesn't support a third person mode,
  318. // so we want to override the default ShapeBase behavior.
  319. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  320. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  321. obj->getEyeCameraTransform(displayDevice, eyeId, outMat);
  322. else
  323. {
  324. Parent::getEyeCameraTransform(displayDevice, eyeId, outMat);
  325. }
  326. }
  327. //----------------------------------------------------------------------------
  328. F32 Camera::getCameraFov()
  329. {
  330. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  331. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  332. return(obj->getCameraFov());
  333. else
  334. return(Parent::getCameraFov());
  335. }
  336. //----------------------------------------------------------------------------
  337. F32 Camera::getDefaultCameraFov()
  338. {
  339. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  340. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  341. return(obj->getDefaultCameraFov());
  342. else
  343. return(Parent::getDefaultCameraFov());
  344. }
  345. //----------------------------------------------------------------------------
  346. bool Camera::isValidCameraFov(F32 fov)
  347. {
  348. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  349. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  350. return(obj->isValidCameraFov(fov));
  351. else
  352. return(Parent::isValidCameraFov(fov));
  353. }
  354. //----------------------------------------------------------------------------
  355. void Camera::setCameraFov(F32 fov)
  356. {
  357. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  358. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  359. obj->setCameraFov(fov);
  360. else
  361. Parent::setCameraFov(fov);
  362. }
  363. //----------------------------------------------------------------------------
  364. void clampPitchAngle(F32 &pitch)
  365. {
  366. // Clamp pitch to +/-MaxPitch, but allow pitch=PI as it is used by some editor
  367. // views (bottom, front, right)
  368. if ((pitch > MaxPitch) && !mIsEqual(pitch, M_PI_F, 0.001f))
  369. pitch = MaxPitch;
  370. else if (pitch < -MaxPitch)
  371. pitch = -MaxPitch;
  372. }
  373. //----------------------------------------------------------------------------
  374. void Camera::processTick(const Move* move)
  375. {
  376. Parent::processTick(move);
  377. if ( isMounted() )
  378. {
  379. // Update SceneContainer.
  380. updateContainer();
  381. return;
  382. }
  383. Point3F vec,pos;
  384. if (move)
  385. {
  386. bool strafeMode = move->trigger[2];
  387. // If using editor then force camera into fly mode, unless using EditOrbitMode
  388. if(gEditingMission && mMode != FlyMode && mMode != EditOrbitMode)
  389. setFlyMode();
  390. // Massage the mode if we're in EditOrbitMode
  391. CameraMotionMode virtualMode = mMode;
  392. if(mMode == EditOrbitMode)
  393. {
  394. if(!mValidEditOrbitPoint)
  395. {
  396. virtualMode = FlyMode;
  397. }
  398. else
  399. {
  400. // Reset any Newton camera velocities for when we switch
  401. // out of EditOrbitMode.
  402. mNewtonRotation = false;
  403. mVelocity.set(0.0f, 0.0f, 0.0f);
  404. mAngularVelocity.set(0.0f, 0.0f, 0.0f);
  405. }
  406. }
  407. // Update orientation
  408. mDelta.rotVec = mRot;
  409. VectorF rotVec(0, 0, 0);
  410. bool doStandardMove = true;
  411. #ifdef TORQUE_EXTENDED_MOVE
  412. GameConnection* con = getControllingClient();
  413. // Work with an absolute rotation from the ExtendedMove class?
  414. if(con && con->getControlSchemeAbsoluteRotation())
  415. {
  416. doStandardMove = false;
  417. const ExtendedMove* emove = dynamic_cast<const ExtendedMove*>(move);
  418. U32 emoveIndex = smExtendedMovePosRotIndex;
  419. if(emoveIndex >= ExtendedMove::MaxPositionsRotations)
  420. emoveIndex = 0;
  421. if(emove->EulerBasedRotation[emoveIndex])
  422. {
  423. if(virtualMode != StationaryMode &&
  424. virtualMode != TrackObjectMode &&
  425. (!mLocked || virtualMode != OrbitObjectMode && virtualMode != OrbitPointMode))
  426. {
  427. // Pitch
  428. mRot.x += (emove->rotX[emoveIndex] - mLastAbsolutePitch);
  429. // Do we also include the relative pitch value?
  430. if(con->getControlSchemeAddPitchToAbsRot() && !strafeMode)
  431. {
  432. F32 x = move->pitch;
  433. if (x > M_PI_F)
  434. x -= M_2PI_F;
  435. mRot.x += x;
  436. }
  437. // Constrain the range of mRot.x
  438. while (mRot.x < -M_PI_F)
  439. mRot.x += M_2PI_F;
  440. while (mRot.x > M_PI_F)
  441. mRot.x -= M_2PI_F;
  442. // Yaw
  443. mRot.z += (emove->rotZ[emoveIndex] - mLastAbsoluteYaw);
  444. // Do we also include the relative yaw value?
  445. if(con->getControlSchemeAddYawToAbsRot() && !strafeMode)
  446. {
  447. F32 z = move->yaw;
  448. if (z > M_PI_F)
  449. z -= M_2PI_F;
  450. mRot.z += z;
  451. }
  452. // Constrain the range of mRot.z
  453. while (mRot.z < -M_PI_F)
  454. mRot.z += M_2PI_F;
  455. while (mRot.z > M_PI_F)
  456. mRot.z -= M_2PI_F;
  457. mLastAbsoluteYaw = emove->rotZ[emoveIndex];
  458. mLastAbsolutePitch = emove->rotX[emoveIndex];
  459. mLastAbsoluteRoll = emove->rotY[emoveIndex];
  460. // Bank
  461. mRot.y = emove->rotY[emoveIndex];
  462. // Constrain the range of mRot.y
  463. while (mRot.y > M_PI_F)
  464. mRot.y -= M_2PI_F;
  465. }
  466. }
  467. }
  468. #endif
  469. if(doStandardMove)
  470. {
  471. // process input/determine rotation vector
  472. if(virtualMode != StationaryMode &&
  473. virtualMode != TrackObjectMode &&
  474. (!mLocked || ((virtualMode != OrbitObjectMode) && (virtualMode != OrbitPointMode))))
  475. {
  476. if(!strafeMode)
  477. {
  478. rotVec.x = move->pitch;
  479. rotVec.z = move->yaw;
  480. }
  481. }
  482. else if(virtualMode == TrackObjectMode && bool(mOrbitObject))
  483. {
  484. // orient the camera to face the object
  485. Point3F objPos;
  486. // If this is a shapebase, use its render eye transform
  487. // to avoid jittering.
  488. ShapeBase *shape = dynamic_cast<ShapeBase*>((GameBase*)mOrbitObject);
  489. if( shape != NULL )
  490. {
  491. MatrixF ret;
  492. shape->getRenderEyeTransform( &ret );
  493. objPos = ret.getPosition();
  494. }
  495. else
  496. {
  497. mOrbitObject->getWorldBox().getCenter(&objPos);
  498. }
  499. mObjToWorld.getColumn(3,&pos);
  500. vec = objPos - pos;
  501. vec.normalizeSafe();
  502. F32 pitch, yaw;
  503. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  504. rotVec.x = -pitch - mRot.x;
  505. rotVec.z = yaw - mRot.z;
  506. if(rotVec.z > M_PI_F)
  507. rotVec.z -= M_2PI_F;
  508. else if(rotVec.z < -M_PI_F)
  509. rotVec.z += M_2PI_F;
  510. }
  511. // apply rotation vector according to physics rules
  512. if(mNewtonRotation)
  513. {
  514. const F32 force = mAngularForce;
  515. const F32 drag = mAngularDrag;
  516. VectorF acc(0.0f, 0.0f, 0.0f);
  517. rotVec.x *= 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  518. rotVec.z *= 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  519. F32 rotVecL = rotVec.len();
  520. if(rotVecL > 0)
  521. {
  522. acc = (rotVec * force / mMass) * TickSec;
  523. }
  524. // Accelerate
  525. mAngularVelocity += acc;
  526. // Drag
  527. mAngularVelocity -= mAngularVelocity * drag * TickSec;
  528. // Rotate
  529. mRot += mAngularVelocity * TickSec;
  530. clampPitchAngle(mRot.x);
  531. }
  532. else
  533. {
  534. mRot.x += rotVec.x;
  535. mRot.z += rotVec.z;
  536. clampPitchAngle(mRot.x);
  537. }
  538. }
  539. // Update position
  540. VectorF posVec(0, 0, 0);
  541. bool mustValidateEyePoint = false;
  542. bool serverInterpolate = false;
  543. // process input/determine translation vector
  544. if(virtualMode == OrbitObjectMode || virtualMode == OrbitPointMode)
  545. {
  546. pos = mDelta.pos;
  547. if(virtualMode == OrbitObjectMode && bool(mOrbitObject))
  548. {
  549. // If this is a shapebase, use its render eye transform
  550. // to avoid jittering.
  551. GameBase *castObj = mOrbitObject;
  552. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  553. if( shape != NULL )
  554. {
  555. MatrixF ret;
  556. shape->getRenderEyeTransform( &ret );
  557. mPosition = ret.getPosition();
  558. }
  559. else
  560. {
  561. // Hopefully this is a static object that doesn't move,
  562. // because the worldbox doesn't get updated between ticks.
  563. mOrbitObject->getWorldBox().getCenter(&mPosition);
  564. }
  565. }
  566. posVec = (mPosition + mOffset) - pos;
  567. mustValidateEyePoint = true;
  568. serverInterpolate = mNewtonMode;
  569. }
  570. else if(virtualMode == EditOrbitMode && mValidEditOrbitPoint)
  571. {
  572. bool faster = move->trigger[0] || move->trigger[1];
  573. F32 scale = smMovementSpeed * (faster + 1);
  574. mCurrentEditOrbitDist -= move->y * TickSec * scale;
  575. mCurrentEditOrbitDist -= move->roll * TickSec * scale; // roll will be -Pi to Pi and we'll attempt to scale it here to be in line with the move->y calculation above
  576. if(mCurrentEditOrbitDist < 0.0f)
  577. mCurrentEditOrbitDist = 0.0f;
  578. mPosition = mEditOrbitPoint;
  579. _setPosition(mPosition, mRot);
  580. _calcEditOrbitPoint(&mObjToWorld, mRot);
  581. pos = mPosition;
  582. }
  583. else if(virtualMode == FlyMode)
  584. {
  585. bool faster = move->trigger[0] || move->trigger[1];
  586. F32 scale = smMovementSpeed * (faster + 1);
  587. mObjToWorld.getColumn(3,&pos);
  588. mObjToWorld.getColumn(0,&vec);
  589. posVec = vec * move->x * TickSec * scale + vec * (strafeMode ? move->yaw * 2.0f * TickSec * scale : 0.0f);
  590. mObjToWorld.getColumn(1,&vec);
  591. posVec += vec * move->y * TickSec * scale + vec * move->roll * TickSec * scale;
  592. mObjToWorld.getColumn(2,&vec);
  593. posVec += vec * move->z * TickSec * scale - vec * (strafeMode ? move->pitch * 2.0f * TickSec * scale : 0.0f);
  594. }
  595. else if(virtualMode == OverheadMode)
  596. {
  597. bool faster = move->trigger[0] || move->trigger[1];
  598. F32 scale = smMovementSpeed * (faster + 1);
  599. mObjToWorld.getColumn(3,&pos);
  600. mObjToWorld.getColumn(0,&vec);
  601. vec.z = 0;
  602. vec.normalizeSafe();
  603. vec = vec * move->x * TickSec * scale + (strafeMode ? vec * move->yaw * 2.0f * TickSec * scale : Point3F(0, 0, 0));
  604. posVec = vec;
  605. mObjToWorld.getColumn(1,&vec);
  606. vec.z = 0;
  607. if (vec.isZero())
  608. {
  609. mObjToWorld.getColumn(2,&vec);
  610. vec.z = 0;
  611. }
  612. vec.normalizeSafe();
  613. vec = vec * move->y * TickSec * scale - (strafeMode ? vec * move->pitch * 2.0f * TickSec * scale : Point3F(0, 0, 0));
  614. posVec += vec;
  615. posVec.z += move->z * TickSec * scale + move->roll * TickSec * scale;
  616. }
  617. else // ignore input
  618. {
  619. mObjToWorld.getColumn(3,&pos);
  620. }
  621. // apply translation vector according to physics rules
  622. mDelta.posVec = pos;
  623. if(mNewtonMode)
  624. {
  625. bool faster = move->trigger[0];
  626. bool brake = move->trigger[1];
  627. const F32 movementSpeedMultiplier = smMovementSpeed / 40.0f; // Using the starting value as the base
  628. const F32 force = faster ? mFlyForce * movementSpeedMultiplier * mSpeedMultiplier : mFlyForce * movementSpeedMultiplier;
  629. const F32 drag = brake ? mDrag * mBrakeMultiplier : mDrag;
  630. VectorF acc(0.0f, 0.0f, 0.0f);
  631. F32 posVecL = posVec.len();
  632. if(posVecL > 0)
  633. {
  634. acc = (posVec * force / mMass) * TickSec;
  635. }
  636. // Accelerate
  637. mVelocity += acc;
  638. // Drag
  639. mVelocity -= mVelocity * drag * TickSec;
  640. // Move
  641. pos += mVelocity * TickSec;
  642. }
  643. else
  644. {
  645. pos += posVec;
  646. }
  647. _setPosition(pos,mRot);
  648. // If on the client, calc delta for backstepping
  649. if (serverInterpolate || isClientObject())
  650. {
  651. mDelta.pos = pos;
  652. mDelta.rot = mRot;
  653. mDelta.posVec = mDelta.posVec - mDelta.pos;
  654. mDelta.rotVec = mDelta.rotVec - mDelta.rot;
  655. for(U32 i=0; i<3; ++i)
  656. {
  657. if (mDelta.rotVec[i] > M_PI_F)
  658. mDelta.rotVec[i] -= M_2PI_F;
  659. else if (mDelta.rotVec[i] < -M_PI_F)
  660. mDelta.rotVec[i] += M_2PI_F;
  661. }
  662. }
  663. if(mustValidateEyePoint)
  664. _validateEyePoint(1.0f, &mObjToWorld);
  665. setMaskBits(MoveMask);
  666. }
  667. // Need to calculate the orbit position for the editor so the camera position
  668. // doesn't change when switching between orbit and other camera modes
  669. if (isServerObject() && (mMode == EditOrbitMode && mValidEditOrbitPoint))
  670. _calcEditOrbitPoint(&mObjToWorld, mRot);
  671. if(getControllingClient() && mContainer)
  672. updateContainer();
  673. }
  674. //----------------------------------------------------------------------------
  675. void Camera::onDeleteNotify( SimObject* obj )
  676. {
  677. if( obj == mOrbitObject )
  678. {
  679. mOrbitObject = NULL;
  680. if( mMode == OrbitObjectMode )
  681. mMode = OrbitPointMode;
  682. }
  683. Parent::onDeleteNotify( obj );
  684. }
  685. //----------------------------------------------------------------------------
  686. void Camera::interpolateTick(F32 dt)
  687. {
  688. Parent::interpolateTick(dt);
  689. if ( isMounted() )
  690. return;
  691. Point3F rot = mDelta.rot + mDelta.rotVec * dt;
  692. if((mMode == OrbitObjectMode || mMode == OrbitPointMode) && !mNewtonMode)
  693. {
  694. if(mMode == OrbitObjectMode && bool(mOrbitObject))
  695. {
  696. // If this is a shapebase, use its render eye transform
  697. // to avoid jittering.
  698. GameBase *castObj = mOrbitObject;
  699. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  700. if( shape != NULL )
  701. {
  702. MatrixF ret;
  703. shape->getRenderEyeTransform( &ret );
  704. mPosition = ret.getPosition();
  705. }
  706. else
  707. {
  708. // Hopefully this is a static object that doesn't move,
  709. // because the worldbox doesn't get updated between ticks.
  710. mOrbitObject->getWorldBox().getCenter(&mPosition);
  711. }
  712. }
  713. _setRenderPosition( mPosition + mOffset, rot );
  714. _validateEyePoint( 1.0f, &mRenderObjToWorld );
  715. }
  716. else if(mMode == EditOrbitMode && mValidEditOrbitPoint)
  717. {
  718. mPosition = mEditOrbitPoint;
  719. _setRenderPosition(mPosition, rot);
  720. _calcEditOrbitPoint(&mRenderObjToWorld, rot);
  721. }
  722. else if(mMode == TrackObjectMode && bool(mOrbitObject) && !mNewtonRotation)
  723. {
  724. // orient the camera to face the object
  725. Point3F objPos;
  726. // If this is a shapebase, use its render eye transform
  727. // to avoid jittering.
  728. ShapeBase *shape = dynamic_cast<ShapeBase*>((GameBase*)mOrbitObject);
  729. if( shape != NULL )
  730. {
  731. MatrixF ret;
  732. shape->getRenderEyeTransform( &ret );
  733. objPos = ret.getPosition();
  734. }
  735. else
  736. {
  737. mOrbitObject->getWorldBox().getCenter(&objPos);
  738. }
  739. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  740. Point3F vec = objPos - pos;
  741. vec.normalizeSafe();
  742. F32 pitch, yaw;
  743. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  744. rot.x = -pitch;
  745. rot.z = yaw;
  746. _setRenderPosition(pos, rot);
  747. }
  748. else
  749. {
  750. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  751. _setRenderPosition(pos,rot);
  752. if(mMode == OrbitObjectMode || mMode == OrbitPointMode)
  753. _validateEyePoint(1.0f, &mRenderObjToWorld);
  754. }
  755. }
  756. //----------------------------------------------------------------------------
  757. void Camera::_setPosition(const Point3F& pos, const Point3F& rot)
  758. {
  759. MatrixF xRot, zRot;
  760. xRot.set(EulerF(rot.x, 0.0f, 0.0f));
  761. zRot.set(EulerF(0.0f, 0.0f, rot.z));
  762. MatrixF temp;
  763. if(mDataBlock && mDataBlock->cameraCanBank)
  764. {
  765. // Take rot.y into account to bank the camera
  766. MatrixF imat;
  767. imat.mul(zRot, xRot);
  768. MatrixF ymat;
  769. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  770. temp.mul(imat, ymat);
  771. }
  772. else
  773. {
  774. temp.mul(zRot, xRot);
  775. }
  776. temp.setColumn(3, pos);
  777. Parent::setTransform(temp);
  778. mRot = rot;
  779. }
  780. //----------------------------------------------------------------------------
  781. void Camera::setRotation(const Point3F& rot)
  782. {
  783. MatrixF xRot, zRot;
  784. xRot.set(EulerF(rot.x, 0.0f, 0.0f));
  785. zRot.set(EulerF(0.0f, 0.0f, rot.z));
  786. MatrixF temp;
  787. if(mDataBlock && mDataBlock->cameraCanBank)
  788. {
  789. // Take rot.y into account to bank the camera
  790. MatrixF imat;
  791. imat.mul(zRot, xRot);
  792. MatrixF ymat;
  793. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  794. temp.mul(imat, ymat);
  795. }
  796. else
  797. {
  798. temp.mul(zRot, xRot);
  799. }
  800. temp.setColumn(3, getPosition());
  801. Parent::setTransform(temp);
  802. mRot = rot;
  803. }
  804. //----------------------------------------------------------------------------
  805. void Camera::_setRenderPosition(const Point3F& pos,const Point3F& rot)
  806. {
  807. MatrixF xRot, zRot;
  808. xRot.set(EulerF(rot.x, 0, 0));
  809. zRot.set(EulerF(0, 0, rot.z));
  810. MatrixF temp;
  811. // mDataBlock may not be defined yet as this method is called during
  812. // SceneObject::onAdd().
  813. if(mDataBlock && mDataBlock->cameraCanBank)
  814. {
  815. // Take rot.y into account to bank the camera
  816. MatrixF imat;
  817. imat.mul(zRot, xRot);
  818. MatrixF ymat;
  819. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  820. temp.mul(imat, ymat);
  821. }
  822. else
  823. {
  824. temp.mul(zRot, xRot);
  825. }
  826. temp.setColumn(3, pos);
  827. Parent::setRenderTransform(temp);
  828. }
  829. //----------------------------------------------------------------------------
  830. void Camera::writePacketData(GameConnection *connection, BitStream *bstream)
  831. {
  832. // Update client regardless of status flags.
  833. Parent::writePacketData(connection, bstream);
  834. Point3F pos;
  835. mObjToWorld.getColumn(3,&pos);
  836. bstream->setCompressionPoint(pos);
  837. mathWrite(*bstream, pos);
  838. bstream->write(mRot.x);
  839. if(mDataBlock && bstream->writeFlag(mDataBlock->cameraCanBank))
  840. {
  841. // Include mRot.y to allow for camera banking
  842. bstream->write(mRot.y);
  843. }
  844. bstream->write(mRot.z);
  845. U32 writeMode = mMode;
  846. Point3F writePos = mPosition;
  847. S32 gIndex = -1;
  848. if(mMode == OrbitObjectMode)
  849. {
  850. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  851. if(gIndex == -1)
  852. {
  853. writeMode = OrbitPointMode;
  854. if(bool(mOrbitObject))
  855. mOrbitObject->getWorldBox().getCenter(&writePos);
  856. }
  857. }
  858. else if(mMode == TrackObjectMode)
  859. {
  860. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  861. if(gIndex == -1)
  862. writeMode = StationaryMode;
  863. }
  864. bstream->writeRangedU32(writeMode, CameraFirstMode, CameraLastMode);
  865. if (writeMode == OrbitObjectMode || writeMode == OrbitPointMode)
  866. {
  867. bstream->write(mMinOrbitDist);
  868. bstream->write(mMaxOrbitDist);
  869. bstream->write(mCurOrbitDist);
  870. if(writeMode == OrbitObjectMode)
  871. {
  872. bstream->writeFlag(mObservingClientObject);
  873. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize);
  874. }
  875. if (writeMode == OrbitPointMode)
  876. bstream->writeCompressedPoint(writePos);
  877. }
  878. else if(writeMode == TrackObjectMode)
  879. {
  880. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize);
  881. }
  882. if(bstream->writeFlag(mNewtonMode))
  883. {
  884. bstream->write(mVelocity.x);
  885. bstream->write(mVelocity.y);
  886. bstream->write(mVelocity.z);
  887. }
  888. if(bstream->writeFlag(mNewtonRotation))
  889. {
  890. bstream->write(mAngularVelocity.x);
  891. bstream->write(mAngularVelocity.y);
  892. bstream->write(mAngularVelocity.z);
  893. }
  894. bstream->writeFlag(mValidEditOrbitPoint);
  895. if(writeMode == EditOrbitMode)
  896. {
  897. bstream->write(mEditOrbitPoint.x);
  898. bstream->write(mEditOrbitPoint.y);
  899. bstream->write(mEditOrbitPoint.z);
  900. bstream->write(mCurrentEditOrbitDist);
  901. }
  902. }
  903. //----------------------------------------------------------------------------
  904. void Camera::readPacketData(GameConnection *connection, BitStream *bstream)
  905. {
  906. Parent::readPacketData(connection, bstream);
  907. Point3F pos,rot;
  908. mathRead(*bstream, &pos);
  909. bstream->setCompressionPoint(pos);
  910. bstream->read(&rot.x);
  911. if(bstream->readFlag())
  912. {
  913. // Include rot.y to allow for camera banking
  914. bstream->read(&rot.y);
  915. }
  916. bstream->read(&rot.z);
  917. GameBase* obj = 0;
  918. mMode = (CameraMotionMode)bstream->readRangedU32(CameraFirstMode, CameraLastMode);
  919. mObservingClientObject = false;
  920. if (mMode == OrbitObjectMode || mMode == OrbitPointMode)
  921. {
  922. bstream->read(&mMinOrbitDist);
  923. bstream->read(&mMaxOrbitDist);
  924. bstream->read(&mCurOrbitDist);
  925. if(mMode == OrbitObjectMode)
  926. {
  927. mObservingClientObject = bstream->readFlag();
  928. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  929. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  930. }
  931. if (mMode == OrbitPointMode)
  932. bstream->readCompressedPoint(&mPosition);
  933. }
  934. else if (mMode == TrackObjectMode)
  935. {
  936. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  937. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  938. }
  939. if (obj != (GameBase*)mOrbitObject)
  940. {
  941. if (mOrbitObject)
  942. {
  943. clearProcessAfter();
  944. clearNotify(mOrbitObject);
  945. }
  946. mOrbitObject = obj;
  947. if (mOrbitObject)
  948. {
  949. processAfter(mOrbitObject);
  950. deleteNotify(mOrbitObject);
  951. }
  952. }
  953. mNewtonMode = bstream->readFlag();
  954. if(mNewtonMode)
  955. {
  956. bstream->read(&mVelocity.x);
  957. bstream->read(&mVelocity.y);
  958. bstream->read(&mVelocity.z);
  959. }
  960. mNewtonRotation = bstream->readFlag();
  961. if(mNewtonRotation)
  962. {
  963. bstream->read(&mAngularVelocity.x);
  964. bstream->read(&mAngularVelocity.y);
  965. bstream->read(&mAngularVelocity.z);
  966. }
  967. mValidEditOrbitPoint = bstream->readFlag();
  968. if(mMode == EditOrbitMode)
  969. {
  970. bstream->read(&mEditOrbitPoint.x);
  971. bstream->read(&mEditOrbitPoint.y);
  972. bstream->read(&mEditOrbitPoint.z);
  973. bstream->read(&mCurrentEditOrbitDist);
  974. }
  975. _setPosition(pos,rot);
  976. // Movement in OrbitObjectMode is not input-based - don't reset interpolation
  977. if(mMode != OrbitObjectMode)
  978. {
  979. mDelta.pos = pos;
  980. mDelta.posVec.set(0.0f, 0.0f, 0.0f);
  981. mDelta.rot = rot;
  982. mDelta.rotVec.set(0.0f, 0.0f, 0.0f);
  983. }
  984. }
  985. //----------------------------------------------------------------------------
  986. U32 Camera::packUpdate(NetConnection *con, U32 mask, BitStream *bstream)
  987. {
  988. Parent::packUpdate(con, mask, bstream);
  989. if (bstream->writeFlag(mask & UpdateMask))
  990. {
  991. bstream->writeFlag(mLocked);
  992. mathWrite(*bstream, mOffset);
  993. }
  994. if(bstream->writeFlag(mask & NewtonCameraMask))
  995. {
  996. bstream->write(mAngularForce);
  997. bstream->write(mAngularDrag);
  998. bstream->write(mMass);
  999. bstream->write(mDrag);
  1000. bstream->write(mFlyForce);
  1001. bstream->write(mSpeedMultiplier);
  1002. bstream->write(mBrakeMultiplier);
  1003. }
  1004. if(bstream->writeFlag(mask & EditOrbitMask))
  1005. {
  1006. bstream->write(mEditOrbitPoint.x);
  1007. bstream->write(mEditOrbitPoint.y);
  1008. bstream->write(mEditOrbitPoint.z);
  1009. bstream->write(mCurrentEditOrbitDist);
  1010. }
  1011. // The rest of the data is part of the control object packet update.
  1012. // If we're controlled by this client, we don't need to send it.
  1013. if(bstream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  1014. return 0;
  1015. if (bstream->writeFlag(mask & MoveMask))
  1016. {
  1017. Point3F pos;
  1018. mObjToWorld.getColumn(3,&pos);
  1019. bstream->write(pos.x);
  1020. bstream->write(pos.y);
  1021. bstream->write(pos.z);
  1022. bstream->write(mRot.x);
  1023. bstream->write(mRot.z);
  1024. // Only required if in NewtonFlyMode
  1025. F32 len = mVelocity.len();
  1026. if(bstream->writeFlag(mNewtonMode && len > 0.02f))
  1027. {
  1028. Point3F outVel = mVelocity;
  1029. outVel *= 1.0f/len;
  1030. bstream->writeNormalVector(outVel, 10);
  1031. len *= 32.0f; // 5 bits of fraction
  1032. if(len > 8191)
  1033. len = 8191;
  1034. bstream->writeInt((S32)len, 13);
  1035. }
  1036. // Rotation
  1037. len = mAngularVelocity.len();
  1038. if(bstream->writeFlag(mNewtonRotation && len > 0.02f))
  1039. {
  1040. Point3F outVel = mAngularVelocity;
  1041. outVel *= 1.0f/len;
  1042. bstream->writeNormalVector(outVel, 10);
  1043. len *= 32.0f; // 5 bits of fraction
  1044. if(len > 8191)
  1045. len = 8191;
  1046. bstream->writeInt((S32)len, 13);
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. //----------------------------------------------------------------------------
  1052. void Camera::unpackUpdate(NetConnection *con, BitStream *bstream)
  1053. {
  1054. Parent::unpackUpdate(con,bstream);
  1055. if (bstream->readFlag())
  1056. {
  1057. mLocked = bstream->readFlag();
  1058. mathRead(*bstream, &mOffset);
  1059. }
  1060. // NewtonCameraMask
  1061. if(bstream->readFlag())
  1062. {
  1063. bstream->read(&mAngularForce);
  1064. bstream->read(&mAngularDrag);
  1065. bstream->read(&mMass);
  1066. bstream->read(&mDrag);
  1067. bstream->read(&mFlyForce);
  1068. bstream->read(&mSpeedMultiplier);
  1069. bstream->read(&mBrakeMultiplier);
  1070. }
  1071. // EditOrbitMask
  1072. if(bstream->readFlag())
  1073. {
  1074. bstream->read(&mEditOrbitPoint.x);
  1075. bstream->read(&mEditOrbitPoint.y);
  1076. bstream->read(&mEditOrbitPoint.z);
  1077. bstream->read(&mCurrentEditOrbitDist);
  1078. }
  1079. // controlled by the client?
  1080. if(bstream->readFlag())
  1081. return;
  1082. // MoveMask
  1083. if (bstream->readFlag())
  1084. {
  1085. Point3F pos,rot;
  1086. bstream->read(&pos.x);
  1087. bstream->read(&pos.y);
  1088. bstream->read(&pos.z);
  1089. bstream->read(&rot.x);
  1090. bstream->read(&rot.z);
  1091. rot.y = 0.0f;
  1092. _setPosition(pos,rot);
  1093. // NewtonMode
  1094. if(bstream->readFlag())
  1095. {
  1096. bstream->readNormalVector(&mVelocity, 10);
  1097. mVelocity *= bstream->readInt(13) / 32.0f;
  1098. }
  1099. // NewtonRotation
  1100. mNewtonRotation = bstream->readFlag();
  1101. if(mNewtonRotation)
  1102. {
  1103. bstream->readNormalVector(&mAngularVelocity, 10);
  1104. mAngularVelocity *= bstream->readInt(13) / 32.0f;
  1105. }
  1106. if(mMode != OrbitObjectMode)
  1107. {
  1108. // New delta for client side interpolation
  1109. mDelta.pos = pos;
  1110. mDelta.rot = rot;
  1111. mDelta.posVec = mDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  1112. }
  1113. }
  1114. }
  1115. //----------------------------------------------------------------------------
  1116. void Camera::initPersistFields()
  1117. {
  1118. addGroup( "Camera" );
  1119. addProtectedField( "controlMode", TYPEID< CameraMotionMode >(), Offset( mMode, Camera ),
  1120. &_setModeField, &defaultProtectedGetFn,
  1121. "The current camera control mode." );
  1122. endGroup( "Camera" );
  1123. addGroup( "Camera: Newton Mode" );
  1124. addField( "newtonMode", TypeBool, Offset(mNewtonMode, Camera),
  1125. "Apply smoothing (acceleration and damping) to camera movements." );
  1126. addField( "newtonRotation", TypeBool, Offset(mNewtonRotation, Camera),
  1127. "Apply smoothing (acceleration and damping) to camera rotations." );
  1128. addProtectedField( "mass", TypeF32, Offset(mMass, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1129. "The camera's mass (Newton mode only). Default value is 10." );
  1130. addProtectedField( "drag", TypeF32, Offset(mDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1131. "Drag on camera when moving (Newton mode only). Default value is 2." );
  1132. addProtectedField( "force", TypeF32, Offset(mFlyForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1133. "Force applied on camera when asked to move (Newton mode only). Default value is 500." );
  1134. addProtectedField( "angularDrag", TypeF32, Offset(mAngularDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1135. "Drag on camera when rotating (Newton mode only). Default value is 2." );
  1136. addProtectedField( "angularForce", TypeF32, Offset(mAngularForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1137. "Force applied on camera when asked to rotate (Newton mode only). Default value is 100." );
  1138. addProtectedField( "speedMultiplier", TypeF32, Offset(mSpeedMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1139. "Speed multiplier when triggering the accelerator (Newton mode only). Default value is 2." );
  1140. addProtectedField( "brakeMultiplier", TypeF32, Offset(mBrakeMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1141. "Speed multiplier when triggering the brake (Newton mode only). Default value is 2." );
  1142. endGroup( "Camera: Newton Mode" );
  1143. Parent::initPersistFields();
  1144. }
  1145. //-----------------------------------------------------------------------------
  1146. void Camera::consoleInit()
  1147. {
  1148. Con::addVariable("$Camera::movementSpeed", TypeF32, &smMovementSpeed,
  1149. "@brief Global camera movement speed in units/s (typically m/s), with a base value of 40.\n\n"
  1150. "Used in the following camera modes:\n"
  1151. "- Edit Orbit Mode\n"
  1152. "- Fly Mode\n"
  1153. "- Overhead Mode\n"
  1154. "@ingroup BaseCamera\n");
  1155. // ExtendedMove support
  1156. Con::addVariable("$camera::extendedMovePosRotIndex", TypeS32, &smExtendedMovePosRotIndex,
  1157. "@brief The ExtendedMove position/rotation index used for camera movements.\n\n"
  1158. "@ingroup BaseCamera\n");
  1159. }
  1160. //-----------------------------------------------------------------------------
  1161. bool Camera::_setNewtonField( void *object, const char *index, const char *data )
  1162. {
  1163. static_cast<Camera*>(object)->setMaskBits(NewtonCameraMask);
  1164. return true; // ok to set value
  1165. }
  1166. //-----------------------------------------------------------------------------
  1167. bool Camera::_setModeField( void *object, const char *index, const char *data )
  1168. {
  1169. Camera *cam = static_cast<Camera*>( object );
  1170. if( dStricmp(data, "Fly") == 0 )
  1171. {
  1172. cam->setFlyMode();
  1173. return false; // already changed mode
  1174. }
  1175. else if( dStricmp(data, "EditOrbit" ) == 0 )
  1176. {
  1177. cam->setEditOrbitMode();
  1178. return false; // already changed mode
  1179. }
  1180. else if( (dStricmp(data, "OrbitObject") == 0 && cam->mMode != OrbitObjectMode) ||
  1181. (dStricmp(data, "TrackObject") == 0 && cam->mMode != TrackObjectMode) ||
  1182. (dStricmp(data, "OrbitPoint") == 0 && cam->mMode != OrbitPointMode) )
  1183. {
  1184. Con::warnf("Couldn't change Camera mode to %s: required information missing. Use camera.set%s().", data, data);
  1185. return false; // don't change the mode - not valid
  1186. }
  1187. else if( dStricmp(data, "OrbitObject") != 0 &&
  1188. dStricmp(data, "TrackObject") != 0 &&
  1189. bool(cam->mOrbitObject) )
  1190. {
  1191. cam->clearProcessAfter();
  1192. cam->clearNotify(cam->mOrbitObject);
  1193. cam->mOrbitObject = NULL;
  1194. }
  1195. // make sure the requested mode is supported, and set it
  1196. // NOTE: The _CameraMode namespace is generated by ImplementEnumType above
  1197. const EngineEnumTable& enums = *( TYPE< CameraMotionMode >()->getEnumTable() );
  1198. const U32 numValues = enums.getNumValues();
  1199. for( S32 i = 0; i < numValues; ++i)
  1200. {
  1201. if( dStricmp(data, enums[i].mName) == 0 )
  1202. {
  1203. cam->mMode = (CameraMotionMode) enums[i].mInt;
  1204. return false;
  1205. }
  1206. }
  1207. Con::warnf("Unsupported camera mode: %s", data);
  1208. return false;
  1209. }
  1210. //-----------------------------------------------------------------------------
  1211. Point3F Camera::getPosition()
  1212. {
  1213. static Point3F position;
  1214. mObjToWorld.getColumn(3, &position);
  1215. return position;
  1216. }
  1217. //-----------------------------------------------------------------------------
  1218. void Camera::setFlyMode()
  1219. {
  1220. mMode = FlyMode;
  1221. if (bool(mOrbitObject))
  1222. {
  1223. clearProcessAfter();
  1224. clearNotify(mOrbitObject);
  1225. }
  1226. mOrbitObject = NULL;
  1227. }
  1228. //-----------------------------------------------------------------------------
  1229. void Camera::setNewtonFlyMode()
  1230. {
  1231. mNewtonMode = true;
  1232. setFlyMode();
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. void Camera::setOrbitMode(GameBase *obj, const Point3F &pos, const Point3F &rot, const Point3F& offset, F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked)
  1236. {
  1237. mObservingClientObject = ownClientObject;
  1238. if (bool(mOrbitObject))
  1239. {
  1240. clearProcessAfter();
  1241. clearNotify(mOrbitObject);
  1242. }
  1243. mOrbitObject = obj;
  1244. if(bool(mOrbitObject))
  1245. {
  1246. processAfter(mOrbitObject);
  1247. deleteNotify(mOrbitObject);
  1248. mOrbitObject->getWorldBox().getCenter(&mPosition);
  1249. mMode = OrbitObjectMode;
  1250. }
  1251. else
  1252. {
  1253. mMode = OrbitPointMode;
  1254. mPosition = pos;
  1255. }
  1256. _setPosition(mPosition, rot);
  1257. mMinOrbitDist = minDist;
  1258. mMaxOrbitDist = maxDist;
  1259. mCurOrbitDist = curDist;
  1260. if (locked != mLocked || mOffset != offset)
  1261. {
  1262. mLocked = locked;
  1263. mOffset = offset;
  1264. setMaskBits(UpdateMask);
  1265. }
  1266. }
  1267. //-----------------------------------------------------------------------------
  1268. void Camera::setTrackObject(GameBase *obj, const Point3F &offset)
  1269. {
  1270. if(bool(mOrbitObject))
  1271. {
  1272. clearProcessAfter();
  1273. clearNotify(mOrbitObject);
  1274. }
  1275. mOrbitObject = obj;
  1276. if(bool(mOrbitObject))
  1277. {
  1278. processAfter(mOrbitObject);
  1279. deleteNotify(mOrbitObject);
  1280. }
  1281. mOffset = offset;
  1282. mMode = TrackObjectMode;
  1283. setMaskBits( UpdateMask );
  1284. }
  1285. //-----------------------------------------------------------------------------
  1286. void Camera::_validateEyePoint(F32 pos, MatrixF *mat)
  1287. {
  1288. if (pos != 0)
  1289. {
  1290. // Use the eye transform to orient the camera
  1291. Point3F dir;
  1292. mat->getColumn(1, &dir);
  1293. if (mMaxOrbitDist - mMinOrbitDist > 0.0f)
  1294. pos *= mMaxOrbitDist - mMinOrbitDist;
  1295. // Use the camera node's pos.
  1296. Point3F startPos = getRenderPosition();
  1297. Point3F endPos;
  1298. // Make sure we don't extend the camera into anything solid
  1299. if(mOrbitObject)
  1300. mOrbitObject->disableCollision();
  1301. disableCollision();
  1302. RayInfo collision;
  1303. U32 mask = TerrainObjectType |
  1304. WaterObjectType |
  1305. StaticShapeObjectType |
  1306. PlayerObjectType |
  1307. ItemObjectType |
  1308. VehicleObjectType;
  1309. SceneContainer* pContainer = isServerObject() ? &gServerContainer : &gClientContainer;
  1310. if (!pContainer->castRay(startPos, startPos - dir * 2.5 * pos, mask, &collision))
  1311. endPos = startPos - dir * pos;
  1312. else
  1313. {
  1314. float dot = mDot(dir, collision.normal);
  1315. if (dot > 0.01f)
  1316. {
  1317. F32 colDist = mDot(startPos - collision.point, dir) - (1 / dot) * CameraRadius;
  1318. if (colDist > pos)
  1319. colDist = pos;
  1320. if (colDist < 0.0f)
  1321. colDist = 0.0f;
  1322. endPos = startPos - dir * colDist;
  1323. }
  1324. else
  1325. endPos = startPos - dir * pos;
  1326. }
  1327. mat->setColumn(3, endPos);
  1328. enableCollision();
  1329. if(mOrbitObject)
  1330. mOrbitObject->enableCollision();
  1331. }
  1332. }
  1333. //-----------------------------------------------------------------------------
  1334. void Camera::setTransform(const MatrixF& mat)
  1335. {
  1336. // This method should never be called on the client.
  1337. // This currently converts all rotation in the mat into
  1338. // rotations around the z and x axis.
  1339. Point3F pos,vec;
  1340. mat.getColumn(1, &vec);
  1341. mat.getColumn(3, &pos);
  1342. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x * vec.x + vec.y * vec.y)), 0.0f, -mAtan2(-vec.x, vec.y));
  1343. _setPosition(pos,rot);
  1344. }
  1345. //-----------------------------------------------------------------------------
  1346. void Camera::setRenderTransform(const MatrixF& mat)
  1347. {
  1348. // This method should never be called on the client.
  1349. // This currently converts all rotation in the mat into
  1350. // rotations around the z and x axis.
  1351. Point3F pos,vec;
  1352. mat.getColumn(1,&vec);
  1353. mat.getColumn(3,&pos);
  1354. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)),0,-mAtan2(-vec.x,vec.y));
  1355. _setRenderPosition(pos,rot);
  1356. }
  1357. //-----------------------------------------------------------------------------
  1358. F32 Camera::getDamageFlash() const
  1359. {
  1360. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1361. {
  1362. const GameBase *castObj = mOrbitObject;
  1363. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1364. if (psb)
  1365. return psb->getDamageFlash();
  1366. }
  1367. return mDamageFlash;
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. F32 Camera::getWhiteOut() const
  1371. {
  1372. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1373. {
  1374. const GameBase *castObj = mOrbitObject;
  1375. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1376. if (psb)
  1377. return psb->getWhiteOut();
  1378. }
  1379. return mWhiteOut;
  1380. }
  1381. //-----------------------------------------------------------------------------
  1382. void Camera::setVelocity(const VectorF& vel)
  1383. {
  1384. mVelocity = vel;
  1385. setMaskBits(MoveMask);
  1386. }
  1387. //-----------------------------------------------------------------------------
  1388. void Camera::setAngularVelocity(const VectorF& vel)
  1389. {
  1390. mAngularVelocity = vel;
  1391. setMaskBits(MoveMask);
  1392. }
  1393. //-----------------------------------------------------------------------------
  1394. void Camera::setEditOrbitMode()
  1395. {
  1396. mMode = EditOrbitMode;
  1397. if (bool(mOrbitObject))
  1398. {
  1399. clearProcessAfter();
  1400. clearNotify(mOrbitObject);
  1401. }
  1402. mOrbitObject = NULL;
  1403. // If there is a valid orbit point, then point to it
  1404. // rather than move the camera.
  1405. if(mValidEditOrbitPoint)
  1406. {
  1407. Point3F currentPos;
  1408. mObjToWorld.getColumn(3,&currentPos);
  1409. Point3F dir = mEditOrbitPoint - currentPos;
  1410. mCurrentEditOrbitDist = dir.len();
  1411. dir.normalize();
  1412. F32 yaw, pitch;
  1413. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1414. mRot.x = -pitch;
  1415. mRot.z = yaw;
  1416. }
  1417. }
  1418. //-----------------------------------------------------------------------------
  1419. void Camera::_calcOrbitPoint( MatrixF* mat, const Point3F& rot )
  1420. {
  1421. Point3F pos;
  1422. pos.x = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mCos( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.x + mOffset.x;
  1423. pos.y = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mSin( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.y + mOffset.y;
  1424. pos.z = mCurOrbitDist * mSin( rot.x ) + mPosition.z + mOffset.z;
  1425. mat->setColumn( 3, pos );
  1426. }
  1427. //-----------------------------------------------------------------------------
  1428. void Camera::_calcEditOrbitPoint( MatrixF *mat, const Point3F& rot )
  1429. {
  1430. //Point3F dir;
  1431. //mat->getColumn(1, &dir);
  1432. //Point3F startPos = getRenderPosition();
  1433. //Point3F endPos = startPos - dir * mCurrentEditOrbitDist;
  1434. Point3F pos;
  1435. pos.x = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mCos(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.x;
  1436. pos.y = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mSin(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.y;
  1437. pos.z = mCurrentEditOrbitDist * mSin(rot.x) + mEditOrbitPoint.z;
  1438. mat->setColumn(3, pos);
  1439. }
  1440. //-----------------------------------------------------------------------------
  1441. void Camera::setValidEditOrbitPoint( bool state )
  1442. {
  1443. mValidEditOrbitPoint = state;
  1444. setMaskBits(EditOrbitMask);
  1445. }
  1446. //-----------------------------------------------------------------------------
  1447. Point3F Camera::getEditOrbitPoint() const
  1448. {
  1449. return mEditOrbitPoint;
  1450. }
  1451. //-----------------------------------------------------------------------------
  1452. void Camera::setEditOrbitPoint( const Point3F& pnt )
  1453. {
  1454. // Change the point that we orbit in EditOrbitMode.
  1455. // We'll also change the facing and distance of the
  1456. // camera so that it doesn't jump around.
  1457. Point3F currentPos;
  1458. mObjToWorld.getColumn(3,&currentPos);
  1459. Point3F dir = pnt - currentPos;
  1460. mCurrentEditOrbitDist = dir.len();
  1461. if(mMode == EditOrbitMode)
  1462. {
  1463. dir.normalize();
  1464. F32 yaw, pitch;
  1465. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1466. mRot.x = -pitch;
  1467. mRot.z = yaw;
  1468. }
  1469. mEditOrbitPoint = pnt;
  1470. setMaskBits(EditOrbitMask);
  1471. }
  1472. //----------------------------------------------------------------------------
  1473. void Camera::lookAt( const Point3F& pos )
  1474. {
  1475. Point3F vec;
  1476. mObjToWorld.getColumn(3, &mPosition);
  1477. vec = pos - mPosition;
  1478. vec.normalizeSafe();
  1479. F32 pitch, yaw;
  1480. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  1481. mRot.x = -pitch;
  1482. mRot.z = yaw;
  1483. _setPosition(mPosition, mRot);
  1484. }
  1485. //----------------------------------------------------------------------------
  1486. void Camera::autoFitRadius( F32 radius )
  1487. {
  1488. F32 fov = mDegToRad( getCameraFov() );
  1489. F32 viewradius = (radius * 2.0f) / mTan(fov * 0.5f);
  1490. // Be careful of infinite sized objects. Clip to 16km
  1491. if(viewradius > 16000.0f)
  1492. viewradius = 16000.0f;
  1493. if(mMode == EditOrbitMode && mValidEditOrbitPoint)
  1494. {
  1495. mCurrentEditOrbitDist = viewradius;
  1496. }
  1497. else if(mValidEditOrbitPoint)
  1498. {
  1499. mCurrentEditOrbitDist = viewradius;
  1500. Point3F currentPos;
  1501. mObjToWorld.getColumn(3,&currentPos);
  1502. Point3F dir = mEditOrbitPoint - currentPos;
  1503. dir.normalize();
  1504. F32 yaw, pitch;
  1505. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1506. mRot.x = -pitch;
  1507. mRot.z = yaw;
  1508. mPosition = mEditOrbitPoint;
  1509. _setPosition(mPosition, mRot);
  1510. _calcEditOrbitPoint(&mObjToWorld, mRot);
  1511. }
  1512. }
  1513. //=============================================================================
  1514. // Console API.
  1515. //=============================================================================
  1516. // MARK: ---- Console API ----
  1517. //-----------------------------------------------------------------------------
  1518. DefineEngineMethod( Camera, getMode, Camera::CameraMotionMode, (), ,
  1519. "Returns the current camera control mode.\n\n"
  1520. "@see CameraMotionMode")
  1521. {
  1522. return object->getMode();
  1523. }
  1524. //-----------------------------------------------------------------------------
  1525. DefineEngineMethod( Camera, getPosition, Point3F, (), ,
  1526. "Get the camera's position in the world.\n\n"
  1527. "@returns The position in the form of \"x y z\".")
  1528. {
  1529. return object->getPosition();
  1530. }
  1531. //-----------------------------------------------------------------------------
  1532. DefineEngineMethod( Camera, getRotation, Point3F, (), ,
  1533. "Get the camera's Euler rotation in radians.\n\n"
  1534. "@returns The rotation in radians in the form of \"x y z\".")
  1535. {
  1536. return object->getRotation();
  1537. }
  1538. //-----------------------------------------------------------------------------
  1539. DefineEngineMethod( Camera, setRotation, void, ( Point3F rot ), ,
  1540. "Set the camera's Euler rotation in radians.\n\n"
  1541. "@param rot The rotation in radians in the form of \"x y z\"."
  1542. "@note Rotation around the Y axis is ignored" )
  1543. {
  1544. return object->setRotation( rot );
  1545. }
  1546. //-----------------------------------------------------------------------------
  1547. DefineEngineMethod( Camera, getOffset, Point3F, (), ,
  1548. "Get the camera's offset from its orbit or tracking point.\n\n"
  1549. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1550. "@returns The offset in the form of \"x y z\".")
  1551. {
  1552. return object->getOffset();
  1553. }
  1554. //-----------------------------------------------------------------------------
  1555. DefineEngineMethod( Camera, setOffset, void, (Point3F offset), ,
  1556. "Set the camera's offset.\n\n"
  1557. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1558. "@param offset The distance to offset the camera by in the form of \"x y z\".")
  1559. {
  1560. object->setOffset(offset);
  1561. }
  1562. //-----------------------------------------------------------------------------
  1563. DefineEngineMethod( Camera, setOrbitMode, void, (GameBase* orbitObject, TransformF orbitPoint, F32 minDistance, F32 maxDistance,
  1564. F32 initDistance, bool ownClientObj, Point3F offset, bool locked), (false, Point3F::Zero, false),
  1565. "Set the camera to orbit around the given object, or if none is given, around the given point.\n\n"
  1566. "@param orbitObject The object to orbit around. If no object is given (0 or blank string is passed in) use the orbitPoint instead\n"
  1567. "@param orbitPoint The point to orbit around when no object is given. In the form of \"x y z ax ay az aa\" such as returned by SceneObject::getTransform().\n"
  1568. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1569. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1570. "@param initDistance The initial distance from the orbit object or point.\n"
  1571. "@param ownClientObj [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1572. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1573. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1574. "@see Camera::setOrbitObject()\n"
  1575. "@see Camera::setOrbitPoint()\n")
  1576. {
  1577. MatrixF mat;
  1578. orbitPoint.mOrientation.setMatrix(&mat);
  1579. object->setOrbitMode(orbitObject, orbitPoint.mPosition, mat.toEuler(), offset,
  1580. minDistance, maxDistance, initDistance, ownClientObj, locked);
  1581. }
  1582. //-----------------------------------------------------------------------------
  1583. DefineEngineMethod( Camera, setOrbitObject, bool, (GameBase* orbitObject, VectorF rotation, F32 minDistance,
  1584. F32 maxDistance, F32 initDistance, bool ownClientObject, Point3F offset, bool locked),
  1585. (false, Point3F::Zero, false),
  1586. "Set the camera to orbit around a given object.\n\n"
  1587. "@param orbitObject The object to orbit around.\n"
  1588. "@param rotation The initial camera rotation about the object in radians in the form of \"x y z\".\n"
  1589. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1590. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1591. "@param initDistance The initial distance from the orbit object or point.\n"
  1592. "@param ownClientObject [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1593. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1594. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1595. "@returns false if the given object could not be found.\n"
  1596. "@see Camera::setOrbitMode()\n")
  1597. {
  1598. if( !orbitObject )
  1599. {
  1600. Con::errorf( "Camera::setOrbitObject - Invalid object");
  1601. return false;
  1602. }
  1603. object->setOrbitMode(orbitObject, Point3F(0.0f, 0.0f, 0.0f), rotation, offset,
  1604. minDistance, maxDistance, initDistance, ownClientObject, locked);
  1605. return true;
  1606. }
  1607. //-----------------------------------------------------------------------------
  1608. DefineEngineMethod( Camera, setOrbitPoint, void, (TransformF orbitPoint, F32 minDistance, F32 maxDistance, F32 initDistance,
  1609. Point3F offset, bool locked),
  1610. (Point3F::Zero, false),
  1611. "Set the camera to orbit around a given point.\n\n"
  1612. "@param orbitPoint The point to orbit around. In the form of \"x y z ax ay az aa\" such as returned by SceneObject::getTransform().\n"
  1613. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1614. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1615. "@param initDistance The initial distance from the orbit object or point.\n"
  1616. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1617. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1618. "@see Camera::setOrbitMode()\n")
  1619. {
  1620. MatrixF mat;
  1621. orbitPoint.mOrientation.setMatrix(&mat);
  1622. object->setOrbitMode(NULL, orbitPoint.mPosition, mat.toEuler(), offset,
  1623. minDistance, maxDistance, initDistance, false, locked);
  1624. }
  1625. //-----------------------------------------------------------------------------
  1626. DefineEngineMethod( Camera, setTrackObject, bool, (GameBase* trackObject, Point3F offset), (Point3F::Zero),
  1627. "Set the camera to track a given object.\n\n"
  1628. "@param trackObject The object to track.\n"
  1629. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1630. "@returns false if the given object could not be found.\n")
  1631. {
  1632. if(!trackObject)
  1633. {
  1634. Con::warnf("Cannot track non-existing object.");
  1635. return false;
  1636. }
  1637. object->setTrackObject(trackObject, offset);
  1638. return true;
  1639. }
  1640. //-----------------------------------------------------------------------------
  1641. DefineEngineMethod( Camera, setEditOrbitMode, void, (), ,
  1642. "Set the editor camera to orbit around a point set with Camera::setEditOrbitPoint().\n\n"
  1643. "@note This method is generally used only within the World Editor and other tools. To "
  1644. "orbit about an object or point within a game, see Camera::setOrbitMode() and its helper methods.\n")
  1645. {
  1646. object->setEditOrbitMode();
  1647. }
  1648. //-----------------------------------------------------------------------------
  1649. DefineEngineMethod( Camera, setFlyMode, void, (), ,
  1650. "Set the camera to fly freely.\n\n"
  1651. "Allows the camera to have 6 degrees of freedom. Provides for instantaneous motion "
  1652. "and rotation unless one of the Newton fields has been set to true. See Camera::newtonMode "
  1653. "and Camera::newtonRotation.\n")
  1654. {
  1655. object->setFlyMode();
  1656. }
  1657. //-----------------------------------------------------------------------------
  1658. DefineEngineMethod( Camera, setNewtonFlyMode, void, (), ,
  1659. "Set the camera to fly freely, but with ease-in and ease-out.\n\n"
  1660. "This method allows for the same 6 degrees of freedom as Camera::setFlyMode() but "
  1661. "activates the ease-in and ease-out on the camera's movement. To also activate "
  1662. "Newton mode for the camera's rotation, set Camera::newtonRotation to true.")
  1663. {
  1664. object->setNewtonFlyMode();
  1665. }
  1666. //-----------------------------------------------------------------------------
  1667. DefineEngineMethod( Camera, isRotationDamped, bool, (), ,
  1668. "Is this a Newton Fly mode camera with damped rotation?\n\n"
  1669. "@returns true if the camera uses a damped rotation. i.e. Camera::newtonRotation is set to true.\n")
  1670. {
  1671. return object->isRotationDamped();
  1672. }
  1673. //-----------------------------------------------------------------------------
  1674. DefineEngineMethod( Camera, getAngularVelocity, VectorF, (), ,
  1675. "Get the angular velocity for a Newton mode camera.\n\n"
  1676. "@returns The angular velocity in the form of \"x y z\".\n"
  1677. "@note Only returns useful results when Camera::newtonRotation is set to true.")
  1678. {
  1679. return object->getAngularVelocity();
  1680. }
  1681. //-----------------------------------------------------------------------------
  1682. DefineEngineMethod( Camera, setAngularVelocity, void, (VectorF velocity), ,
  1683. "Set the angular velocity for a Newton mode camera.\n\n"
  1684. "@param velocity The angular velocity infor form of \"x y z\".\n"
  1685. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1686. {
  1687. object->setAngularVelocity(velocity);
  1688. }
  1689. //-----------------------------------------------------------------------------
  1690. DefineEngineMethod( Camera, setAngularForce, void, (F32 force), ,
  1691. "Set the angular force for a Newton mode camera.\n\n"
  1692. "@param force The angular force applied when attempting to rotate the camera."
  1693. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1694. {
  1695. object->setAngularForce(force);
  1696. }
  1697. //-----------------------------------------------------------------------------
  1698. DefineEngineMethod( Camera, setAngularDrag, void, (F32 drag), ,
  1699. "Set the angular drag for a Newton mode camera.\n\n"
  1700. "@param drag The angular drag applied while the camera is rotating."
  1701. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1702. {
  1703. object->setAngularDrag(drag);
  1704. }
  1705. //-----------------------------------------------------------------------------
  1706. DefineEngineMethod( Camera, setMass, void, (F32 mass), ,
  1707. "Set the mass for a Newton mode camera.\n\n"
  1708. "@param mass The mass used during ease-in and ease-out calculations."
  1709. "@note Only used when Camera is in Newton mode.")
  1710. {
  1711. object->setMass(mass);
  1712. }
  1713. //-----------------------------------------------------------------------------
  1714. DefineEngineMethod( Camera, getVelocity, VectorF, (), ,
  1715. "Get the velocity for the camera.\n\n"
  1716. "@returns The camera's velocity in the form of \"x y z\"."
  1717. "@note Only useful when the Camera is in Newton mode.")
  1718. {
  1719. return object->getVelocity();
  1720. }
  1721. //-----------------------------------------------------------------------------
  1722. DefineEngineMethod( Camera, setVelocity, void, (VectorF velocity), ,
  1723. "Set the velocity for the camera.\n\n"
  1724. "@param velocity The camera's velocity in the form of \"x y z\"."
  1725. "@note Only affects the Camera when in Newton mode.")
  1726. {
  1727. object->setVelocity(velocity);
  1728. }
  1729. //-----------------------------------------------------------------------------
  1730. DefineEngineMethod( Camera, setDrag, void, (F32 drag), ,
  1731. "Set the drag for a Newton mode camera.\n\n"
  1732. "@param drag The drag applied to the camera while moving."
  1733. "@note Only used when Camera is in Newton mode.")
  1734. {
  1735. object->setDrag(drag);
  1736. }
  1737. //-----------------------------------------------------------------------------
  1738. DefineEngineMethod( Camera, setFlyForce, void, (F32 force), ,
  1739. "Set the force applied to a Newton mode camera while moving.\n\n"
  1740. "@param force The force applied to the camera while attempting to move."
  1741. "@note Only used when Camera is in Newton mode.")
  1742. {
  1743. object->setFlyForce(force);
  1744. }
  1745. //-----------------------------------------------------------------------------
  1746. DefineEngineMethod( Camera, setSpeedMultiplier, void, (F32 multiplier), ,
  1747. "Set the Newton mode camera speed multiplier when trigger[0] is active.\n\n"
  1748. "@param multiplier The speed multiplier to apply."
  1749. "@note Only used when Camera is in Newton mode.")
  1750. {
  1751. object->setSpeedMultiplier(multiplier);
  1752. }
  1753. //-----------------------------------------------------------------------------
  1754. DefineEngineMethod( Camera, setBrakeMultiplier, void, (F32 multiplier), ,
  1755. "Set the Newton mode camera brake multiplier when trigger[1] is active.\n\n"
  1756. "@param multiplier The brake multiplier to apply."
  1757. "@note Only used when Camera is in Newton mode.")
  1758. {
  1759. object->setBrakeMultiplier(multiplier);
  1760. }
  1761. //----------------------------------------------------------------------------
  1762. DefineEngineMethod( Camera, isEditOrbitMode, bool, (), ,
  1763. "Is the camera in edit orbit mode?\n\n"
  1764. "@returns true if the camera is in edit orbit mode.")
  1765. {
  1766. return object->isEditOrbitMode();
  1767. }
  1768. //----------------------------------------------------------------------------
  1769. DefineEngineMethod( Camera, setValidEditOrbitPoint, void, (bool validPoint), ,
  1770. "Set if there is a valid editor camera orbit point.\n"
  1771. "When validPoint is set to false the Camera operates as if it is "
  1772. "in Fly mode rather than an Orbit mode.\n\n"
  1773. "@param validPoint Indicates the validity of the orbit point."
  1774. "@note Only used when Camera is in Edit Orbit Mode.")
  1775. {
  1776. object->setValidEditOrbitPoint(validPoint);
  1777. }
  1778. //----------------------------------------------------------------------------
  1779. DefineEngineMethod( Camera, setEditOrbitPoint, void, (Point3F point), ,
  1780. "Set the editor camera's orbit point.\n\n"
  1781. "@param point The point the camera will orbit in the form of \"x y z\".")
  1782. {
  1783. object->setEditOrbitPoint(point);
  1784. }
  1785. //----------------------------------------------------------------------------
  1786. DefineEngineMethod( Camera, autoFitRadius, void, (F32 radius), ,
  1787. "Move the camera to fully view the given radius.\n\n"
  1788. "@note For this operation to take affect a valid edit orbit point must first be specified. See Camera::setEditOrbitPoint().\n"
  1789. "@param radius The radius to view.")
  1790. {
  1791. object->autoFitRadius(radius);
  1792. }
  1793. //----------------------------------------------------------------------------
  1794. DefineEngineMethod( Camera, lookAt, void, (Point3F point), ,
  1795. "Point the camera at the specified position. Does not work in Orbit or Track modes.\n\n"
  1796. "@param point The position to point the camera at.")
  1797. {
  1798. object->lookAt(point);
  1799. }