camera.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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. _setPosition(pos,rot);
  1092. // NewtonMode
  1093. if(bstream->readFlag())
  1094. {
  1095. bstream->readNormalVector(&mVelocity, 10);
  1096. mVelocity *= bstream->readInt(13) / 32.0f;
  1097. }
  1098. // NewtonRotation
  1099. mNewtonRotation = bstream->readFlag();
  1100. if(mNewtonRotation)
  1101. {
  1102. bstream->readNormalVector(&mAngularVelocity, 10);
  1103. mAngularVelocity *= bstream->readInt(13) / 32.0f;
  1104. }
  1105. if(mMode != OrbitObjectMode)
  1106. {
  1107. // New delta for client side interpolation
  1108. mDelta.pos = pos;
  1109. mDelta.rot = rot;
  1110. mDelta.posVec = mDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  1111. }
  1112. }
  1113. }
  1114. //----------------------------------------------------------------------------
  1115. void Camera::initPersistFields()
  1116. {
  1117. addGroup( "Camera" );
  1118. addProtectedField( "controlMode", TYPEID< CameraMotionMode >(), Offset( mMode, Camera ),
  1119. &_setModeField, &defaultProtectedGetFn,
  1120. "The current camera control mode." );
  1121. endGroup( "Camera" );
  1122. addGroup( "Camera: Newton Mode" );
  1123. addField( "newtonMode", TypeBool, Offset(mNewtonMode, Camera),
  1124. "Apply smoothing (acceleration and damping) to camera movements." );
  1125. addField( "newtonRotation", TypeBool, Offset(mNewtonRotation, Camera),
  1126. "Apply smoothing (acceleration and damping) to camera rotations." );
  1127. addProtectedField( "mass", TypeF32, Offset(mMass, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1128. "The camera's mass (Newton mode only). Default value is 10." );
  1129. addProtectedField( "drag", TypeF32, Offset(mDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1130. "Drag on camera when moving (Newton mode only). Default value is 2." );
  1131. addProtectedField( "force", TypeF32, Offset(mFlyForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1132. "Force applied on camera when asked to move (Newton mode only). Default value is 500." );
  1133. addProtectedField( "angularDrag", TypeF32, Offset(mAngularDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1134. "Drag on camera when rotating (Newton mode only). Default value is 2." );
  1135. addProtectedField( "angularForce", TypeF32, Offset(mAngularForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1136. "Force applied on camera when asked to rotate (Newton mode only). Default value is 100." );
  1137. addProtectedField( "speedMultiplier", TypeF32, Offset(mSpeedMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1138. "Speed multiplier when triggering the accelerator (Newton mode only). Default value is 2." );
  1139. addProtectedField( "brakeMultiplier", TypeF32, Offset(mBrakeMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1140. "Speed multiplier when triggering the brake (Newton mode only). Default value is 2." );
  1141. endGroup( "Camera: Newton Mode" );
  1142. Parent::initPersistFields();
  1143. }
  1144. //-----------------------------------------------------------------------------
  1145. void Camera::consoleInit()
  1146. {
  1147. Con::addVariable("$Camera::movementSpeed", TypeF32, &smMovementSpeed,
  1148. "@brief Global camera movement speed in units/s (typically m/s), with a base value of 40.\n\n"
  1149. "Used in the following camera modes:\n"
  1150. "- Edit Orbit Mode\n"
  1151. "- Fly Mode\n"
  1152. "- Overhead Mode\n"
  1153. "@ingroup BaseCamera\n");
  1154. // ExtendedMove support
  1155. Con::addVariable("$camera::extendedMovePosRotIndex", TypeS32, &smExtendedMovePosRotIndex,
  1156. "@brief The ExtendedMove position/rotation index used for camera movements.\n\n"
  1157. "@ingroup BaseCamera\n");
  1158. }
  1159. //-----------------------------------------------------------------------------
  1160. bool Camera::_setNewtonField( void *object, const char *index, const char *data )
  1161. {
  1162. static_cast<Camera*>(object)->setMaskBits(NewtonCameraMask);
  1163. return true; // ok to set value
  1164. }
  1165. //-----------------------------------------------------------------------------
  1166. bool Camera::_setModeField( void *object, const char *index, const char *data )
  1167. {
  1168. Camera *cam = static_cast<Camera*>( object );
  1169. if( dStricmp(data, "Fly") == 0 )
  1170. {
  1171. cam->setFlyMode();
  1172. return false; // already changed mode
  1173. }
  1174. else if( dStricmp(data, "EditOrbit" ) == 0 )
  1175. {
  1176. cam->setEditOrbitMode();
  1177. return false; // already changed mode
  1178. }
  1179. else if( (dStricmp(data, "OrbitObject") == 0 && cam->mMode != OrbitObjectMode) ||
  1180. (dStricmp(data, "TrackObject") == 0 && cam->mMode != TrackObjectMode) ||
  1181. (dStricmp(data, "OrbitPoint") == 0 && cam->mMode != OrbitPointMode) )
  1182. {
  1183. Con::warnf("Couldn't change Camera mode to %s: required information missing. Use camera.set%s().", data, data);
  1184. return false; // don't change the mode - not valid
  1185. }
  1186. else if( dStricmp(data, "OrbitObject") != 0 &&
  1187. dStricmp(data, "TrackObject") != 0 &&
  1188. bool(cam->mOrbitObject) )
  1189. {
  1190. cam->clearProcessAfter();
  1191. cam->clearNotify(cam->mOrbitObject);
  1192. cam->mOrbitObject = NULL;
  1193. }
  1194. // make sure the requested mode is supported, and set it
  1195. // NOTE: The _CameraMode namespace is generated by ImplementEnumType above
  1196. const EngineEnumTable& enums = *( TYPE< CameraMotionMode >()->getEnumTable() );
  1197. const U32 numValues = enums.getNumValues();
  1198. for( S32 i = 0; i < numValues; ++i)
  1199. {
  1200. if( dStricmp(data, enums[i].mName) == 0 )
  1201. {
  1202. cam->mMode = (CameraMotionMode) enums[i].mInt;
  1203. return false;
  1204. }
  1205. }
  1206. Con::warnf("Unsupported camera mode: %s", data);
  1207. return false;
  1208. }
  1209. //-----------------------------------------------------------------------------
  1210. Point3F Camera::getPosition()
  1211. {
  1212. static Point3F position;
  1213. mObjToWorld.getColumn(3, &position);
  1214. return position;
  1215. }
  1216. //-----------------------------------------------------------------------------
  1217. void Camera::setFlyMode()
  1218. {
  1219. mMode = FlyMode;
  1220. if (bool(mOrbitObject))
  1221. {
  1222. clearProcessAfter();
  1223. clearNotify(mOrbitObject);
  1224. }
  1225. mOrbitObject = NULL;
  1226. }
  1227. //-----------------------------------------------------------------------------
  1228. void Camera::setNewtonFlyMode()
  1229. {
  1230. mNewtonMode = true;
  1231. setFlyMode();
  1232. }
  1233. //-----------------------------------------------------------------------------
  1234. void Camera::setOrbitMode(GameBase *obj, const Point3F &pos, const Point3F &rot, const Point3F& offset, F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked)
  1235. {
  1236. mObservingClientObject = ownClientObject;
  1237. if (bool(mOrbitObject))
  1238. {
  1239. clearProcessAfter();
  1240. clearNotify(mOrbitObject);
  1241. }
  1242. mOrbitObject = obj;
  1243. if(bool(mOrbitObject))
  1244. {
  1245. processAfter(mOrbitObject);
  1246. deleteNotify(mOrbitObject);
  1247. mOrbitObject->getWorldBox().getCenter(&mPosition);
  1248. mMode = OrbitObjectMode;
  1249. }
  1250. else
  1251. {
  1252. mMode = OrbitPointMode;
  1253. mPosition = pos;
  1254. }
  1255. _setPosition(mPosition, rot);
  1256. mMinOrbitDist = minDist;
  1257. mMaxOrbitDist = maxDist;
  1258. mCurOrbitDist = curDist;
  1259. if (locked != mLocked || mOffset != offset)
  1260. {
  1261. mLocked = locked;
  1262. mOffset = offset;
  1263. setMaskBits(UpdateMask);
  1264. }
  1265. }
  1266. //-----------------------------------------------------------------------------
  1267. void Camera::setTrackObject(GameBase *obj, const Point3F &offset)
  1268. {
  1269. if(bool(mOrbitObject))
  1270. {
  1271. clearProcessAfter();
  1272. clearNotify(mOrbitObject);
  1273. }
  1274. mOrbitObject = obj;
  1275. if(bool(mOrbitObject))
  1276. {
  1277. processAfter(mOrbitObject);
  1278. deleteNotify(mOrbitObject);
  1279. }
  1280. mOffset = offset;
  1281. mMode = TrackObjectMode;
  1282. setMaskBits( UpdateMask );
  1283. }
  1284. //-----------------------------------------------------------------------------
  1285. void Camera::_validateEyePoint(F32 pos, MatrixF *mat)
  1286. {
  1287. if (pos != 0)
  1288. {
  1289. // Use the eye transform to orient the camera
  1290. Point3F dir;
  1291. mat->getColumn(1, &dir);
  1292. if (mMaxOrbitDist - mMinOrbitDist > 0.0f)
  1293. pos *= mMaxOrbitDist - mMinOrbitDist;
  1294. // Use the camera node's pos.
  1295. Point3F startPos = getRenderPosition();
  1296. Point3F endPos;
  1297. // Make sure we don't extend the camera into anything solid
  1298. if(mOrbitObject)
  1299. mOrbitObject->disableCollision();
  1300. disableCollision();
  1301. RayInfo collision;
  1302. U32 mask = TerrainObjectType |
  1303. WaterObjectType |
  1304. StaticShapeObjectType |
  1305. PlayerObjectType |
  1306. ItemObjectType |
  1307. VehicleObjectType;
  1308. SceneContainer* pContainer = isServerObject() ? &gServerContainer : &gClientContainer;
  1309. if (!pContainer->castRay(startPos, startPos - dir * 2.5 * pos, mask, &collision))
  1310. endPos = startPos - dir * pos;
  1311. else
  1312. {
  1313. float dot = mDot(dir, collision.normal);
  1314. if (dot > 0.01f)
  1315. {
  1316. F32 colDist = mDot(startPos - collision.point, dir) - (1 / dot) * CameraRadius;
  1317. if (colDist > pos)
  1318. colDist = pos;
  1319. if (colDist < 0.0f)
  1320. colDist = 0.0f;
  1321. endPos = startPos - dir * colDist;
  1322. }
  1323. else
  1324. endPos = startPos - dir * pos;
  1325. }
  1326. mat->setColumn(3, endPos);
  1327. enableCollision();
  1328. if(mOrbitObject)
  1329. mOrbitObject->enableCollision();
  1330. }
  1331. }
  1332. //-----------------------------------------------------------------------------
  1333. void Camera::setTransform(const MatrixF& mat)
  1334. {
  1335. // This method should never be called on the client.
  1336. // This currently converts all rotation in the mat into
  1337. // rotations around the z and x axis.
  1338. Point3F pos,vec;
  1339. mat.getColumn(1, &vec);
  1340. mat.getColumn(3, &pos);
  1341. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x * vec.x + vec.y * vec.y)), 0.0f, -mAtan2(-vec.x, vec.y));
  1342. _setPosition(pos,rot);
  1343. }
  1344. //-----------------------------------------------------------------------------
  1345. void Camera::setRenderTransform(const MatrixF& mat)
  1346. {
  1347. // This method should never be called on the client.
  1348. // This currently converts all rotation in the mat into
  1349. // rotations around the z and x axis.
  1350. Point3F pos,vec;
  1351. mat.getColumn(1,&vec);
  1352. mat.getColumn(3,&pos);
  1353. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)),0,-mAtan2(-vec.x,vec.y));
  1354. _setRenderPosition(pos,rot);
  1355. }
  1356. //-----------------------------------------------------------------------------
  1357. F32 Camera::getDamageFlash() const
  1358. {
  1359. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1360. {
  1361. const GameBase *castObj = mOrbitObject;
  1362. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1363. if (psb)
  1364. return psb->getDamageFlash();
  1365. }
  1366. return mDamageFlash;
  1367. }
  1368. //-----------------------------------------------------------------------------
  1369. F32 Camera::getWhiteOut() const
  1370. {
  1371. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1372. {
  1373. const GameBase *castObj = mOrbitObject;
  1374. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1375. if (psb)
  1376. return psb->getWhiteOut();
  1377. }
  1378. return mWhiteOut;
  1379. }
  1380. //-----------------------------------------------------------------------------
  1381. void Camera::setVelocity(const VectorF& vel)
  1382. {
  1383. mVelocity = vel;
  1384. setMaskBits(MoveMask);
  1385. }
  1386. //-----------------------------------------------------------------------------
  1387. void Camera::setAngularVelocity(const VectorF& vel)
  1388. {
  1389. mAngularVelocity = vel;
  1390. setMaskBits(MoveMask);
  1391. }
  1392. //-----------------------------------------------------------------------------
  1393. void Camera::setEditOrbitMode()
  1394. {
  1395. mMode = EditOrbitMode;
  1396. if (bool(mOrbitObject))
  1397. {
  1398. clearProcessAfter();
  1399. clearNotify(mOrbitObject);
  1400. }
  1401. mOrbitObject = NULL;
  1402. // If there is a valid orbit point, then point to it
  1403. // rather than move the camera.
  1404. if(mValidEditOrbitPoint)
  1405. {
  1406. Point3F currentPos;
  1407. mObjToWorld.getColumn(3,&currentPos);
  1408. Point3F dir = mEditOrbitPoint - currentPos;
  1409. mCurrentEditOrbitDist = dir.len();
  1410. dir.normalize();
  1411. F32 yaw, pitch;
  1412. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1413. mRot.x = -pitch;
  1414. mRot.z = yaw;
  1415. }
  1416. }
  1417. //-----------------------------------------------------------------------------
  1418. void Camera::_calcOrbitPoint( MatrixF* mat, const Point3F& rot )
  1419. {
  1420. Point3F pos;
  1421. pos.x = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mCos( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.x + mOffset.x;
  1422. pos.y = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mSin( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.y + mOffset.y;
  1423. pos.z = mCurOrbitDist * mSin( rot.x ) + mPosition.z + mOffset.z;
  1424. mat->setColumn( 3, pos );
  1425. }
  1426. //-----------------------------------------------------------------------------
  1427. void Camera::_calcEditOrbitPoint( MatrixF *mat, const Point3F& rot )
  1428. {
  1429. //Point3F dir;
  1430. //mat->getColumn(1, &dir);
  1431. //Point3F startPos = getRenderPosition();
  1432. //Point3F endPos = startPos - dir * mCurrentEditOrbitDist;
  1433. Point3F pos;
  1434. pos.x = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mCos(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.x;
  1435. pos.y = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mSin(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.y;
  1436. pos.z = mCurrentEditOrbitDist * mSin(rot.x) + mEditOrbitPoint.z;
  1437. mat->setColumn(3, pos);
  1438. }
  1439. //-----------------------------------------------------------------------------
  1440. void Camera::setValidEditOrbitPoint( bool state )
  1441. {
  1442. mValidEditOrbitPoint = state;
  1443. setMaskBits(EditOrbitMask);
  1444. }
  1445. //-----------------------------------------------------------------------------
  1446. Point3F Camera::getEditOrbitPoint() const
  1447. {
  1448. return mEditOrbitPoint;
  1449. }
  1450. //-----------------------------------------------------------------------------
  1451. void Camera::setEditOrbitPoint( const Point3F& pnt )
  1452. {
  1453. // Change the point that we orbit in EditOrbitMode.
  1454. // We'll also change the facing and distance of the
  1455. // camera so that it doesn't jump around.
  1456. Point3F currentPos;
  1457. mObjToWorld.getColumn(3,&currentPos);
  1458. Point3F dir = pnt - currentPos;
  1459. mCurrentEditOrbitDist = dir.len();
  1460. if(mMode == EditOrbitMode)
  1461. {
  1462. dir.normalize();
  1463. F32 yaw, pitch;
  1464. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1465. mRot.x = -pitch;
  1466. mRot.z = yaw;
  1467. }
  1468. mEditOrbitPoint = pnt;
  1469. setMaskBits(EditOrbitMask);
  1470. }
  1471. //----------------------------------------------------------------------------
  1472. void Camera::lookAt( const Point3F& pos )
  1473. {
  1474. Point3F vec;
  1475. mObjToWorld.getColumn(3, &mPosition);
  1476. vec = pos - mPosition;
  1477. vec.normalizeSafe();
  1478. F32 pitch, yaw;
  1479. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  1480. mRot.x = -pitch;
  1481. mRot.z = yaw;
  1482. _setPosition(mPosition, mRot);
  1483. }
  1484. //----------------------------------------------------------------------------
  1485. void Camera::autoFitRadius( F32 radius )
  1486. {
  1487. F32 fov = mDegToRad( getCameraFov() );
  1488. F32 viewradius = (radius * 2.0f) / mTan(fov * 0.5f);
  1489. // Be careful of infinite sized objects. Clip to 16km
  1490. if(viewradius > 16000.0f)
  1491. viewradius = 16000.0f;
  1492. if(mMode == EditOrbitMode && mValidEditOrbitPoint)
  1493. {
  1494. mCurrentEditOrbitDist = viewradius;
  1495. }
  1496. else if(mValidEditOrbitPoint)
  1497. {
  1498. mCurrentEditOrbitDist = viewradius;
  1499. Point3F currentPos;
  1500. mObjToWorld.getColumn(3,&currentPos);
  1501. Point3F dir = mEditOrbitPoint - currentPos;
  1502. dir.normalize();
  1503. F32 yaw, pitch;
  1504. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1505. mRot.x = -pitch;
  1506. mRot.z = yaw;
  1507. mPosition = mEditOrbitPoint;
  1508. _setPosition(mPosition, mRot);
  1509. _calcEditOrbitPoint(&mObjToWorld, mRot);
  1510. }
  1511. }
  1512. //=============================================================================
  1513. // Console API.
  1514. //=============================================================================
  1515. // MARK: ---- Console API ----
  1516. //-----------------------------------------------------------------------------
  1517. DefineEngineMethod( Camera, getMode, Camera::CameraMotionMode, (), ,
  1518. "Returns the current camera control mode.\n\n"
  1519. "@see CameraMotionMode")
  1520. {
  1521. return object->getMode();
  1522. }
  1523. //-----------------------------------------------------------------------------
  1524. DefineEngineMethod( Camera, getPosition, Point3F, (), ,
  1525. "Get the camera's position in the world.\n\n"
  1526. "@returns The position in the form of \"x y z\".")
  1527. {
  1528. return object->getPosition();
  1529. }
  1530. //-----------------------------------------------------------------------------
  1531. DefineEngineMethod( Camera, getRotation, Point3F, (), ,
  1532. "Get the camera's Euler rotation in radians.\n\n"
  1533. "@returns The rotation in radians in the form of \"x y z\".")
  1534. {
  1535. return object->getRotation();
  1536. }
  1537. //-----------------------------------------------------------------------------
  1538. DefineEngineMethod( Camera, setRotation, void, ( Point3F rot ), ,
  1539. "Set the camera's Euler rotation in radians.\n\n"
  1540. "@param rot The rotation in radians in the form of \"x y z\"."
  1541. "@note Rotation around the Y axis is ignored" )
  1542. {
  1543. return object->setRotation( rot );
  1544. }
  1545. //-----------------------------------------------------------------------------
  1546. DefineEngineMethod( Camera, getOffset, Point3F, (), ,
  1547. "Get the camera's offset from its orbit or tracking point.\n\n"
  1548. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1549. "@returns The offset in the form of \"x y z\".")
  1550. {
  1551. return object->getOffset();
  1552. }
  1553. //-----------------------------------------------------------------------------
  1554. DefineEngineMethod( Camera, setOffset, void, (Point3F offset), ,
  1555. "Set the camera's offset.\n\n"
  1556. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1557. "@param offset The distance to offset the camera by in the form of \"x y z\".")
  1558. {
  1559. object->setOffset(offset);
  1560. }
  1561. //-----------------------------------------------------------------------------
  1562. DefineEngineMethod( Camera, setOrbitMode, void, (GameBase* orbitObject, TransformF orbitPoint, F32 minDistance, F32 maxDistance,
  1563. F32 initDistance, bool ownClientObj, Point3F offset, bool locked), (false, Point3F::Zero, false),
  1564. "Set the camera to orbit around the given object, or if none is given, around the given point.\n\n"
  1565. "@param orbitObject The object to orbit around. If no object is given (0 or blank string is passed in) use the orbitPoint instead\n"
  1566. "@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"
  1567. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1568. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1569. "@param initDistance The initial distance from the orbit object or point.\n"
  1570. "@param ownClientObj [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1571. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1572. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1573. "@see Camera::setOrbitObject()\n"
  1574. "@see Camera::setOrbitPoint()\n")
  1575. {
  1576. MatrixF mat;
  1577. orbitPoint.mOrientation.setMatrix(&mat);
  1578. object->setOrbitMode(orbitObject, orbitPoint.mPosition, mat.toEuler(), offset,
  1579. minDistance, maxDistance, initDistance, ownClientObj, locked);
  1580. }
  1581. //-----------------------------------------------------------------------------
  1582. DefineEngineMethod( Camera, setOrbitObject, bool, (GameBase* orbitObject, VectorF rotation, F32 minDistance,
  1583. F32 maxDistance, F32 initDistance, bool ownClientObject, Point3F offset, bool locked),
  1584. (false, Point3F::Zero, false),
  1585. "Set the camera to orbit around a given object.\n\n"
  1586. "@param orbitObject The object to orbit around.\n"
  1587. "@param rotation The initial camera rotation about the object in radians in the form of \"x y z\".\n"
  1588. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1589. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1590. "@param initDistance The initial distance from the orbit object or point.\n"
  1591. "@param ownClientObject [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1592. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1593. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1594. "@returns false if the given object could not be found.\n"
  1595. "@see Camera::setOrbitMode()\n")
  1596. {
  1597. if( !orbitObject )
  1598. {
  1599. Con::errorf( "Camera::setOrbitObject - Invalid object");
  1600. return false;
  1601. }
  1602. object->setOrbitMode(orbitObject, Point3F(0.0f, 0.0f, 0.0f), rotation, offset,
  1603. minDistance, maxDistance, initDistance, ownClientObject, locked);
  1604. return true;
  1605. }
  1606. //-----------------------------------------------------------------------------
  1607. DefineEngineMethod( Camera, setOrbitPoint, void, (TransformF orbitPoint, F32 minDistance, F32 maxDistance, F32 initDistance,
  1608. Point3F offset, bool locked),
  1609. (Point3F::Zero, false),
  1610. "Set the camera to orbit around a given point.\n\n"
  1611. "@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"
  1612. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1613. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1614. "@param initDistance The initial distance from the orbit object or point.\n"
  1615. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1616. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1617. "@see Camera::setOrbitMode()\n")
  1618. {
  1619. MatrixF mat;
  1620. orbitPoint.mOrientation.setMatrix(&mat);
  1621. object->setOrbitMode(NULL, orbitPoint.mPosition, mat.toEuler(), offset,
  1622. minDistance, maxDistance, initDistance, false, locked);
  1623. }
  1624. //-----------------------------------------------------------------------------
  1625. DefineEngineMethod( Camera, setTrackObject, bool, (GameBase* trackObject, Point3F offset), (Point3F::Zero),
  1626. "Set the camera to track a given object.\n\n"
  1627. "@param trackObject The object to track.\n"
  1628. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1629. "@returns false if the given object could not be found.\n")
  1630. {
  1631. if(!trackObject)
  1632. {
  1633. Con::warnf("Cannot track non-existing object.");
  1634. return false;
  1635. }
  1636. object->setTrackObject(trackObject, offset);
  1637. return true;
  1638. }
  1639. //-----------------------------------------------------------------------------
  1640. DefineEngineMethod( Camera, setEditOrbitMode, void, (), ,
  1641. "Set the editor camera to orbit around a point set with Camera::setEditOrbitPoint().\n\n"
  1642. "@note This method is generally used only within the World Editor and other tools. To "
  1643. "orbit about an object or point within a game, see Camera::setOrbitMode() and its helper methods.\n")
  1644. {
  1645. object->setEditOrbitMode();
  1646. }
  1647. //-----------------------------------------------------------------------------
  1648. DefineEngineMethod( Camera, setFlyMode, void, (), ,
  1649. "Set the camera to fly freely.\n\n"
  1650. "Allows the camera to have 6 degrees of freedom. Provides for instantaneous motion "
  1651. "and rotation unless one of the Newton fields has been set to true. See Camera::newtonMode "
  1652. "and Camera::newtonRotation.\n")
  1653. {
  1654. object->setFlyMode();
  1655. }
  1656. //-----------------------------------------------------------------------------
  1657. DefineEngineMethod( Camera, setNewtonFlyMode, void, (), ,
  1658. "Set the camera to fly freely, but with ease-in and ease-out.\n\n"
  1659. "This method allows for the same 6 degrees of freedom as Camera::setFlyMode() but "
  1660. "activates the ease-in and ease-out on the camera's movement. To also activate "
  1661. "Newton mode for the camera's rotation, set Camera::newtonRotation to true.")
  1662. {
  1663. object->setNewtonFlyMode();
  1664. }
  1665. //-----------------------------------------------------------------------------
  1666. DefineEngineMethod( Camera, isRotationDamped, bool, (), ,
  1667. "Is this a Newton Fly mode camera with damped rotation?\n\n"
  1668. "@returns true if the camera uses a damped rotation. i.e. Camera::newtonRotation is set to true.\n")
  1669. {
  1670. return object->isRotationDamped();
  1671. }
  1672. //-----------------------------------------------------------------------------
  1673. DefineEngineMethod( Camera, getAngularVelocity, VectorF, (), ,
  1674. "Get the angular velocity for a Newton mode camera.\n\n"
  1675. "@returns The angular velocity in the form of \"x y z\".\n"
  1676. "@note Only returns useful results when Camera::newtonRotation is set to true.")
  1677. {
  1678. return object->getAngularVelocity();
  1679. }
  1680. //-----------------------------------------------------------------------------
  1681. DefineEngineMethod( Camera, setAngularVelocity, void, (VectorF velocity), ,
  1682. "Set the angular velocity for a Newton mode camera.\n\n"
  1683. "@param velocity The angular velocity infor form of \"x y z\".\n"
  1684. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1685. {
  1686. object->setAngularVelocity(velocity);
  1687. }
  1688. //-----------------------------------------------------------------------------
  1689. DefineEngineMethod( Camera, setAngularForce, void, (F32 force), ,
  1690. "Set the angular force for a Newton mode camera.\n\n"
  1691. "@param force The angular force applied when attempting to rotate the camera."
  1692. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1693. {
  1694. object->setAngularForce(force);
  1695. }
  1696. //-----------------------------------------------------------------------------
  1697. DefineEngineMethod( Camera, setAngularDrag, void, (F32 drag), ,
  1698. "Set the angular drag for a Newton mode camera.\n\n"
  1699. "@param drag The angular drag applied while the camera is rotating."
  1700. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1701. {
  1702. object->setAngularDrag(drag);
  1703. }
  1704. //-----------------------------------------------------------------------------
  1705. DefineEngineMethod( Camera, setMass, void, (F32 mass), ,
  1706. "Set the mass for a Newton mode camera.\n\n"
  1707. "@param mass The mass used during ease-in and ease-out calculations."
  1708. "@note Only used when Camera is in Newton mode.")
  1709. {
  1710. object->setMass(mass);
  1711. }
  1712. //-----------------------------------------------------------------------------
  1713. DefineEngineMethod( Camera, getVelocity, VectorF, (), ,
  1714. "Get the velocity for the camera.\n\n"
  1715. "@returns The camera's velocity in the form of \"x y z\"."
  1716. "@note Only useful when the Camera is in Newton mode.")
  1717. {
  1718. return object->getVelocity();
  1719. }
  1720. //-----------------------------------------------------------------------------
  1721. DefineEngineMethod( Camera, setVelocity, void, (VectorF velocity), ,
  1722. "Set the velocity for the camera.\n\n"
  1723. "@param velocity The camera's velocity in the form of \"x y z\"."
  1724. "@note Only affects the Camera when in Newton mode.")
  1725. {
  1726. object->setVelocity(velocity);
  1727. }
  1728. //-----------------------------------------------------------------------------
  1729. DefineEngineMethod( Camera, setDrag, void, (F32 drag), ,
  1730. "Set the drag for a Newton mode camera.\n\n"
  1731. "@param drag The drag applied to the camera while moving."
  1732. "@note Only used when Camera is in Newton mode.")
  1733. {
  1734. object->setDrag(drag);
  1735. }
  1736. //-----------------------------------------------------------------------------
  1737. DefineEngineMethod( Camera, setFlyForce, void, (F32 force), ,
  1738. "Set the force applied to a Newton mode camera while moving.\n\n"
  1739. "@param force The force applied to the camera while attempting to move."
  1740. "@note Only used when Camera is in Newton mode.")
  1741. {
  1742. object->setFlyForce(force);
  1743. }
  1744. //-----------------------------------------------------------------------------
  1745. DefineEngineMethod( Camera, setSpeedMultiplier, void, (F32 multiplier), ,
  1746. "Set the Newton mode camera speed multiplier when trigger[0] is active.\n\n"
  1747. "@param multiplier The speed multiplier to apply."
  1748. "@note Only used when Camera is in Newton mode.")
  1749. {
  1750. object->setSpeedMultiplier(multiplier);
  1751. }
  1752. //-----------------------------------------------------------------------------
  1753. DefineEngineMethod( Camera, setBrakeMultiplier, void, (F32 multiplier), ,
  1754. "Set the Newton mode camera brake multiplier when trigger[1] is active.\n\n"
  1755. "@param multiplier The brake multiplier to apply."
  1756. "@note Only used when Camera is in Newton mode.")
  1757. {
  1758. object->setBrakeMultiplier(multiplier);
  1759. }
  1760. //----------------------------------------------------------------------------
  1761. DefineEngineMethod( Camera, isEditOrbitMode, bool, (), ,
  1762. "Is the camera in edit orbit mode?\n\n"
  1763. "@returns true if the camera is in edit orbit mode.")
  1764. {
  1765. return object->isEditOrbitMode();
  1766. }
  1767. //----------------------------------------------------------------------------
  1768. DefineEngineMethod( Camera, setValidEditOrbitPoint, void, (bool validPoint), ,
  1769. "Set if there is a valid editor camera orbit point.\n"
  1770. "When validPoint is set to false the Camera operates as if it is "
  1771. "in Fly mode rather than an Orbit mode.\n\n"
  1772. "@param validPoint Indicates the validity of the orbit point."
  1773. "@note Only used when Camera is in Edit Orbit Mode.")
  1774. {
  1775. object->setValidEditOrbitPoint(validPoint);
  1776. }
  1777. //----------------------------------------------------------------------------
  1778. DefineEngineMethod( Camera, setEditOrbitPoint, void, (Point3F point), ,
  1779. "Set the editor camera's orbit point.\n\n"
  1780. "@param point The point the camera will orbit in the form of \"x y z\".")
  1781. {
  1782. object->setEditOrbitPoint(point);
  1783. }
  1784. //----------------------------------------------------------------------------
  1785. DefineEngineMethod( Camera, autoFitRadius, void, (F32 radius), ,
  1786. "Move the camera to fully view the given radius.\n\n"
  1787. "@note For this operation to take affect a valid edit orbit point must first be specified. See Camera::setEditOrbitPoint().\n"
  1788. "@param radius The radius to view.")
  1789. {
  1790. object->autoFitRadius(radius);
  1791. }
  1792. //----------------------------------------------------------------------------
  1793. DefineEngineMethod( Camera, lookAt, void, (Point3F point), ,
  1794. "Point the camera at the specified position. Does not work in Orbit or Track modes.\n\n"
  1795. "@param point The position to point the camera at.")
  1796. {
  1797. object->lookAt(point);
  1798. }