2
0

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. // For NewtonFlyMode
  240. mNewtonRotation = false;
  241. mAngularVelocity.set(0.0f, 0.0f, 0.0f);
  242. mAngularForce = 100.0f;
  243. mAngularDrag = 2.0f;
  244. mVelocity.set(0.0f, 0.0f, 0.0f);
  245. mNewtonMode = false;
  246. mMass = 10.0f;
  247. mDrag = 2.0;
  248. mFlyForce = 500.0f;
  249. mSpeedMultiplier = 2.0f;
  250. mBrakeMultiplier = 2.0f;
  251. // For EditOrbitMode
  252. mValidEditOrbitPoint = false;
  253. mEditOrbitPoint.set(0.0f, 0.0f, 0.0f);
  254. mCurrentEditOrbitDist = 2.0;
  255. mLocked = false;
  256. }
  257. //----------------------------------------------------------------------------
  258. Camera::~Camera()
  259. {
  260. }
  261. //----------------------------------------------------------------------------
  262. bool Camera::onAdd()
  263. {
  264. if(!Parent::onAdd() || !mDataBlock)
  265. return false;
  266. mObjBox.maxExtents = mObjScale;
  267. mObjBox.minExtents = mObjScale;
  268. mObjBox.minExtents.neg();
  269. resetWorldBox();
  270. addToScene();
  271. scriptOnAdd();
  272. return true;
  273. }
  274. //----------------------------------------------------------------------------
  275. void Camera::onRemove()
  276. {
  277. scriptOnRemove();
  278. removeFromScene();
  279. Parent::onRemove();
  280. }
  281. //----------------------------------------------------------------------------
  282. bool Camera::onNewDataBlock( GameBaseData *dptr, bool reload )
  283. {
  284. mDataBlock = dynamic_cast<CameraData*>(dptr);
  285. if ( !mDataBlock || !Parent::onNewDataBlock( dptr, reload ) )
  286. return false;
  287. scriptOnNewDataBlock();
  288. return true;
  289. }
  290. //----------------------------------------------------------------------------
  291. void Camera::onEditorEnable()
  292. {
  293. mNetFlags.set(Ghostable);
  294. }
  295. //----------------------------------------------------------------------------
  296. void Camera::onEditorDisable()
  297. {
  298. mNetFlags.clear(Ghostable);
  299. }
  300. //----------------------------------------------------------------------------
  301. // check if the object needs to be observed through its own camera...
  302. void Camera::getCameraTransform(F32* pos, MatrixF* mat)
  303. {
  304. // The camera doesn't support a third person mode,
  305. // so we want to override the default ShapeBase behavior.
  306. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  307. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  308. obj->getCameraTransform(pos, mat);
  309. else
  310. getRenderEyeTransform(mat);
  311. // Apply Camera FX.
  312. mat->mul( gCamFXMgr.getTrans() );
  313. }
  314. //----------------------------------------------------------------------------
  315. F32 Camera::getCameraFov()
  316. {
  317. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  318. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  319. return(obj->getCameraFov());
  320. else
  321. return(Parent::getCameraFov());
  322. }
  323. //----------------------------------------------------------------------------
  324. F32 Camera::getDefaultCameraFov()
  325. {
  326. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  327. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  328. return(obj->getDefaultCameraFov());
  329. else
  330. return(Parent::getDefaultCameraFov());
  331. }
  332. //----------------------------------------------------------------------------
  333. bool Camera::isValidCameraFov(F32 fov)
  334. {
  335. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  336. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  337. return(obj->isValidCameraFov(fov));
  338. else
  339. return(Parent::isValidCameraFov(fov));
  340. }
  341. //----------------------------------------------------------------------------
  342. void Camera::setCameraFov(F32 fov)
  343. {
  344. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  345. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  346. obj->setCameraFov(fov);
  347. else
  348. Parent::setCameraFov(fov);
  349. }
  350. //----------------------------------------------------------------------------
  351. void clampPitchAngle(F32 &pitch)
  352. {
  353. // Clamp pitch to +/-MaxPitch, but allow pitch=PI as it is used by some editor
  354. // views (bottom, front, right)
  355. if ((pitch > MaxPitch) && !mIsEqual(pitch, M_PI_F, 0.001f))
  356. pitch = MaxPitch;
  357. else if (pitch < -MaxPitch)
  358. pitch = -MaxPitch;
  359. }
  360. //----------------------------------------------------------------------------
  361. void Camera::processTick(const Move* move)
  362. {
  363. Parent::processTick(move);
  364. if ( isMounted() )
  365. {
  366. // Fetch Mount Transform.
  367. MatrixF mat;
  368. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  369. // Apply.
  370. setTransform( mat );
  371. // Update SceneContainer.
  372. updateContainer();
  373. return;
  374. }
  375. Point3F vec,pos;
  376. if (move)
  377. {
  378. bool strafeMode = move->trigger[2];
  379. // If using editor then force camera into fly mode, unless using EditOrbitMode
  380. if(gEditingMission && mMode != FlyMode && mMode != EditOrbitMode)
  381. setFlyMode();
  382. // Massage the mode if we're in EditOrbitMode
  383. CameraMotionMode virtualMode = mMode;
  384. if(mMode == EditOrbitMode)
  385. {
  386. if(!mValidEditOrbitPoint)
  387. {
  388. virtualMode = FlyMode;
  389. }
  390. else
  391. {
  392. // Reset any Newton camera velocities for when we switch
  393. // out of EditOrbitMode.
  394. mNewtonRotation = false;
  395. mVelocity.set(0.0f, 0.0f, 0.0f);
  396. mAngularVelocity.set(0.0f, 0.0f, 0.0f);
  397. }
  398. }
  399. // Update orientation
  400. mDelta.rotVec = mRot;
  401. VectorF rotVec(0, 0, 0);
  402. bool doStandardMove = true;
  403. #ifdef TORQUE_EXTENDED_MOVE
  404. GameConnection* con = getControllingClient();
  405. // Work with an absolute rotation from the ExtendedMove class?
  406. if(con && con->getControlSchemeAbsoluteRotation())
  407. {
  408. doStandardMove = false;
  409. const ExtendedMove* emove = dynamic_cast<const ExtendedMove*>(move);
  410. U32 emoveIndex = smExtendedMovePosRotIndex;
  411. if(emoveIndex >= ExtendedMove::MaxPositionsRotations)
  412. emoveIndex = 0;
  413. if(emove->EulerBasedRotation[emoveIndex])
  414. {
  415. if(virtualMode != StationaryMode &&
  416. virtualMode != TrackObjectMode &&
  417. (!mLocked || virtualMode != OrbitObjectMode && virtualMode != OrbitPointMode))
  418. {
  419. // Pitch
  420. mRot.x += (emove->rotX[emoveIndex] - mLastAbsolutePitch);
  421. // Do we also include the relative pitch value?
  422. if(con->getControlSchemeAddPitchToAbsRot() && !strafeMode)
  423. {
  424. F32 x = move->pitch;
  425. if (x > M_PI_F)
  426. x -= M_2PI_F;
  427. mRot.x += x;
  428. }
  429. // Constrain the range of mRot.x
  430. while (mRot.x < -M_PI_F)
  431. mRot.x += M_2PI_F;
  432. while (mRot.x > M_PI_F)
  433. mRot.x -= M_2PI_F;
  434. // Yaw
  435. mRot.z += (emove->rotZ[emoveIndex] - mLastAbsoluteYaw);
  436. // Do we also include the relative yaw value?
  437. if(con->getControlSchemeAddYawToAbsRot() && !strafeMode)
  438. {
  439. F32 z = move->yaw;
  440. if (z > M_PI_F)
  441. z -= M_2PI_F;
  442. mRot.z += z;
  443. }
  444. // Constrain the range of mRot.z
  445. while (mRot.z < -M_PI_F)
  446. mRot.z += M_2PI_F;
  447. while (mRot.z > M_PI_F)
  448. mRot.z -= M_2PI_F;
  449. mLastAbsoluteYaw = emove->rotZ[emoveIndex];
  450. mLastAbsolutePitch = emove->rotX[emoveIndex];
  451. // Bank
  452. mRot.y = emove->rotY[emoveIndex];
  453. // Constrain the range of mRot.y
  454. while (mRot.y > M_PI_F)
  455. mRot.y -= M_2PI_F;
  456. }
  457. }
  458. }
  459. #endif
  460. if(doStandardMove)
  461. {
  462. // process input/determine rotation vector
  463. if(virtualMode != StationaryMode &&
  464. virtualMode != TrackObjectMode &&
  465. (!mLocked || virtualMode != OrbitObjectMode && virtualMode != OrbitPointMode))
  466. {
  467. if(!strafeMode)
  468. {
  469. rotVec.x = move->pitch;
  470. rotVec.z = move->yaw;
  471. }
  472. }
  473. else if(virtualMode == TrackObjectMode && bool(mOrbitObject))
  474. {
  475. // orient the camera to face the object
  476. Point3F objPos;
  477. // If this is a shapebase, use its render eye transform
  478. // to avoid jittering.
  479. ShapeBase *shape = dynamic_cast<ShapeBase*>((GameBase*)mOrbitObject);
  480. if( shape != NULL )
  481. {
  482. MatrixF ret;
  483. shape->getRenderEyeTransform( &ret );
  484. objPos = ret.getPosition();
  485. }
  486. else
  487. {
  488. mOrbitObject->getWorldBox().getCenter(&objPos);
  489. }
  490. mObjToWorld.getColumn(3,&pos);
  491. vec = objPos - pos;
  492. vec.normalizeSafe();
  493. F32 pitch, yaw;
  494. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  495. rotVec.x = -pitch - mRot.x;
  496. rotVec.z = yaw - mRot.z;
  497. if(rotVec.z > M_PI_F)
  498. rotVec.z -= M_2PI_F;
  499. else if(rotVec.z < -M_PI_F)
  500. rotVec.z += M_2PI_F;
  501. }
  502. // apply rotation vector according to physics rules
  503. if(mNewtonRotation)
  504. {
  505. const F32 force = mAngularForce;
  506. const F32 drag = mAngularDrag;
  507. VectorF acc(0.0f, 0.0f, 0.0f);
  508. rotVec.x *= 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  509. rotVec.z *= 2.0f; // Assume that our -2PI to 2PI range was clamped to -PI to PI in script
  510. F32 rotVecL = rotVec.len();
  511. if(rotVecL > 0)
  512. {
  513. acc = (rotVec * force / mMass) * TickSec;
  514. }
  515. // Accelerate
  516. mAngularVelocity += acc;
  517. // Drag
  518. mAngularVelocity -= mAngularVelocity * drag * TickSec;
  519. // Rotate
  520. mRot += mAngularVelocity * TickSec;
  521. clampPitchAngle(mRot.x);
  522. }
  523. else
  524. {
  525. mRot.x += rotVec.x;
  526. mRot.z += rotVec.z;
  527. clampPitchAngle(mRot.x);
  528. }
  529. }
  530. // Update position
  531. VectorF posVec(0, 0, 0);
  532. bool mustValidateEyePoint = false;
  533. bool serverInterpolate = false;
  534. // process input/determine translation vector
  535. if(virtualMode == OrbitObjectMode || virtualMode == OrbitPointMode)
  536. {
  537. pos = mDelta.pos;
  538. if(virtualMode == OrbitObjectMode && bool(mOrbitObject))
  539. {
  540. // If this is a shapebase, use its render eye transform
  541. // to avoid jittering.
  542. GameBase *castObj = mOrbitObject;
  543. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  544. if( shape != NULL )
  545. {
  546. MatrixF ret;
  547. shape->getRenderEyeTransform( &ret );
  548. mPosition = ret.getPosition();
  549. }
  550. else
  551. {
  552. // Hopefully this is a static object that doesn't move,
  553. // because the worldbox doesn't get updated between ticks.
  554. mOrbitObject->getWorldBox().getCenter(&mPosition);
  555. }
  556. }
  557. posVec = (mPosition + mOffset) - pos;
  558. mustValidateEyePoint = true;
  559. serverInterpolate = mNewtonMode;
  560. }
  561. else if(virtualMode == EditOrbitMode && mValidEditOrbitPoint)
  562. {
  563. bool faster = move->trigger[0] || move->trigger[1];
  564. F32 scale = smMovementSpeed * (faster + 1);
  565. mCurrentEditOrbitDist -= move->y * TickSec * scale;
  566. 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
  567. if(mCurrentEditOrbitDist < 0.0f)
  568. mCurrentEditOrbitDist = 0.0f;
  569. mPosition = mEditOrbitPoint;
  570. _setPosition(mPosition, mRot);
  571. _calcEditOrbitPoint(&mObjToWorld, mRot);
  572. pos = mPosition;
  573. }
  574. else if(virtualMode == FlyMode)
  575. {
  576. bool faster = move->trigger[0] || move->trigger[1];
  577. F32 scale = smMovementSpeed * (faster + 1);
  578. mObjToWorld.getColumn(3,&pos);
  579. mObjToWorld.getColumn(0,&vec);
  580. posVec = vec * move->x * TickSec * scale + vec * (strafeMode ? move->yaw * 2.0f * TickSec * scale : 0.0f);
  581. mObjToWorld.getColumn(1,&vec);
  582. posVec += vec * move->y * TickSec * scale + vec * move->roll * TickSec * scale;
  583. mObjToWorld.getColumn(2,&vec);
  584. posVec += vec * move->z * TickSec * scale - vec * (strafeMode ? move->pitch * 2.0f * TickSec * scale : 0.0f);
  585. }
  586. else if(virtualMode == OverheadMode)
  587. {
  588. bool faster = move->trigger[0] || move->trigger[1];
  589. F32 scale = smMovementSpeed * (faster + 1);
  590. mObjToWorld.getColumn(3,&pos);
  591. mObjToWorld.getColumn(0,&vec);
  592. vec.z = 0;
  593. vec.normalizeSafe();
  594. vec = vec * move->x * TickSec * scale + (strafeMode ? vec * move->yaw * 2.0f * TickSec * scale : Point3F(0, 0, 0));
  595. posVec = vec;
  596. mObjToWorld.getColumn(1,&vec);
  597. vec.z = 0;
  598. if (vec.isZero())
  599. {
  600. mObjToWorld.getColumn(2,&vec);
  601. vec.z = 0;
  602. }
  603. vec.normalizeSafe();
  604. vec = vec * move->y * TickSec * scale - (strafeMode ? vec * move->pitch * 2.0f * TickSec * scale : Point3F(0, 0, 0));
  605. posVec += vec;
  606. posVec.z += move->z * TickSec * scale + move->roll * TickSec * scale;
  607. }
  608. else // ignore input
  609. {
  610. mObjToWorld.getColumn(3,&pos);
  611. }
  612. // apply translation vector according to physics rules
  613. mDelta.posVec = pos;
  614. if(mNewtonMode)
  615. {
  616. bool faster = move->trigger[0];
  617. bool brake = move->trigger[1];
  618. const F32 movementSpeedMultiplier = smMovementSpeed / 40.0f; // Using the starting value as the base
  619. const F32 force = faster ? mFlyForce * movementSpeedMultiplier * mSpeedMultiplier : mFlyForce * movementSpeedMultiplier;
  620. const F32 drag = brake ? mDrag * mBrakeMultiplier : mDrag;
  621. VectorF acc(0.0f, 0.0f, 0.0f);
  622. F32 posVecL = posVec.len();
  623. if(posVecL > 0)
  624. {
  625. acc = (posVec * force / mMass) * TickSec;
  626. }
  627. // Accelerate
  628. mVelocity += acc;
  629. // Drag
  630. mVelocity -= mVelocity * drag * TickSec;
  631. // Move
  632. pos += mVelocity * TickSec;
  633. }
  634. else
  635. {
  636. pos += posVec;
  637. }
  638. _setPosition(pos,mRot);
  639. // If on the client, calc delta for backstepping
  640. if (serverInterpolate || isClientObject())
  641. {
  642. mDelta.pos = pos;
  643. mDelta.rot = mRot;
  644. mDelta.posVec = mDelta.posVec - mDelta.pos;
  645. mDelta.rotVec = mDelta.rotVec - mDelta.rot;
  646. for(U32 i=0; i<3; ++i)
  647. {
  648. if (mDelta.rotVec[i] > M_PI_F)
  649. mDelta.rotVec[i] -= M_2PI_F;
  650. else if (mDelta.rotVec[i] < -M_PI_F)
  651. mDelta.rotVec[i] += M_2PI_F;
  652. }
  653. }
  654. if(mustValidateEyePoint)
  655. _validateEyePoint(1.0f, &mObjToWorld);
  656. setMaskBits(MoveMask);
  657. }
  658. // Need to calculate the orbit position for the editor so the camera position
  659. // doesn't change when switching between orbit and other camera modes
  660. if (isServerObject() && (mMode == EditOrbitMode && mValidEditOrbitPoint))
  661. _calcEditOrbitPoint(&mObjToWorld, mRot);
  662. if(getControllingClient() && mContainer)
  663. updateContainer();
  664. }
  665. //----------------------------------------------------------------------------
  666. void Camera::onDeleteNotify( SimObject* obj )
  667. {
  668. if( obj == mOrbitObject )
  669. {
  670. mOrbitObject = NULL;
  671. if( mMode == OrbitObjectMode )
  672. mMode = OrbitPointMode;
  673. }
  674. Parent::onDeleteNotify( obj );
  675. }
  676. //----------------------------------------------------------------------------
  677. void Camera::interpolateTick(F32 dt)
  678. {
  679. Parent::interpolateTick(dt);
  680. if ( isMounted() )
  681. {
  682. // Fetch Mount Transform.
  683. MatrixF mat;
  684. mMount.object->getRenderMountTransform( dt, mMount.node, mMount.xfm, &mat );
  685. // Apply.
  686. setRenderTransform( mat );
  687. return;
  688. }
  689. Point3F rot = mDelta.rot + mDelta.rotVec * dt;
  690. if((mMode == OrbitObjectMode || mMode == OrbitPointMode) && !mNewtonMode)
  691. {
  692. if(mMode == OrbitObjectMode && bool(mOrbitObject))
  693. {
  694. // If this is a shapebase, use its render eye transform
  695. // to avoid jittering.
  696. GameBase *castObj = mOrbitObject;
  697. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  698. if( shape != NULL )
  699. {
  700. MatrixF ret;
  701. shape->getRenderEyeTransform( &ret );
  702. mPosition = ret.getPosition();
  703. }
  704. else
  705. {
  706. // Hopefully this is a static object that doesn't move,
  707. // because the worldbox doesn't get updated between ticks.
  708. mOrbitObject->getWorldBox().getCenter(&mPosition);
  709. }
  710. }
  711. _setRenderPosition( mPosition + mOffset, rot );
  712. _validateEyePoint( 1.0f, &mRenderObjToWorld );
  713. }
  714. else if(mMode == EditOrbitMode && mValidEditOrbitPoint)
  715. {
  716. mPosition = mEditOrbitPoint;
  717. _setRenderPosition(mPosition, rot);
  718. _calcEditOrbitPoint(&mRenderObjToWorld, rot);
  719. }
  720. else if(mMode == TrackObjectMode && bool(mOrbitObject) && !mNewtonRotation)
  721. {
  722. // orient the camera to face the object
  723. Point3F objPos;
  724. // If this is a shapebase, use its render eye transform
  725. // to avoid jittering.
  726. ShapeBase *shape = dynamic_cast<ShapeBase*>((GameBase*)mOrbitObject);
  727. if( shape != NULL )
  728. {
  729. MatrixF ret;
  730. shape->getRenderEyeTransform( &ret );
  731. objPos = ret.getPosition();
  732. }
  733. else
  734. {
  735. mOrbitObject->getWorldBox().getCenter(&objPos);
  736. }
  737. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  738. Point3F vec = objPos - pos;
  739. vec.normalizeSafe();
  740. F32 pitch, yaw;
  741. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  742. rot.x = -pitch;
  743. rot.z = yaw;
  744. _setRenderPosition(pos, rot);
  745. }
  746. else
  747. {
  748. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  749. _setRenderPosition(pos,rot);
  750. if(mMode == OrbitObjectMode || mMode == OrbitPointMode)
  751. _validateEyePoint(1.0f, &mRenderObjToWorld);
  752. }
  753. }
  754. //----------------------------------------------------------------------------
  755. void Camera::_setPosition(const Point3F& pos, const Point3F& rot)
  756. {
  757. MatrixF xRot, zRot;
  758. xRot.set(EulerF(rot.x, 0.0f, 0.0f));
  759. zRot.set(EulerF(0.0f, 0.0f, rot.z));
  760. MatrixF temp;
  761. if(mDataBlock && mDataBlock->cameraCanBank)
  762. {
  763. // Take rot.y into account to bank the camera
  764. MatrixF imat;
  765. imat.mul(zRot, xRot);
  766. MatrixF ymat;
  767. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  768. temp.mul(imat, ymat);
  769. }
  770. else
  771. {
  772. temp.mul(zRot, xRot);
  773. }
  774. temp.setColumn(3, pos);
  775. Parent::setTransform(temp);
  776. mRot = rot;
  777. }
  778. //----------------------------------------------------------------------------
  779. void Camera::setRotation(const Point3F& rot)
  780. {
  781. MatrixF xRot, zRot;
  782. xRot.set(EulerF(rot.x, 0.0f, 0.0f));
  783. zRot.set(EulerF(0.0f, 0.0f, rot.z));
  784. MatrixF temp;
  785. if(mDataBlock && mDataBlock->cameraCanBank)
  786. {
  787. // Take rot.y into account to bank the camera
  788. MatrixF imat;
  789. imat.mul(zRot, xRot);
  790. MatrixF ymat;
  791. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  792. temp.mul(imat, ymat);
  793. }
  794. else
  795. {
  796. temp.mul(zRot, xRot);
  797. }
  798. temp.setColumn(3, getPosition());
  799. Parent::setTransform(temp);
  800. mRot = rot;
  801. }
  802. //----------------------------------------------------------------------------
  803. void Camera::_setRenderPosition(const Point3F& pos,const Point3F& rot)
  804. {
  805. MatrixF xRot, zRot;
  806. xRot.set(EulerF(rot.x, 0, 0));
  807. zRot.set(EulerF(0, 0, rot.z));
  808. MatrixF temp;
  809. // mDataBlock may not be defined yet as this method is called during
  810. // SceneObject::onAdd().
  811. if(mDataBlock && mDataBlock->cameraCanBank)
  812. {
  813. // Take rot.y into account to bank the camera
  814. MatrixF imat;
  815. imat.mul(zRot, xRot);
  816. MatrixF ymat;
  817. ymat.set(EulerF(0.0f, rot.y, 0.0f));
  818. temp.mul(imat, ymat);
  819. }
  820. else
  821. {
  822. temp.mul(zRot, xRot);
  823. }
  824. temp.setColumn(3, pos);
  825. Parent::setRenderTransform(temp);
  826. }
  827. //----------------------------------------------------------------------------
  828. void Camera::writePacketData(GameConnection *connection, BitStream *bstream)
  829. {
  830. // Update client regardless of status flags.
  831. Parent::writePacketData(connection, bstream);
  832. Point3F pos;
  833. mObjToWorld.getColumn(3,&pos);
  834. bstream->setCompressionPoint(pos);
  835. mathWrite(*bstream, pos);
  836. bstream->write(mRot.x);
  837. if(mDataBlock && bstream->writeFlag(mDataBlock->cameraCanBank))
  838. {
  839. // Include mRot.y to allow for camera banking
  840. bstream->write(mRot.y);
  841. }
  842. bstream->write(mRot.z);
  843. U32 writeMode = mMode;
  844. Point3F writePos = mPosition;
  845. S32 gIndex = -1;
  846. if(mMode == OrbitObjectMode)
  847. {
  848. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  849. if(gIndex == -1)
  850. {
  851. writeMode = OrbitPointMode;
  852. if(bool(mOrbitObject))
  853. mOrbitObject->getWorldBox().getCenter(&writePos);
  854. }
  855. }
  856. else if(mMode == TrackObjectMode)
  857. {
  858. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  859. if(gIndex == -1)
  860. writeMode = StationaryMode;
  861. }
  862. bstream->writeRangedU32(writeMode, CameraFirstMode, CameraLastMode);
  863. if (writeMode == OrbitObjectMode || writeMode == OrbitPointMode)
  864. {
  865. bstream->write(mMinOrbitDist);
  866. bstream->write(mMaxOrbitDist);
  867. bstream->write(mCurOrbitDist);
  868. if(writeMode == OrbitObjectMode)
  869. {
  870. bstream->writeFlag(mObservingClientObject);
  871. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize);
  872. }
  873. if (writeMode == OrbitPointMode)
  874. bstream->writeCompressedPoint(writePos);
  875. }
  876. else if(writeMode == TrackObjectMode)
  877. {
  878. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize);
  879. }
  880. if(bstream->writeFlag(mNewtonMode))
  881. {
  882. bstream->write(mVelocity.x);
  883. bstream->write(mVelocity.y);
  884. bstream->write(mVelocity.z);
  885. }
  886. if(bstream->writeFlag(mNewtonRotation))
  887. {
  888. bstream->write(mAngularVelocity.x);
  889. bstream->write(mAngularVelocity.y);
  890. bstream->write(mAngularVelocity.z);
  891. }
  892. bstream->writeFlag(mValidEditOrbitPoint);
  893. if(writeMode == EditOrbitMode)
  894. {
  895. bstream->write(mEditOrbitPoint.x);
  896. bstream->write(mEditOrbitPoint.y);
  897. bstream->write(mEditOrbitPoint.z);
  898. bstream->write(mCurrentEditOrbitDist);
  899. }
  900. }
  901. //----------------------------------------------------------------------------
  902. void Camera::readPacketData(GameConnection *connection, BitStream *bstream)
  903. {
  904. Parent::readPacketData(connection, bstream);
  905. Point3F pos,rot;
  906. mathRead(*bstream, &pos);
  907. bstream->setCompressionPoint(pos);
  908. bstream->read(&rot.x);
  909. if(bstream->readFlag())
  910. {
  911. // Include rot.y to allow for camera banking
  912. bstream->read(&rot.y);
  913. }
  914. bstream->read(&rot.z);
  915. GameBase* obj = 0;
  916. mMode = (CameraMotionMode)bstream->readRangedU32(CameraFirstMode, CameraLastMode);
  917. mObservingClientObject = false;
  918. if (mMode == OrbitObjectMode || mMode == OrbitPointMode)
  919. {
  920. bstream->read(&mMinOrbitDist);
  921. bstream->read(&mMaxOrbitDist);
  922. bstream->read(&mCurOrbitDist);
  923. if(mMode == OrbitObjectMode)
  924. {
  925. mObservingClientObject = bstream->readFlag();
  926. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  927. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  928. }
  929. if (mMode == OrbitPointMode)
  930. bstream->readCompressedPoint(&mPosition);
  931. }
  932. else if (mMode == TrackObjectMode)
  933. {
  934. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  935. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  936. }
  937. if (obj != (GameBase*)mOrbitObject)
  938. {
  939. if (mOrbitObject)
  940. {
  941. clearProcessAfter();
  942. clearNotify(mOrbitObject);
  943. }
  944. mOrbitObject = obj;
  945. if (mOrbitObject)
  946. {
  947. processAfter(mOrbitObject);
  948. deleteNotify(mOrbitObject);
  949. }
  950. }
  951. mNewtonMode = bstream->readFlag();
  952. if(mNewtonMode)
  953. {
  954. bstream->read(&mVelocity.x);
  955. bstream->read(&mVelocity.y);
  956. bstream->read(&mVelocity.z);
  957. }
  958. mNewtonRotation = bstream->readFlag();
  959. if(mNewtonRotation)
  960. {
  961. bstream->read(&mAngularVelocity.x);
  962. bstream->read(&mAngularVelocity.y);
  963. bstream->read(&mAngularVelocity.z);
  964. }
  965. mValidEditOrbitPoint = bstream->readFlag();
  966. if(mMode == EditOrbitMode)
  967. {
  968. bstream->read(&mEditOrbitPoint.x);
  969. bstream->read(&mEditOrbitPoint.y);
  970. bstream->read(&mEditOrbitPoint.z);
  971. bstream->read(&mCurrentEditOrbitDist);
  972. }
  973. _setPosition(pos,rot);
  974. // Movement in OrbitObjectMode is not input-based - don't reset interpolation
  975. if(mMode != OrbitObjectMode)
  976. {
  977. mDelta.pos = pos;
  978. mDelta.posVec.set(0.0f, 0.0f, 0.0f);
  979. mDelta.rot = rot;
  980. mDelta.rotVec.set(0.0f, 0.0f, 0.0f);
  981. }
  982. }
  983. //----------------------------------------------------------------------------
  984. U32 Camera::packUpdate(NetConnection *con, U32 mask, BitStream *bstream)
  985. {
  986. Parent::packUpdate(con, mask, bstream);
  987. if (bstream->writeFlag(mask & UpdateMask))
  988. {
  989. bstream->writeFlag(mLocked);
  990. mathWrite(*bstream, mOffset);
  991. }
  992. if(bstream->writeFlag(mask & NewtonCameraMask))
  993. {
  994. bstream->write(mAngularForce);
  995. bstream->write(mAngularDrag);
  996. bstream->write(mMass);
  997. bstream->write(mDrag);
  998. bstream->write(mFlyForce);
  999. bstream->write(mSpeedMultiplier);
  1000. bstream->write(mBrakeMultiplier);
  1001. }
  1002. if(bstream->writeFlag(mask & EditOrbitMask))
  1003. {
  1004. bstream->write(mEditOrbitPoint.x);
  1005. bstream->write(mEditOrbitPoint.y);
  1006. bstream->write(mEditOrbitPoint.z);
  1007. bstream->write(mCurrentEditOrbitDist);
  1008. }
  1009. // The rest of the data is part of the control object packet update.
  1010. // If we're controlled by this client, we don't need to send it.
  1011. if(bstream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  1012. return 0;
  1013. if (bstream->writeFlag(mask & MoveMask))
  1014. {
  1015. Point3F pos;
  1016. mObjToWorld.getColumn(3,&pos);
  1017. bstream->write(pos.x);
  1018. bstream->write(pos.y);
  1019. bstream->write(pos.z);
  1020. bstream->write(mRot.x);
  1021. bstream->write(mRot.z);
  1022. // Only required if in NewtonFlyMode
  1023. F32 len = mVelocity.len();
  1024. if(bstream->writeFlag(mNewtonMode && len > 0.02f))
  1025. {
  1026. Point3F outVel = mVelocity;
  1027. outVel *= 1.0f/len;
  1028. bstream->writeNormalVector(outVel, 10);
  1029. len *= 32.0f; // 5 bits of fraction
  1030. if(len > 8191)
  1031. len = 8191;
  1032. bstream->writeInt((S32)len, 13);
  1033. }
  1034. // Rotation
  1035. len = mAngularVelocity.len();
  1036. if(bstream->writeFlag(mNewtonRotation && len > 0.02f))
  1037. {
  1038. Point3F outVel = mAngularVelocity;
  1039. outVel *= 1.0f/len;
  1040. bstream->writeNormalVector(outVel, 10);
  1041. len *= 32.0f; // 5 bits of fraction
  1042. if(len > 8191)
  1043. len = 8191;
  1044. bstream->writeInt((S32)len, 13);
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049. //----------------------------------------------------------------------------
  1050. void Camera::unpackUpdate(NetConnection *con, BitStream *bstream)
  1051. {
  1052. Parent::unpackUpdate(con,bstream);
  1053. if (bstream->readFlag())
  1054. {
  1055. mLocked = bstream->readFlag();
  1056. mathRead(*bstream, &mOffset);
  1057. }
  1058. // NewtonCameraMask
  1059. if(bstream->readFlag())
  1060. {
  1061. bstream->read(&mAngularForce);
  1062. bstream->read(&mAngularDrag);
  1063. bstream->read(&mMass);
  1064. bstream->read(&mDrag);
  1065. bstream->read(&mFlyForce);
  1066. bstream->read(&mSpeedMultiplier);
  1067. bstream->read(&mBrakeMultiplier);
  1068. }
  1069. // EditOrbitMask
  1070. if(bstream->readFlag())
  1071. {
  1072. bstream->read(&mEditOrbitPoint.x);
  1073. bstream->read(&mEditOrbitPoint.y);
  1074. bstream->read(&mEditOrbitPoint.z);
  1075. bstream->read(&mCurrentEditOrbitDist);
  1076. }
  1077. // controlled by the client?
  1078. if(bstream->readFlag())
  1079. return;
  1080. // MoveMask
  1081. if (bstream->readFlag())
  1082. {
  1083. Point3F pos,rot;
  1084. bstream->read(&pos.x);
  1085. bstream->read(&pos.y);
  1086. bstream->read(&pos.z);
  1087. bstream->read(&rot.x);
  1088. bstream->read(&rot.z);
  1089. _setPosition(pos,rot);
  1090. // NewtonMode
  1091. if(bstream->readFlag())
  1092. {
  1093. bstream->readNormalVector(&mVelocity, 10);
  1094. mVelocity *= bstream->readInt(13) / 32.0f;
  1095. }
  1096. // NewtonRotation
  1097. mNewtonRotation = bstream->readFlag();
  1098. if(mNewtonRotation)
  1099. {
  1100. bstream->readNormalVector(&mAngularVelocity, 10);
  1101. mAngularVelocity *= bstream->readInt(13) / 32.0f;
  1102. }
  1103. if(mMode != OrbitObjectMode)
  1104. {
  1105. // New delta for client side interpolation
  1106. mDelta.pos = pos;
  1107. mDelta.rot = rot;
  1108. mDelta.posVec = mDelta.rotVec = VectorF(0.0f, 0.0f, 0.0f);
  1109. }
  1110. }
  1111. }
  1112. //----------------------------------------------------------------------------
  1113. void Camera::initPersistFields()
  1114. {
  1115. addGroup( "Camera" );
  1116. addProtectedField( "controlMode", TYPEID< CameraMotionMode >(), Offset( mMode, Camera ),
  1117. &_setModeField, &defaultProtectedGetFn,
  1118. "The current camera control mode." );
  1119. endGroup( "Camera" );
  1120. addGroup( "Camera: Newton Mode" );
  1121. addField( "newtonMode", TypeBool, Offset(mNewtonMode, Camera),
  1122. "Apply smoothing (acceleration and damping) to camera movements." );
  1123. addField( "newtonRotation", TypeBool, Offset(mNewtonRotation, Camera),
  1124. "Apply smoothing (acceleration and damping) to camera rotations." );
  1125. addProtectedField( "mass", TypeF32, Offset(mMass, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1126. "The camera's mass (Newton mode only). Default value is 10." );
  1127. addProtectedField( "drag", TypeF32, Offset(mDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1128. "Drag on camera when moving (Newton mode only). Default value is 2." );
  1129. addProtectedField( "force", TypeF32, Offset(mFlyForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1130. "Force applied on camera when asked to move (Newton mode only). Default value is 500." );
  1131. addProtectedField( "angularDrag", TypeF32, Offset(mAngularDrag, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1132. "Drag on camera when rotating (Newton mode only). Default value is 2." );
  1133. addProtectedField( "angularForce", TypeF32, Offset(mAngularForce, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1134. "Force applied on camera when asked to rotate (Newton mode only). Default value is 100." );
  1135. addProtectedField( "speedMultiplier", TypeF32, Offset(mSpeedMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1136. "Speed multiplier when triggering the accelerator (Newton mode only). Default value is 2." );
  1137. addProtectedField( "brakeMultiplier", TypeF32, Offset(mBrakeMultiplier, Camera), &_setNewtonField, &defaultProtectedGetFn,
  1138. "Speed multiplier when triggering the brake (Newton mode only). Default value is 2." );
  1139. endGroup( "Camera: Newton Mode" );
  1140. Parent::initPersistFields();
  1141. }
  1142. //-----------------------------------------------------------------------------
  1143. void Camera::consoleInit()
  1144. {
  1145. Con::addVariable("$Camera::movementSpeed", TypeF32, &smMovementSpeed,
  1146. "@brief Global camera movement speed in units/s (typically m/s), with a base value of 40.\n\n"
  1147. "Used in the following camera modes:\n"
  1148. "- Edit Orbit Mode\n"
  1149. "- Fly Mode\n"
  1150. "- Overhead Mode\n"
  1151. "@ingroup BaseCamera\n");
  1152. // ExtendedMove support
  1153. Con::addVariable("$camera::extendedMovePosRotIndex", TypeS32, &smExtendedMovePosRotIndex,
  1154. "@brief The ExtendedMove position/rotation index used for camera movements.\n\n"
  1155. "@ingroup BaseCamera\n");
  1156. }
  1157. //-----------------------------------------------------------------------------
  1158. bool Camera::_setNewtonField( void *object, const char *index, const char *data )
  1159. {
  1160. static_cast<Camera*>(object)->setMaskBits(NewtonCameraMask);
  1161. return true; // ok to set value
  1162. }
  1163. //-----------------------------------------------------------------------------
  1164. bool Camera::_setModeField( void *object, const char *index, const char *data )
  1165. {
  1166. Camera *cam = static_cast<Camera*>( object );
  1167. if( dStricmp(data, "Fly") == 0 )
  1168. {
  1169. cam->setFlyMode();
  1170. return false; // already changed mode
  1171. }
  1172. else if( dStricmp(data, "EditOrbit" ) == 0 )
  1173. {
  1174. cam->setEditOrbitMode();
  1175. return false; // already changed mode
  1176. }
  1177. else if( (dStricmp(data, "OrbitObject") == 0 && cam->mMode != OrbitObjectMode) ||
  1178. (dStricmp(data, "TrackObject") == 0 && cam->mMode != TrackObjectMode) ||
  1179. (dStricmp(data, "OrbitPoint") == 0 && cam->mMode != OrbitPointMode) )
  1180. {
  1181. Con::warnf("Couldn't change Camera mode to %s: required information missing. Use camera.set%s().", data, data);
  1182. return false; // don't change the mode - not valid
  1183. }
  1184. else if( dStricmp(data, "OrbitObject") != 0 &&
  1185. dStricmp(data, "TrackObject") != 0 &&
  1186. bool(cam->mOrbitObject) )
  1187. {
  1188. cam->clearProcessAfter();
  1189. cam->clearNotify(cam->mOrbitObject);
  1190. cam->mOrbitObject = NULL;
  1191. }
  1192. // make sure the requested mode is supported, and set it
  1193. // NOTE: The _CameraMode namespace is generated by ImplementEnumType above
  1194. const EngineEnumTable& enums = *( TYPE< CameraMotionMode >()->getEnumTable() );
  1195. const U32 numValues = enums.getNumValues();
  1196. for( S32 i = 0; i < numValues; ++i)
  1197. {
  1198. if( dStricmp(data, enums[i].mName) == 0 )
  1199. {
  1200. cam->mMode = (CameraMotionMode) enums[i].mInt;
  1201. return false;
  1202. }
  1203. }
  1204. Con::warnf("Unsupported camera mode: %s", data);
  1205. return false;
  1206. }
  1207. //-----------------------------------------------------------------------------
  1208. Point3F Camera::getPosition()
  1209. {
  1210. static Point3F position;
  1211. mObjToWorld.getColumn(3, &position);
  1212. return position;
  1213. }
  1214. //-----------------------------------------------------------------------------
  1215. void Camera::setFlyMode()
  1216. {
  1217. mMode = FlyMode;
  1218. if (bool(mOrbitObject))
  1219. {
  1220. clearProcessAfter();
  1221. clearNotify(mOrbitObject);
  1222. }
  1223. mOrbitObject = NULL;
  1224. }
  1225. //-----------------------------------------------------------------------------
  1226. void Camera::setNewtonFlyMode()
  1227. {
  1228. mNewtonMode = true;
  1229. setFlyMode();
  1230. }
  1231. //-----------------------------------------------------------------------------
  1232. void Camera::setOrbitMode(GameBase *obj, const Point3F &pos, const Point3F &rot, const Point3F& offset, F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked)
  1233. {
  1234. mObservingClientObject = ownClientObject;
  1235. if (bool(mOrbitObject))
  1236. {
  1237. clearProcessAfter();
  1238. clearNotify(mOrbitObject);
  1239. }
  1240. mOrbitObject = obj;
  1241. if(bool(mOrbitObject))
  1242. {
  1243. processAfter(mOrbitObject);
  1244. deleteNotify(mOrbitObject);
  1245. mOrbitObject->getWorldBox().getCenter(&mPosition);
  1246. mMode = OrbitObjectMode;
  1247. }
  1248. else
  1249. {
  1250. mMode = OrbitPointMode;
  1251. mPosition = pos;
  1252. }
  1253. _setPosition(mPosition, rot);
  1254. mMinOrbitDist = minDist;
  1255. mMaxOrbitDist = maxDist;
  1256. mCurOrbitDist = curDist;
  1257. if (locked != mLocked || mOffset != offset)
  1258. {
  1259. mLocked = locked;
  1260. mOffset = offset;
  1261. setMaskBits(UpdateMask);
  1262. }
  1263. }
  1264. //-----------------------------------------------------------------------------
  1265. void Camera::setTrackObject(GameBase *obj, const Point3F &offset)
  1266. {
  1267. if(bool(mOrbitObject))
  1268. {
  1269. clearProcessAfter();
  1270. clearNotify(mOrbitObject);
  1271. }
  1272. mOrbitObject = obj;
  1273. if(bool(mOrbitObject))
  1274. {
  1275. processAfter(mOrbitObject);
  1276. deleteNotify(mOrbitObject);
  1277. }
  1278. mOffset = offset;
  1279. mMode = TrackObjectMode;
  1280. setMaskBits( UpdateMask );
  1281. }
  1282. //-----------------------------------------------------------------------------
  1283. void Camera::_validateEyePoint(F32 pos, MatrixF *mat)
  1284. {
  1285. if (pos != 0)
  1286. {
  1287. // Use the eye transform to orient the camera
  1288. Point3F dir;
  1289. mat->getColumn(1, &dir);
  1290. if (mMaxOrbitDist - mMinOrbitDist > 0.0f)
  1291. pos *= mMaxOrbitDist - mMinOrbitDist;
  1292. // Use the camera node's pos.
  1293. Point3F startPos = getRenderPosition();
  1294. Point3F endPos;
  1295. // Make sure we don't extend the camera into anything solid
  1296. if(mOrbitObject)
  1297. mOrbitObject->disableCollision();
  1298. disableCollision();
  1299. RayInfo collision;
  1300. U32 mask = TerrainObjectType |
  1301. WaterObjectType |
  1302. StaticShapeObjectType |
  1303. PlayerObjectType |
  1304. ItemObjectType |
  1305. VehicleObjectType;
  1306. SceneContainer* pContainer = isServerObject() ? &gServerContainer : &gClientContainer;
  1307. if (!pContainer->castRay(startPos, startPos - dir * 2.5 * pos, mask, &collision))
  1308. endPos = startPos - dir * pos;
  1309. else
  1310. {
  1311. float dot = mDot(dir, collision.normal);
  1312. if (dot > 0.01f)
  1313. {
  1314. F32 colDist = mDot(startPos - collision.point, dir) - (1 / dot) * CameraRadius;
  1315. if (colDist > pos)
  1316. colDist = pos;
  1317. if (colDist < 0.0f)
  1318. colDist = 0.0f;
  1319. endPos = startPos - dir * colDist;
  1320. }
  1321. else
  1322. endPos = startPos - dir * pos;
  1323. }
  1324. mat->setColumn(3, endPos);
  1325. enableCollision();
  1326. if(mOrbitObject)
  1327. mOrbitObject->enableCollision();
  1328. }
  1329. }
  1330. //-----------------------------------------------------------------------------
  1331. void Camera::setTransform(const MatrixF& mat)
  1332. {
  1333. // This method should never be called on the client.
  1334. // This currently converts all rotation in the mat into
  1335. // rotations around the z and x axis.
  1336. Point3F pos,vec;
  1337. mat.getColumn(1, &vec);
  1338. mat.getColumn(3, &pos);
  1339. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x * vec.x + vec.y * vec.y)), 0.0f, -mAtan2(-vec.x, vec.y));
  1340. _setPosition(pos,rot);
  1341. }
  1342. //-----------------------------------------------------------------------------
  1343. void Camera::setRenderTransform(const MatrixF& mat)
  1344. {
  1345. // This method should never be called on the client.
  1346. // This currently converts all rotation in the mat into
  1347. // rotations around the z and x axis.
  1348. Point3F pos,vec;
  1349. mat.getColumn(1,&vec);
  1350. mat.getColumn(3,&pos);
  1351. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)),0,-mAtan2(-vec.x,vec.y));
  1352. _setRenderPosition(pos,rot);
  1353. }
  1354. //-----------------------------------------------------------------------------
  1355. F32 Camera::getDamageFlash() const
  1356. {
  1357. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1358. {
  1359. const GameBase *castObj = mOrbitObject;
  1360. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1361. if (psb)
  1362. return psb->getDamageFlash();
  1363. }
  1364. return mDamageFlash;
  1365. }
  1366. //-----------------------------------------------------------------------------
  1367. F32 Camera::getWhiteOut() const
  1368. {
  1369. if (mMode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  1370. {
  1371. const GameBase *castObj = mOrbitObject;
  1372. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  1373. if (psb)
  1374. return psb->getWhiteOut();
  1375. }
  1376. return mWhiteOut;
  1377. }
  1378. //-----------------------------------------------------------------------------
  1379. void Camera::setVelocity(const VectorF& vel)
  1380. {
  1381. mVelocity = vel;
  1382. setMaskBits(MoveMask);
  1383. }
  1384. //-----------------------------------------------------------------------------
  1385. void Camera::setAngularVelocity(const VectorF& vel)
  1386. {
  1387. mAngularVelocity = vel;
  1388. setMaskBits(MoveMask);
  1389. }
  1390. //-----------------------------------------------------------------------------
  1391. void Camera::setEditOrbitMode()
  1392. {
  1393. mMode = EditOrbitMode;
  1394. if (bool(mOrbitObject))
  1395. {
  1396. clearProcessAfter();
  1397. clearNotify(mOrbitObject);
  1398. }
  1399. mOrbitObject = NULL;
  1400. // If there is a valid orbit point, then point to it
  1401. // rather than move the camera.
  1402. if(mValidEditOrbitPoint)
  1403. {
  1404. Point3F currentPos;
  1405. mObjToWorld.getColumn(3,&currentPos);
  1406. Point3F dir = mEditOrbitPoint - currentPos;
  1407. mCurrentEditOrbitDist = dir.len();
  1408. dir.normalize();
  1409. F32 yaw, pitch;
  1410. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1411. mRot.x = -pitch;
  1412. mRot.z = yaw;
  1413. }
  1414. }
  1415. //-----------------------------------------------------------------------------
  1416. void Camera::_calcOrbitPoint( MatrixF* mat, const Point3F& rot )
  1417. {
  1418. Point3F pos;
  1419. pos.x = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mCos( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.x + mOffset.x;
  1420. pos.y = mCurOrbitDist * mSin( rot.x + mDegToRad( 90.0f ) ) * mSin( -1.0f * ( rot.z + mDegToRad( 90.0f ) ) ) + mPosition.y + mOffset.y;
  1421. pos.z = mCurOrbitDist * mSin( rot.x ) + mPosition.z + mOffset.z;
  1422. mat->setColumn( 3, pos );
  1423. }
  1424. //-----------------------------------------------------------------------------
  1425. void Camera::_calcEditOrbitPoint( MatrixF *mat, const Point3F& rot )
  1426. {
  1427. //Point3F dir;
  1428. //mat->getColumn(1, &dir);
  1429. //Point3F startPos = getRenderPosition();
  1430. //Point3F endPos = startPos - dir * mCurrentEditOrbitDist;
  1431. Point3F pos;
  1432. pos.x = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mCos(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.x;
  1433. pos.y = mCurrentEditOrbitDist * mSin(rot.x + mDegToRad(90.0f)) * mSin(-1.0f * (rot.z + mDegToRad(90.0f))) + mEditOrbitPoint.y;
  1434. pos.z = mCurrentEditOrbitDist * mSin(rot.x) + mEditOrbitPoint.z;
  1435. mat->setColumn(3, pos);
  1436. }
  1437. //-----------------------------------------------------------------------------
  1438. void Camera::setValidEditOrbitPoint( bool state )
  1439. {
  1440. mValidEditOrbitPoint = state;
  1441. setMaskBits(EditOrbitMask);
  1442. }
  1443. //-----------------------------------------------------------------------------
  1444. Point3F Camera::getEditOrbitPoint() const
  1445. {
  1446. return mEditOrbitPoint;
  1447. }
  1448. //-----------------------------------------------------------------------------
  1449. void Camera::setEditOrbitPoint( const Point3F& pnt )
  1450. {
  1451. // Change the point that we orbit in EditOrbitMode.
  1452. // We'll also change the facing and distance of the
  1453. // camera so that it doesn't jump around.
  1454. Point3F currentPos;
  1455. mObjToWorld.getColumn(3,&currentPos);
  1456. Point3F dir = pnt - currentPos;
  1457. mCurrentEditOrbitDist = dir.len();
  1458. if(mMode == EditOrbitMode)
  1459. {
  1460. dir.normalize();
  1461. F32 yaw, pitch;
  1462. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1463. mRot.x = -pitch;
  1464. mRot.z = yaw;
  1465. }
  1466. mEditOrbitPoint = pnt;
  1467. setMaskBits(EditOrbitMask);
  1468. }
  1469. //----------------------------------------------------------------------------
  1470. void Camera::lookAt( const Point3F& pos )
  1471. {
  1472. Point3F vec;
  1473. mObjToWorld.getColumn(3, &mPosition);
  1474. vec = pos - mPosition;
  1475. vec.normalizeSafe();
  1476. F32 pitch, yaw;
  1477. MathUtils::getAnglesFromVector(vec, yaw, pitch);
  1478. mRot.x = -pitch;
  1479. mRot.z = yaw;
  1480. _setPosition(mPosition, mRot);
  1481. }
  1482. //----------------------------------------------------------------------------
  1483. void Camera::autoFitRadius( F32 radius )
  1484. {
  1485. F32 fov = mDegToRad( getCameraFov() );
  1486. F32 viewradius = (radius * 2.0f) / mTan(fov * 0.5f);
  1487. // Be careful of infinite sized objects. Clip to 16km
  1488. if(viewradius > 16000.0f)
  1489. viewradius = 16000.0f;
  1490. if(mMode == EditOrbitMode && mValidEditOrbitPoint)
  1491. {
  1492. mCurrentEditOrbitDist = viewradius;
  1493. }
  1494. else if(mValidEditOrbitPoint)
  1495. {
  1496. mCurrentEditOrbitDist = viewradius;
  1497. Point3F currentPos;
  1498. mObjToWorld.getColumn(3,&currentPos);
  1499. Point3F dir = mEditOrbitPoint - currentPos;
  1500. dir.normalize();
  1501. F32 yaw, pitch;
  1502. MathUtils::getAnglesFromVector(dir, yaw, pitch);
  1503. mRot.x = -pitch;
  1504. mRot.z = yaw;
  1505. mPosition = mEditOrbitPoint;
  1506. _setPosition(mPosition, mRot);
  1507. _calcEditOrbitPoint(&mObjToWorld, mRot);
  1508. }
  1509. }
  1510. //=============================================================================
  1511. // Console API.
  1512. //=============================================================================
  1513. // MARK: ---- Console API ----
  1514. //-----------------------------------------------------------------------------
  1515. DefineEngineMethod( Camera, getMode, Camera::CameraMotionMode, (), ,
  1516. "Returns the current camera control mode.\n\n"
  1517. "@see CameraMotionMode")
  1518. {
  1519. return object->getMode();
  1520. }
  1521. //-----------------------------------------------------------------------------
  1522. DefineEngineMethod( Camera, getPosition, Point3F, (), ,
  1523. "Get the camera's position in the world.\n\n"
  1524. "@returns The position in the form of \"x y z\".")
  1525. {
  1526. return object->getPosition();
  1527. }
  1528. //-----------------------------------------------------------------------------
  1529. DefineEngineMethod( Camera, getRotation, Point3F, (), ,
  1530. "Get the camera's Euler rotation in radians.\n\n"
  1531. "@returns The rotation in radians in the form of \"x y z\".")
  1532. {
  1533. return object->getRotation();
  1534. }
  1535. //-----------------------------------------------------------------------------
  1536. DefineEngineMethod( Camera, setRotation, void, ( Point3F rot ), ,
  1537. "Set the camera's Euler rotation in radians.\n\n"
  1538. "@param rot The rotation in radians in the form of \"x y z\"."
  1539. "@note Rotation around the Y axis is ignored" )
  1540. {
  1541. return object->setRotation( rot );
  1542. }
  1543. //-----------------------------------------------------------------------------
  1544. DefineEngineMethod( Camera, getOffset, Point3F, (), ,
  1545. "Get the camera's offset from its orbit or tracking point.\n\n"
  1546. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1547. "@returns The offset in the form of \"x y z\".")
  1548. {
  1549. return object->getOffset();
  1550. }
  1551. //-----------------------------------------------------------------------------
  1552. DefineEngineMethod( Camera, setOffset, void, (Point3F offset), ,
  1553. "Set the camera's offset.\n\n"
  1554. "The offset is added to the camera's position when set to CameraMode::OrbitObject.\n"
  1555. "@param offset The distance to offset the camera by in the form of \"x y z\".")
  1556. {
  1557. object->setOffset(offset);
  1558. }
  1559. //-----------------------------------------------------------------------------
  1560. DefineEngineMethod( Camera, setOrbitMode, void, (GameBase* orbitObject, TransformF orbitPoint, F32 minDistance, F32 maxDistance,
  1561. F32 initDistance, bool ownClientObj, Point3F offset, bool locked), (false, Point3F::Zero, false),
  1562. "Set the camera to orbit around the given object, or if none is given, around the given point.\n\n"
  1563. "@param orbitObject The object to orbit around. If no object is given (0 or blank string is passed in) use the orbitPoint instead\n"
  1564. "@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"
  1565. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1566. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1567. "@param initDistance The initial distance from the orbit object or point.\n"
  1568. "@param ownClientObj [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1569. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1570. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1571. "@see Camera::setOrbitObject()\n"
  1572. "@see Camera::setOrbitPoint()\n")
  1573. {
  1574. MatrixF mat;
  1575. orbitPoint.mOrientation.setMatrix(&mat);
  1576. object->setOrbitMode(orbitObject, orbitPoint.mPosition, mat.toEuler(), offset,
  1577. minDistance, maxDistance, initDistance, ownClientObj, locked);
  1578. }
  1579. //-----------------------------------------------------------------------------
  1580. DefineEngineMethod( Camera, setOrbitObject, bool, (GameBase* orbitObject, VectorF rotation, F32 minDistance,
  1581. F32 maxDistance, F32 initDistance, bool ownClientObject, Point3F offset, bool locked),
  1582. (false, Point3F::Zero, false),
  1583. "Set the camera to orbit around a given object.\n\n"
  1584. "@param orbitObject The object to orbit around.\n"
  1585. "@param rotation The initial camera rotation about the object in radians in the form of \"x y z\".\n"
  1586. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1587. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1588. "@param initDistance The initial distance from the orbit object or point.\n"
  1589. "@param ownClientObject [optional] Are we orbiting an object that is owned by us? Default is false.\n"
  1590. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1591. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1592. "@returns false if the given object could not be found.\n"
  1593. "@see Camera::setOrbitMode()\n")
  1594. {
  1595. if( !orbitObject )
  1596. {
  1597. Con::errorf( "Camera::setOrbitObject - Invalid object");
  1598. return false;
  1599. }
  1600. object->setOrbitMode(orbitObject, Point3F(0.0f, 0.0f, 0.0f), rotation, offset,
  1601. minDistance, maxDistance, initDistance, ownClientObject, locked);
  1602. return true;
  1603. }
  1604. //-----------------------------------------------------------------------------
  1605. DefineEngineMethod( Camera, setOrbitPoint, void, (TransformF orbitPoint, F32 minDistance, F32 maxDistance, F32 initDistance,
  1606. Point3F offset, bool locked),
  1607. (Point3F::Zero, false),
  1608. "Set the camera to orbit around a given point.\n\n"
  1609. "@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"
  1610. "@param minDistance The minimum distance allowed to the orbit object or point.\n"
  1611. "@param maxDistance The maximum distance allowed from the orbit object or point.\n"
  1612. "@param initDistance The initial distance from the orbit object or point.\n"
  1613. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1614. "@param locked [optional] Indicates the camera does not receive input from the player. Default is false.\n"
  1615. "@see Camera::setOrbitMode()\n")
  1616. {
  1617. MatrixF mat;
  1618. orbitPoint.mOrientation.setMatrix(&mat);
  1619. object->setOrbitMode(NULL, orbitPoint.mPosition, mat.toEuler(), offset,
  1620. minDistance, maxDistance, initDistance, false, locked);
  1621. }
  1622. //-----------------------------------------------------------------------------
  1623. DefineEngineMethod( Camera, setTrackObject, bool, (GameBase* trackObject, Point3F offset), (Point3F::Zero),
  1624. "Set the camera to track a given object.\n\n"
  1625. "@param trackObject The object to track.\n"
  1626. "@param offset [optional] An offset added to the camera's position. Default is no offset.\n"
  1627. "@returns false if the given object could not be found.\n")
  1628. {
  1629. if(!trackObject)
  1630. {
  1631. Con::warnf("Cannot track non-existing object.");
  1632. return false;
  1633. }
  1634. object->setTrackObject(trackObject, offset);
  1635. return true;
  1636. }
  1637. //-----------------------------------------------------------------------------
  1638. DefineEngineMethod( Camera, setEditOrbitMode, void, (), ,
  1639. "Set the editor camera to orbit around a point set with Camera::setEditOrbitPoint().\n\n"
  1640. "@note This method is generally used only within the World Editor and other tools. To "
  1641. "orbit about an object or point within a game, see Camera::setOrbitMode() and its helper methods.\n")
  1642. {
  1643. object->setEditOrbitMode();
  1644. }
  1645. //-----------------------------------------------------------------------------
  1646. DefineEngineMethod( Camera, setFlyMode, void, (), ,
  1647. "Set the camera to fly freely.\n\n"
  1648. "Allows the camera to have 6 degrees of freedom. Provides for instantaneous motion "
  1649. "and rotation unless one of the Newton fields has been set to true. See Camera::newtonMode "
  1650. "and Camera::newtonRotation.\n")
  1651. {
  1652. object->setFlyMode();
  1653. }
  1654. //-----------------------------------------------------------------------------
  1655. DefineEngineMethod( Camera, setNewtonFlyMode, void, (), ,
  1656. "Set the camera to fly freely, but with ease-in and ease-out.\n\n"
  1657. "This method allows for the same 6 degrees of freedom as Camera::setFlyMode() but "
  1658. "activates the ease-in and ease-out on the camera's movement. To also activate "
  1659. "Newton mode for the camera's rotation, set Camera::newtonRotation to true.")
  1660. {
  1661. object->setNewtonFlyMode();
  1662. }
  1663. //-----------------------------------------------------------------------------
  1664. DefineEngineMethod( Camera, isRotationDamped, bool, (), ,
  1665. "Is this a Newton Fly mode camera with damped rotation?\n\n"
  1666. "@returns true if the camera uses a damped rotation. i.e. Camera::newtonRotation is set to true.\n")
  1667. {
  1668. return object->isRotationDamped();
  1669. }
  1670. //-----------------------------------------------------------------------------
  1671. DefineEngineMethod( Camera, getAngularVelocity, VectorF, (), ,
  1672. "Get the angular velocity for a Newton mode camera.\n\n"
  1673. "@returns The angular velocity in the form of \"x y z\".\n"
  1674. "@note Only returns useful results when Camera::newtonRotation is set to true.")
  1675. {
  1676. return object->getAngularVelocity();
  1677. }
  1678. //-----------------------------------------------------------------------------
  1679. DefineEngineMethod( Camera, setAngularVelocity, void, (VectorF velocity), ,
  1680. "Set the angular velocity for a Newton mode camera.\n\n"
  1681. "@param velocity The angular velocity infor form of \"x y z\".\n"
  1682. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1683. {
  1684. object->setAngularVelocity(velocity);
  1685. }
  1686. //-----------------------------------------------------------------------------
  1687. DefineEngineMethod( Camera, setAngularForce, void, (F32 force), ,
  1688. "Set the angular force for a Newton mode camera.\n\n"
  1689. "@param force The angular force applied when attempting to rotate the camera."
  1690. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1691. {
  1692. object->setAngularForce(force);
  1693. }
  1694. //-----------------------------------------------------------------------------
  1695. DefineEngineMethod( Camera, setAngularDrag, void, (F32 drag), ,
  1696. "Set the angular drag for a Newton mode camera.\n\n"
  1697. "@param drag The angular drag applied while the camera is rotating."
  1698. "@note Only takes affect when Camera::newtonRotation is set to true.")
  1699. {
  1700. object->setAngularDrag(drag);
  1701. }
  1702. //-----------------------------------------------------------------------------
  1703. DefineEngineMethod( Camera, setMass, void, (F32 mass), ,
  1704. "Set the mass for a Newton mode camera.\n\n"
  1705. "@param mass The mass used during ease-in and ease-out calculations."
  1706. "@note Only used when Camera is in Newton mode.")
  1707. {
  1708. object->setMass(mass);
  1709. }
  1710. //-----------------------------------------------------------------------------
  1711. DefineEngineMethod( Camera, getVelocity, VectorF, (), ,
  1712. "Get the velocity for the camera.\n\n"
  1713. "@returns The camera's velocity in the form of \"x y z\"."
  1714. "@note Only useful when the Camera is in Newton mode.")
  1715. {
  1716. return object->getVelocity();
  1717. }
  1718. //-----------------------------------------------------------------------------
  1719. DefineEngineMethod( Camera, setVelocity, void, (VectorF velocity), ,
  1720. "Set the velocity for the camera.\n\n"
  1721. "@param velocity The camera's velocity in the form of \"x y z\"."
  1722. "@note Only affects the Camera when in Newton mode.")
  1723. {
  1724. object->setVelocity(velocity);
  1725. }
  1726. //-----------------------------------------------------------------------------
  1727. DefineEngineMethod( Camera, setDrag, void, (F32 drag), ,
  1728. "Set the drag for a Newton mode camera.\n\n"
  1729. "@param drag The drag applied to the camera while moving."
  1730. "@note Only used when Camera is in Newton mode.")
  1731. {
  1732. object->setDrag(drag);
  1733. }
  1734. //-----------------------------------------------------------------------------
  1735. DefineEngineMethod( Camera, setFlyForce, void, (F32 force), ,
  1736. "Set the force applied to a Newton mode camera while moving.\n\n"
  1737. "@param force The force applied to the camera while attempting to move."
  1738. "@note Only used when Camera is in Newton mode.")
  1739. {
  1740. object->setFlyForce(force);
  1741. }
  1742. //-----------------------------------------------------------------------------
  1743. DefineEngineMethod( Camera, setSpeedMultiplier, void, (F32 multiplier), ,
  1744. "Set the Newton mode camera speed multiplier when trigger[0] is active.\n\n"
  1745. "@param multiplier The speed multiplier to apply."
  1746. "@note Only used when Camera is in Newton mode.")
  1747. {
  1748. object->setSpeedMultiplier(multiplier);
  1749. }
  1750. //-----------------------------------------------------------------------------
  1751. DefineEngineMethod( Camera, setBrakeMultiplier, void, (F32 multiplier), ,
  1752. "Set the Newton mode camera brake multiplier when trigger[1] is active.\n\n"
  1753. "@param multiplier The brake multiplier to apply."
  1754. "@note Only used when Camera is in Newton mode.")
  1755. {
  1756. object->setBrakeMultiplier(multiplier);
  1757. }
  1758. //----------------------------------------------------------------------------
  1759. DefineEngineMethod( Camera, isEditOrbitMode, bool, (), ,
  1760. "Is the camera in edit orbit mode?\n\n"
  1761. "@returns true if the camera is in edit orbit mode.")
  1762. {
  1763. return object->isEditOrbitMode();
  1764. }
  1765. //----------------------------------------------------------------------------
  1766. DefineEngineMethod( Camera, setValidEditOrbitPoint, void, (bool validPoint), ,
  1767. "Set if there is a valid editor camera orbit point.\n"
  1768. "When validPoint is set to false the Camera operates as if it is "
  1769. "in Fly mode rather than an Orbit mode.\n\n"
  1770. "@param validPoint Indicates the validity of the orbit point."
  1771. "@note Only used when Camera is in Edit Orbit Mode.")
  1772. {
  1773. object->setValidEditOrbitPoint(validPoint);
  1774. }
  1775. //----------------------------------------------------------------------------
  1776. DefineEngineMethod( Camera, setEditOrbitPoint, void, (Point3F point), ,
  1777. "Set the editor camera's orbit point.\n\n"
  1778. "@param point The point the camera will orbit in the form of \"x y z\".")
  1779. {
  1780. object->setEditOrbitPoint(point);
  1781. }
  1782. //----------------------------------------------------------------------------
  1783. DefineEngineMethod( Camera, autoFitRadius, void, (F32 radius), ,
  1784. "Move the camera to fully view the given radius.\n\n"
  1785. "@note For this operation to take affect a valid edit orbit point must first be specified. See Camera::setEditOrbitPoint().\n"
  1786. "@param radius The radius to view.")
  1787. {
  1788. object->autoFitRadius(radius);
  1789. }
  1790. //----------------------------------------------------------------------------
  1791. DefineEngineMethod( Camera, lookAt, void, (Point3F point), ,
  1792. "Point the camera at the specified position. Does not work in Orbit or Track modes.\n\n"
  1793. "@param point The position to point the camera at.")
  1794. {
  1795. object->lookAt(point);
  1796. }