afxCamera.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //
  26. // afxCamera implements a modified camera for demonstrating a third person camera style
  27. // which is more common to RPG games than the standard FPS style camera. For the most part,
  28. // it is a hybrid of the standard TGE camera and the third person mode of the Advanced Camera
  29. // resource, authored by Thomas "Man of Ice" Lund. This camera implements the bare minimum
  30. // required for demonstrating an RPG style camera and leaves tons of room for improvement.
  31. // It should be replaced with a better camera if possible.
  32. //
  33. // Advanced Camera Resource by Thomas "Man of Ice" Lund:
  34. // http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5471
  35. //
  36. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  37. #include "afx/arcaneFX.h"
  38. #include "math/mathUtils.h"
  39. #include "math/mathIO.h"
  40. #include "T3D/gameBase/gameConnection.h"
  41. #include "T3D/camera.h"
  42. #include "T3D/player.h"
  43. #include "T3D/sfx/sfx3DWorld.h"
  44. #include "afx/afxCamera.h"
  45. #define MaxPitch 1.3962f
  46. #define CameraRadius 0.05f;
  47. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  48. // afxCameraData
  49. IMPLEMENT_CO_DATABLOCK_V1(afxCameraData);
  50. ConsoleDocClass( afxCameraData,
  51. "@brief A datablock that describes an afxCamera.\n\n"
  52. "@ingroup afxMisc\n"
  53. "@ingroup AFX\n"
  54. "@ingroup Datablocks\n"
  55. );
  56. U32 afxCameraData::sCameraCollisionMask = TerrainObjectType | InteriorLikeObjectType | TerrainLikeObjectType;
  57. void afxCameraData::initPersistFields()
  58. {
  59. Con::addVariable("pref::afxCamera::collisionMask", TypeS32, &sCameraCollisionMask);
  60. Parent::initPersistFields();
  61. }
  62. void afxCameraData::packData(BitStream* stream)
  63. {
  64. Parent::packData(stream);
  65. }
  66. void afxCameraData::unpackData(BitStream* stream)
  67. {
  68. Parent::unpackData(stream);
  69. }
  70. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  71. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  72. // afxCamera
  73. IMPLEMENT_CO_NETOBJECT_V1(afxCamera);
  74. ConsoleDocClass( afxCamera,
  75. "@brief A 3rd person camera object.\n\n"
  76. "@ingroup afxMisc\n"
  77. "@ingroup AFX\n"
  78. );
  79. afxCamera::afxCamera()
  80. {
  81. mNetFlags.clear(Ghostable);
  82. mTypeMask |= CameraObjectType;
  83. delta.pos = Point3F(0,0,100);
  84. delta.rot = Point3F(0,0,0);
  85. delta.posVec = delta.rotVec = VectorF(0,0,0);
  86. mObjToWorld.setColumn(3,delta.pos);
  87. mRot = delta.rot;
  88. mMinOrbitDist = 0;
  89. mMaxOrbitDist = 0;
  90. mCurOrbitDist = 0;
  91. mOrbitObject = NULL;
  92. mPosition.set(0.f, 0.f, 0.f);
  93. mObservingClientObject = false;
  94. mode = FlyMode;
  95. cam_subject = NULL;
  96. coi_offset.set(0, 0, 2);
  97. cam_offset.set(0, 0, 0);
  98. cam_distance = 0.0f;
  99. cam_angle = 0.0f;
  100. cam_dirty = false;
  101. flymode_saved = false;
  102. third_person_snap_s = 1;
  103. third_person_snap_c = 1;
  104. flymode_saved_pos.zero();
  105. mDamageState = Disabled;
  106. }
  107. afxCamera::~afxCamera()
  108. {
  109. }
  110. //----------------------------------------------------------------------------
  111. void afxCamera::cam_update(F32 dt, bool on_server)
  112. {
  113. if (mode == ThirdPersonMode && cam_subject)
  114. cam_update_3pov(dt, on_server);
  115. }
  116. void afxCamera::set_cam_pos(const Point3F& pos,const Point3F& rot)
  117. {
  118. MatrixF xRot, zRot;
  119. xRot.set(EulerF(rot.x, 0, 0));
  120. zRot.set(EulerF(0, 0, rot.z));
  121. MatrixF temp;
  122. temp.mul(zRot, xRot);
  123. temp.setColumn(3, pos);
  124. Parent::setTransform(temp);
  125. mRot = rot;
  126. }
  127. //----------------------------------------------------------------------------
  128. //----------------------------------------------------------------------------
  129. Point3F &afxCamera::getPosition()
  130. {
  131. static Point3F position;
  132. mObjToWorld.getColumn(3, &position);
  133. return position;
  134. }
  135. //----------------------------------------------------------------------------
  136. //----------------------------------------------------------------------------
  137. //----------------------------------------------------------------------------
  138. // NEW Observer Code
  139. //----------------------------------------------------------------------------
  140. //----------------------------------------------------------------------------
  141. void afxCamera::setFlyMode()
  142. {
  143. mode = FlyMode;
  144. if (flymode_saved)
  145. snapToPosition(flymode_saved_pos);
  146. if (bool(mOrbitObject))
  147. {
  148. clearProcessAfter();
  149. clearNotify(mOrbitObject);
  150. }
  151. mOrbitObject = NULL;
  152. }
  153. void afxCamera::setOrbitMode(GameBase *obj, Point3F &pos, AngAxisF &rot, F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject)
  154. {
  155. mObservingClientObject = ownClientObject;
  156. if(bool(mOrbitObject)) {
  157. clearProcessAfter();
  158. clearNotify(mOrbitObject);
  159. }
  160. mOrbitObject = obj;
  161. if(bool(mOrbitObject))
  162. {
  163. processAfter(mOrbitObject);
  164. deleteNotify(mOrbitObject);
  165. mOrbitObject->getWorldBox().getCenter(&mPosition);
  166. mode = OrbitObjectMode;
  167. }
  168. else
  169. {
  170. mode = OrbitPointMode;
  171. mPosition = pos;
  172. }
  173. QuatF q(rot);
  174. MatrixF tempMat(true);
  175. q.setMatrix(&tempMat);
  176. Point3F dir;
  177. tempMat.getColumn(1, &dir);
  178. set_cam_pos(mPosition, dir);
  179. mMinOrbitDist = minDist;
  180. mMaxOrbitDist = maxDist;
  181. mCurOrbitDist = curDist;
  182. }
  183. void afxCamera::validateEyePoint(F32 pos, MatrixF *mat)
  184. {
  185. if (pos != 0) {
  186. // Use the eye transform to orient the camera
  187. Point3F dir;
  188. mat->getColumn(1, &dir);
  189. pos *= mMaxOrbitDist - mMinOrbitDist;
  190. // Use the camera node's pos.
  191. Point3F startPos;
  192. Point3F endPos;
  193. mObjToWorld.getColumn(3,&startPos);
  194. // Make sure we don't extend the camera into anything solid
  195. if(mOrbitObject)
  196. mOrbitObject->disableCollision();
  197. disableCollision();
  198. RayInfo collision;
  199. SceneContainer* pContainer = isServerObject() ? &gServerContainer : &gClientContainer;
  200. if (!pContainer->castRay(startPos, startPos - dir * 2.5 * pos, afxCameraData::sCameraCollisionMask, &collision))
  201. endPos = startPos - dir * pos;
  202. else
  203. {
  204. float dot = mDot(dir, collision.normal);
  205. if(dot > 0.01)
  206. {
  207. float colDist = mDot(startPos - collision.point, dir) - (1 / dot) * CameraRadius;
  208. if(colDist > pos)
  209. colDist = pos;
  210. if(colDist < 0)
  211. colDist = 0;
  212. endPos = startPos - dir * colDist;
  213. }
  214. else
  215. endPos = startPos - dir * pos;
  216. }
  217. mat->setColumn(3,endPos);
  218. enableCollision();
  219. if(mOrbitObject)
  220. mOrbitObject->enableCollision();
  221. }
  222. }
  223. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  224. // Sets the position and calculates rotation
  225. void afxCamera::snapToPosition(const Point3F& tPos)
  226. {
  227. MatrixF transMat;
  228. if (cam_subject)
  229. {
  230. // get the subject's transform
  231. MatrixF objToWorld = cam_subject->getRenderTransform();
  232. // transform the center-of-interest to world-space
  233. Point3F objPos;
  234. objToWorld.mulP(coi_offset, &objPos);
  235. // find normalized direction vector looking from camera to coi
  236. VectorF dirVec = objPos - tPos;
  237. dirVec.normalize();
  238. MathUtils::getAnglesFromVector(dirVec, mRot.z, mRot.x);
  239. mRot.x = 0 - mRot.x;
  240. transMat = MathUtils::createOrientFromDir(dirVec);
  241. }
  242. else
  243. {
  244. transMat.identity();
  245. }
  246. transMat.setColumn(3, tPos);
  247. Parent::setTransform(transMat);
  248. }
  249. void afxCamera::setCameraSubject(SceneObject* new_subject)
  250. {
  251. // cleanup any existing chase subject
  252. if (cam_subject)
  253. {
  254. if (dynamic_cast<GameBase*>(cam_subject))
  255. clearProcessAfter();
  256. clearNotify(cam_subject);
  257. }
  258. cam_subject = new_subject;
  259. // set associations with new chase subject
  260. if (cam_subject)
  261. {
  262. if (dynamic_cast<GameBase*>(cam_subject))
  263. processAfter((GameBase*)cam_subject);
  264. deleteNotify(cam_subject);
  265. }
  266. mode = (cam_subject) ? ThirdPersonMode : FlyMode;
  267. setMaskBits(SubjectMask);
  268. }
  269. void afxCamera::setThirdPersonOffset(const Point3F& offset)
  270. {
  271. // new method
  272. if (cam_distance > 0.0f)
  273. {
  274. if (isClientObject())
  275. {
  276. GameConnection* conn = GameConnection::getConnectionToServer();
  277. if (conn)
  278. {
  279. // this auto switches to/from first person
  280. if (conn->isFirstPerson())
  281. {
  282. if (cam_distance >= 1.0f)
  283. conn->setFirstPerson(false);
  284. }
  285. else
  286. {
  287. if (cam_distance < 1.0f)
  288. conn->setFirstPerson(true);
  289. }
  290. }
  291. }
  292. cam_offset = offset;
  293. cam_dirty = true;
  294. return;
  295. }
  296. // old backwards-compatible method
  297. if (offset.y != cam_offset.y && isClientObject())
  298. {
  299. GameConnection* conn = GameConnection::getConnectionToServer();
  300. if (conn)
  301. {
  302. // this auto switches to/from first person
  303. if (conn->isFirstPerson())
  304. {
  305. if (offset.y <= -1.0f)
  306. conn->setFirstPerson(false);
  307. }
  308. else
  309. {
  310. if (offset.y > -1.0f)
  311. conn->setFirstPerson(true);
  312. }
  313. }
  314. }
  315. cam_offset = offset;
  316. cam_dirty = true;
  317. }
  318. void afxCamera::setThirdPersonOffset(const Point3F& offset, const Point3F& coi_offset)
  319. {
  320. this->coi_offset = coi_offset;
  321. setThirdPersonOffset(offset);
  322. }
  323. void afxCamera::setThirdPersonDistance(F32 distance)
  324. {
  325. cam_distance = distance;
  326. cam_dirty = true;
  327. }
  328. F32 afxCamera::getThirdPersonDistance()
  329. {
  330. return cam_distance;
  331. }
  332. void afxCamera::setThirdPersonAngle(F32 angle)
  333. {
  334. cam_angle = angle;
  335. cam_dirty = true;
  336. }
  337. F32 afxCamera::getThirdPersonAngle()
  338. {
  339. return cam_angle;
  340. }
  341. void afxCamera::setThirdPersonMode()
  342. {
  343. mode = ThirdPersonMode;
  344. flymode_saved_pos = getPosition();
  345. flymode_saved = true;
  346. cam_dirty = true;
  347. third_person_snap_s++;
  348. }
  349. void afxCamera::setThirdPersonSnap()
  350. {
  351. if (mode == ThirdPersonMode)
  352. third_person_snap_s += 2;
  353. }
  354. void afxCamera::setThirdPersonSnapClient()
  355. {
  356. if (mode == ThirdPersonMode)
  357. third_person_snap_c++;
  358. }
  359. const char* afxCamera::getMode()
  360. {
  361. switch (mode)
  362. {
  363. case ThirdPersonMode:
  364. return "ThirdPerson";
  365. case FlyMode:
  366. return "Fly";
  367. case OrbitObjectMode:
  368. return "Orbit";
  369. }
  370. return "Unknown";
  371. }
  372. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  373. // Console Methods
  374. static char buffer[100];
  375. ConsoleMethod(afxCamera, setOrbitMode, void, 7, 8,
  376. "(GameBase orbitObject, transform mat, float minDistance, float maxDistance, float curDistance, bool ownClientObject)"
  377. "Set the camera to orbit around some given object.\n\n"
  378. "@param orbitObject Object we want to orbit.\n"
  379. "@param mat A set of fields: posX posY posZ aaX aaY aaZ aaTheta\n"
  380. "@param minDistance Minimum distance to keep from object.\n"
  381. "@param maxDistance Maximum distance to keep from object.\n"
  382. "@param curDistance Distance to set initially from object.\n"
  383. "@param ownClientObj Are we observing an object owned by us?")
  384. {
  385. Point3F pos;
  386. AngAxisF aa;
  387. F32 minDis, maxDis, curDis;
  388. GameBase *orbitObject = NULL;
  389. if(Sim::findObject(argv[2],orbitObject) == false)
  390. {
  391. Con::warnf("Cannot orbit non-existing object.");
  392. object->setFlyMode();
  393. return;
  394. }
  395. dSscanf(argv[3],"%f %f %f %f %f %f %f",
  396. &pos.x,&pos.y,&pos.z,&aa.axis.x,&aa.axis.y,&aa.axis.z,&aa.angle);
  397. minDis = dAtof(argv[4]);
  398. maxDis = dAtof(argv[5]);
  399. curDis = dAtof(argv[6]);
  400. object->setOrbitMode(orbitObject, pos, aa, minDis, maxDis, curDis, (argc == 8) ? dAtob(argv[7]) : false);
  401. }
  402. ConsoleMethod( afxCamera, setFlyMode, void, 2, 2, "()" "Set the camera to be able to fly freely.")
  403. {
  404. object->setFlyMode();
  405. }
  406. ConsoleMethod( afxCamera, getPosition, const char *, 2, 2, "()"
  407. "Get the position of the camera.\n\n"
  408. "@returns A string of form \"x y z\".")
  409. {
  410. Point3F& pos = object->getPosition();
  411. dSprintf(buffer, sizeof(buffer),"%f %f %f",pos.x,pos.y,pos.z);
  412. return buffer;
  413. }
  414. ConsoleMethod(afxCamera, setCameraSubject, bool, 3, 3, "")
  415. {
  416. SceneObject* subject;
  417. if (!Sim::findObject(argv[2], subject))
  418. {
  419. Con::errorf("Camera subject \"%s\" not found.", argv[2].getStringValue());
  420. return false;
  421. }
  422. object->setCameraSubject(subject);
  423. return true;
  424. }
  425. ConsoleMethod(afxCamera, setThirdPersonDistance, bool, 3, 3, "")
  426. {
  427. F32 distance;
  428. dSscanf(argv[2], "%f", &distance);
  429. object->setThirdPersonDistance(distance);
  430. return true;
  431. }
  432. ConsoleMethod(afxCamera, getThirdPersonDistance, F32, 2, 2, "")
  433. {
  434. return object->getThirdPersonDistance();
  435. }
  436. ConsoleMethod(afxCamera, setThirdPersonAngle, bool, 3, 3, "")
  437. {
  438. F32 angle;
  439. dSscanf(argv[2], "%f", &angle);
  440. object->setThirdPersonAngle(angle);
  441. return true;
  442. }
  443. ConsoleMethod(afxCamera, getThirdPersonAngle, F32, 2, 2, "")
  444. {
  445. return object->getThirdPersonAngle();
  446. }
  447. ConsoleMethod(afxCamera, setThirdPersonOffset, void, 3, 4, "(Point3F offset [, Point3f coi_offset])")
  448. {
  449. Point3F offset;
  450. dSscanf(argv[2], "%f %f %f", &offset.x, &offset.y, &offset.z);
  451. if (argc > 3)
  452. {
  453. Point3F coi_offset;
  454. dSscanf(argv[3], "%f %f %f", &coi_offset.x, &coi_offset.y, &coi_offset.z);
  455. object->setThirdPersonOffset(offset, coi_offset);
  456. }
  457. else
  458. object->setThirdPersonOffset(offset);
  459. }
  460. ConsoleMethod(afxCamera, getThirdPersonOffset, const char *, 2, 2, "()")
  461. {
  462. const Point3F& pos = object->getThirdPersonOffset();
  463. dSprintf(buffer, sizeof(buffer),"%f %f %f",pos.x,pos.y,pos.z);
  464. return buffer;
  465. }
  466. ConsoleMethod(afxCamera, getThirdPersonCOIOffset, const char *, 2, 2, "()")
  467. {
  468. const Point3F& pos = object->getThirdPersonCOIOffset();
  469. dSprintf(buffer, sizeof(buffer),"%f %f %f",pos.x,pos.y,pos.z);
  470. return buffer;
  471. }
  472. ConsoleMethod(afxCamera, setThirdPersonMode, void, 2, 2, "()")
  473. {
  474. object->setThirdPersonMode();
  475. }
  476. ConsoleMethod(afxCamera, setThirdPersonSnap, void, 2, 2, "()")
  477. {
  478. object->setThirdPersonSnap();
  479. }
  480. ConsoleMethod(afxCamera, getMode, const char *, 2, 2, "()")
  481. {
  482. return object->getMode();
  483. }
  484. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  485. // 3POV SECTION
  486. void afxCamera::cam_update_3pov(F32 dt, bool on_server)
  487. {
  488. Point3F goal_pos;
  489. Point3F curr_pos = getRenderPosition();
  490. MatrixF xfm = cam_subject->getRenderTransform();
  491. Point3F coi = cam_subject->getRenderPosition() + coi_offset;
  492. // for player subjects, pitch is adjusted
  493. Player* player_subj = dynamic_cast<Player*>(cam_subject);
  494. if (player_subj)
  495. {
  496. if (cam_distance > 0.0f)
  497. {
  498. // rotate xfm by amount of cam_angle
  499. F32 look_yaw = player_subj->getHeadRotation().z + mDegToRad(-cam_angle);
  500. MatrixF look_yaw_mtx(EulerF(0,0,look_yaw));
  501. xfm.mul(look_yaw_mtx);
  502. // rotate xfm by amount of head pitch in player
  503. F32 head_pitch = player_subj->getHeadRotation().x;
  504. MatrixF head_pitch_mtx(EulerF(head_pitch,0,0));
  505. xfm.mul(head_pitch_mtx);
  506. VectorF behind_vec(0, -cam_distance, 0);
  507. xfm.mulP(behind_vec, &goal_pos);
  508. goal_pos += cam_offset;
  509. }
  510. else // old backwards-compatible method
  511. {
  512. // rotate xfm by amount of head pitch in player
  513. F32 head_pitch = player_subj->getHeadRotation().x;
  514. MatrixF head_pitch_mtx(EulerF(head_pitch,0,0));
  515. xfm.mul(head_pitch_mtx);
  516. VectorF behind_vec(0, cam_offset.y, 0);
  517. xfm.mulP(behind_vec, &goal_pos);
  518. goal_pos.z += cam_offset.z;
  519. }
  520. }
  521. // for non-player subjects, camera will follow, but pitch won't adjust.
  522. else
  523. {
  524. xfm.mulP(cam_offset, &goal_pos);
  525. }
  526. // avoid view occlusion
  527. if (avoid_blocked_view(coi, goal_pos, goal_pos) && !on_server)
  528. {
  529. // snap to final position if path to goal is blocked
  530. if (test_blocked_line(curr_pos, goal_pos))
  531. third_person_snap_c++;
  532. }
  533. // place camera into its final position
  534. // speed factor values
  535. // 15 -- tight
  536. // 10 -- normal
  537. // 5 -- loose
  538. // 1 -- very loose
  539. F32 speed_factor = 8.0f;
  540. F32 time_inc = 1.0f/speed_factor;
  541. // snap to final position
  542. if (on_server || (third_person_snap_c > 0 || dt > time_inc))
  543. {
  544. snapToPosition(goal_pos);
  545. if (!on_server && third_person_snap_c > 0)
  546. third_person_snap_c--;
  547. return;
  548. }
  549. // interpolate to final position
  550. else
  551. {
  552. // interpretation: always move a proportion of the distance
  553. // from current location to destination that would cover the
  554. // entire distance in time_inc duration at constant velocity.
  555. F32 t = (dt >= time_inc) ? 1.0f : dt*speed_factor;
  556. snapToPosition(goal_pos*t + curr_pos*(1.0-t));
  557. }
  558. }
  559. // See if the camera view is occluded by certain objects,
  560. // and move the camera closer to the subject in that case
  561. bool afxCamera::avoid_blocked_view(const Point3F& startpos, const Point3F& endpos, Point3F& newpos)
  562. {
  563. // cast ray to check for intersection with potential blocker objects
  564. RayInfo hit_info;
  565. if (!getContainer()->castRay(startpos, endpos, afxCameraData::sCameraCollisionMask, &hit_info))
  566. {
  567. // no hit: just return original endpos
  568. newpos = endpos;
  569. return false;
  570. }
  571. // did hit: return the hit location nudged forward slightly
  572. // to avoid seeing clipped portions of blocking object.
  573. Point3F sight_line = startpos - hit_info.point;
  574. sight_line.normalize();
  575. newpos = hit_info.point + sight_line*0.4f;
  576. return true;
  577. }
  578. bool afxCamera::test_blocked_line(const Point3F& startpos, const Point3F& endpos)
  579. {
  580. RayInfo hit_info;
  581. return getContainer()->castRay(startpos, endpos, afxCameraData::sCameraCollisionMask, &hit_info);
  582. }
  583. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  584. // STD OVERRIDES SECTION
  585. bool afxCamera::onAdd()
  586. {
  587. if(!Parent::onAdd())
  588. return false;
  589. mObjBox.maxExtents = mObjScale;
  590. mObjBox.minExtents = mObjScale;
  591. mObjBox.minExtents.neg();
  592. resetWorldBox();
  593. addToScene();
  594. return true;
  595. }
  596. void afxCamera::onRemove()
  597. {
  598. removeFromScene();
  599. Parent::onRemove();
  600. }
  601. void afxCamera::onDeleteNotify(SimObject *obj)
  602. {
  603. Parent::onDeleteNotify(obj);
  604. if (obj == (SimObject*)mOrbitObject)
  605. {
  606. mOrbitObject = NULL;
  607. if (mode == OrbitObjectMode)
  608. mode = OrbitPointMode;
  609. }
  610. if (obj == cam_subject)
  611. {
  612. cam_subject = NULL;
  613. }
  614. }
  615. void afxCamera::advanceTime(F32 dt)
  616. {
  617. Parent::advanceTime(dt);
  618. if (gSFX3DWorld)
  619. {
  620. if (mode == ThirdPersonMode && cam_subject)
  621. {
  622. if (gSFX3DWorld->getListener() != cam_subject)
  623. gSFX3DWorld->setListener(cam_subject);
  624. }
  625. else if (mode == FlyMode)
  626. {
  627. if (gSFX3DWorld->getListener() != this)
  628. gSFX3DWorld->setListener(this);
  629. }
  630. }
  631. cam_update(dt, false);
  632. }
  633. void afxCamera::processTick(const Move* move)
  634. {
  635. Parent::processTick(move);
  636. Point3F vec,pos;
  637. // move will be NULL unless camera becomes the control object as in FlyMode
  638. if (move)
  639. {
  640. // UPDATE ORIENTATION //
  641. delta.rotVec = mRot;
  642. mObjToWorld.getColumn(3, &delta.posVec);
  643. mRot.x = mClampF(mRot.x + move->pitch, -MaxPitch, MaxPitch);
  644. mRot.z += move->yaw;
  645. // ORBIT MODE //
  646. if (mode == OrbitObjectMode || mode == OrbitPointMode)
  647. {
  648. if(mode == OrbitObjectMode && bool(mOrbitObject))
  649. {
  650. // If this is a shapebase, use its render eye transform
  651. // to avoid jittering.
  652. GameBase *castObj = mOrbitObject;
  653. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  654. if( shape != NULL ) {
  655. MatrixF ret;
  656. shape->getRenderEyeTransform( &ret );
  657. mPosition = ret.getPosition();
  658. }
  659. else
  660. {
  661. // Hopefully this is a static object that doesn't move,
  662. // because the worldbox doesn't get updated between ticks.
  663. mOrbitObject->getWorldBox().getCenter(&mPosition);
  664. }
  665. }
  666. set_cam_pos(mPosition, mRot);
  667. validateEyePoint(1.0f, &mObjToWorld);
  668. pos = mPosition;
  669. }
  670. // NON-ORBIT MODE (FLY MODE) //
  671. else // if (mode == FlyMode)
  672. {
  673. // Update pos
  674. bool faster = move->trigger[0] || move->trigger[1];
  675. F32 scale = Camera::getMovementSpeed() * (faster + 1);
  676. mObjToWorld.getColumn(3,&pos);
  677. mObjToWorld.getColumn(0,&vec);
  678. pos += vec * move->x * TickSec * scale;
  679. mObjToWorld.getColumn(1,&vec);
  680. pos += vec * move->y * TickSec * scale;
  681. mObjToWorld.getColumn(2,&vec);
  682. pos += vec * move->z * TickSec * scale;
  683. set_cam_pos(pos,mRot);
  684. }
  685. // If on the client, calc delta for backstepping
  686. if (isClientObject())
  687. {
  688. delta.pos = pos;
  689. delta.rot = mRot;
  690. delta.posVec = delta.posVec - delta.pos;
  691. delta.rotVec = delta.rotVec - delta.rot;
  692. }
  693. else
  694. {
  695. setMaskBits(MoveMask);
  696. }
  697. }
  698. else // if (!move)
  699. {
  700. if (isServerObject())
  701. cam_update(1.0/32.0, true);
  702. }
  703. if (getControllingClient() && mContainer)
  704. updateContainer();
  705. }
  706. void afxCamera::interpolateTick(F32 dt)
  707. {
  708. Parent::interpolateTick(dt);
  709. if (mode == ThirdPersonMode)
  710. return;
  711. Point3F rot = delta.rot + delta.rotVec * dt;
  712. if(mode == OrbitObjectMode || mode == OrbitPointMode)
  713. {
  714. if(mode == OrbitObjectMode && bool(mOrbitObject))
  715. {
  716. // If this is a shapebase, use its render eye transform
  717. // to avoid jittering.
  718. GameBase *castObj = mOrbitObject;
  719. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  720. if( shape != NULL )
  721. {
  722. MatrixF ret;
  723. shape->getRenderEyeTransform( &ret );
  724. mPosition = ret.getPosition();
  725. }
  726. else
  727. {
  728. // Hopefully this is a static object that doesn't move,
  729. // because the worldbox doesn't get updated between ticks.
  730. mOrbitObject->getWorldBox().getCenter(&mPosition);
  731. }
  732. }
  733. set_cam_pos(mPosition, rot);
  734. validateEyePoint(1.0f, &mObjToWorld);
  735. }
  736. else
  737. {
  738. // NOTE - posVec is 0,0,0 unless cam is control-object and process tick is
  739. // updating the delta
  740. Point3F pos = delta.pos + delta.posVec * dt;
  741. set_cam_pos(pos,rot);
  742. }
  743. }
  744. void afxCamera::writePacketData(GameConnection *connection, BitStream *bstream)
  745. {
  746. // Update client regardless of status flags.
  747. Parent::writePacketData(connection, bstream);
  748. Point3F pos; mObjToWorld.getColumn(3, &pos);
  749. bstream->setCompressionPoint(pos); // SET COMPRESSION POINT
  750. mathWrite(*bstream, pos); // SND POS
  751. bstream->write(mRot.x); // SND X ROT
  752. bstream->write(mRot.z); // SND Z ROT
  753. if (bstream->writeFlag(cam_dirty))
  754. {
  755. mathWrite(*bstream, cam_offset); // SND CAM_OFFSET
  756. mathWrite(*bstream, coi_offset); // SND COI_OFFSET
  757. bstream->write(cam_distance);
  758. bstream->write(cam_angle);
  759. cam_dirty = false;
  760. }
  761. U32 writeMode = mode;
  762. Point3F writePos = mPosition;
  763. S32 gIndex = -1;
  764. if (mode == OrbitObjectMode)
  765. {
  766. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  767. if(gIndex == -1)
  768. {
  769. writeMode = OrbitPointMode;
  770. mOrbitObject->getWorldBox().getCenter(&writePos);
  771. }
  772. }
  773. bstream->writeRangedU32(writeMode, CameraFirstMode, CameraLastMode); // SND MODE
  774. if (writeMode == ThirdPersonMode)
  775. {
  776. bstream->write(third_person_snap_s > 0); // SND SNAP
  777. if (third_person_snap_s > 0)
  778. third_person_snap_s--;
  779. }
  780. if (writeMode == OrbitObjectMode || writeMode == OrbitPointMode)
  781. {
  782. bstream->write(mMinOrbitDist); // SND ORBIT MIN DIST
  783. bstream->write(mMaxOrbitDist); // SND ORBIT MAX DIST
  784. bstream->write(mCurOrbitDist); // SND ORBIT CURR DIST
  785. if(writeMode == OrbitObjectMode)
  786. {
  787. bstream->writeFlag(mObservingClientObject); // SND OBSERVING CLIENT OBJ
  788. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize); // SND ORBIT OBJ
  789. }
  790. if (writeMode == OrbitPointMode)
  791. bstream->writeCompressedPoint(writePos); // WRITE COMPRESSION POINT
  792. }
  793. }
  794. void afxCamera::readPacketData(GameConnection *connection, BitStream *bstream)
  795. {
  796. Parent::readPacketData(connection, bstream);
  797. Point3F pos,rot;
  798. mathRead(*bstream, &pos); // RCV POS
  799. bstream->setCompressionPoint(pos);
  800. bstream->read(&rot.x); // RCV X ROT
  801. bstream->read(&rot.z); // RCV Z ROT
  802. if (bstream->readFlag())
  803. {
  804. Point3F new_cam_offset, new_coi_offset;
  805. mathRead(*bstream, &new_cam_offset); // RCV CAM_OFFSET
  806. mathRead(*bstream, &new_coi_offset); // RCV COI_OFFSET
  807. bstream->read(&cam_distance);
  808. bstream->read(&cam_angle);
  809. setThirdPersonOffset(new_cam_offset, new_coi_offset);
  810. }
  811. GameBase* obj = 0;
  812. mode = bstream->readRangedU32(CameraFirstMode, // RCV MODE
  813. CameraLastMode);
  814. if (mode == ThirdPersonMode)
  815. {
  816. bool snap; bstream->read(&snap);
  817. if (snap)
  818. third_person_snap_c++;
  819. }
  820. mObservingClientObject = false;
  821. if (mode == OrbitObjectMode || mode == OrbitPointMode) {
  822. bstream->read(&mMinOrbitDist);
  823. bstream->read(&mMaxOrbitDist);
  824. bstream->read(&mCurOrbitDist);
  825. if(mode == OrbitObjectMode)
  826. {
  827. mObservingClientObject = bstream->readFlag();
  828. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  829. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  830. }
  831. if (mode == OrbitPointMode)
  832. bstream->readCompressedPoint(&mPosition);
  833. }
  834. if (obj != (GameBase*)mOrbitObject) {
  835. if (mOrbitObject) {
  836. clearProcessAfter();
  837. clearNotify(mOrbitObject);
  838. }
  839. mOrbitObject = obj;
  840. if (mOrbitObject) {
  841. processAfter(mOrbitObject);
  842. deleteNotify(mOrbitObject);
  843. }
  844. }
  845. if (mode == ThirdPersonMode)
  846. return;
  847. set_cam_pos(pos,rot);
  848. delta.pos = pos;
  849. delta.rot = rot;
  850. delta.rotVec.set(0,0,0);
  851. delta.posVec.set(0,0,0);
  852. }
  853. U32 afxCamera::packUpdate(NetConnection* conn, U32 mask, BitStream *bstream)
  854. {
  855. U32 retMask = Parent::packUpdate(conn,mask,bstream);
  856. // The rest of the data is part of the control object packet update.
  857. // If we're controlled by this client, we don't need to send it.
  858. //if(bstream->writeFlag(getControllingClient() == conn && !(mask & InitialUpdateMask)))
  859. // return 0;
  860. if (bstream->writeFlag(mask & MoveMask)) {
  861. Point3F pos;
  862. mObjToWorld.getColumn(3,&pos);
  863. bstream->write(pos.x);
  864. bstream->write(pos.y);
  865. bstream->write(pos.z);
  866. bstream->write(mRot.x);
  867. bstream->write(mRot.z);
  868. }
  869. if (bstream->writeFlag(mask & SubjectMask))
  870. {
  871. S32 ghost_id = (cam_subject) ? conn->getGhostIndex(cam_subject) : -1;
  872. if (bstream->writeFlag(ghost_id != -1))
  873. bstream->writeRangedU32(U32(ghost_id), 0, NetConnection::MaxGhostCount);
  874. else if (cam_subject)
  875. retMask |= SubjectMask;
  876. }
  877. return retMask;
  878. }
  879. void afxCamera::unpackUpdate(NetConnection *conn, BitStream *bstream)
  880. {
  881. Parent::unpackUpdate(conn,bstream);
  882. // controlled by the client?
  883. //if(bstream->readFlag())
  884. // return;
  885. if (bstream->readFlag()) {
  886. Point3F pos,rot;
  887. bstream->read(&pos.x);
  888. bstream->read(&pos.y);
  889. bstream->read(&pos.z);
  890. bstream->read(&rot.x);
  891. bstream->read(&rot.z);
  892. set_cam_pos(pos,rot);
  893. // New delta for client side interpolation
  894. delta.pos = pos;
  895. delta.rot = rot;
  896. delta.posVec = delta.rotVec = VectorF(0,0,0);
  897. }
  898. if (bstream->readFlag())
  899. {
  900. if (bstream->readFlag())
  901. {
  902. S32 ghost_id = bstream->readRangedU32(0, NetConnection::MaxGhostCount);
  903. cam_subject = dynamic_cast<GameBase*>(conn->resolveGhost(ghost_id));
  904. }
  905. else
  906. cam_subject = NULL;
  907. }
  908. }
  909. // Override to ensure both are kept in scope
  910. void afxCamera::onCameraScopeQuery(NetConnection* conn, CameraScopeQuery* query)
  911. {
  912. if (cam_subject)
  913. conn->objectInScope(cam_subject);
  914. Parent::onCameraScopeQuery(conn, query);
  915. }
  916. //----------------------------------------------------------------------------
  917. // check if the object needs to be observed through its own camera...
  918. void afxCamera::getCameraTransform(F32* pos, MatrixF* mat)
  919. {
  920. // The camera doesn't support a third person mode,
  921. // so we want to override the default ShapeBase behavior.
  922. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  923. if (obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  924. obj->getCameraTransform(pos, mat);
  925. else
  926. getEyeTransform(mat);
  927. }
  928. void afxCamera::setTransform(const MatrixF& mat)
  929. {
  930. // This method should never be called on the client.
  931. // This currently converts all rotation in the mat into
  932. // rotations around the z and x axis.
  933. Point3F pos,vec;
  934. mat.getColumn(1,&vec);
  935. mat.getColumn(3,&pos);
  936. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)),0,-mAtan2(-vec.x,vec.y));
  937. set_cam_pos(pos,rot);
  938. }
  939. void afxCamera::onEditorEnable()
  940. {
  941. mNetFlags.set(Ghostable);
  942. }
  943. void afxCamera::onEditorDisable()
  944. {
  945. mNetFlags.clear(Ghostable);
  946. }
  947. F32 afxCamera::getCameraFov()
  948. {
  949. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  950. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  951. return(obj->getCameraFov());
  952. else
  953. return(Parent::getCameraFov());
  954. }
  955. F32 afxCamera::getDefaultCameraFov()
  956. {
  957. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  958. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  959. return(obj->getDefaultCameraFov());
  960. else
  961. return(Parent::getDefaultCameraFov());
  962. }
  963. bool afxCamera::isValidCameraFov(F32 fov)
  964. {
  965. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  966. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  967. return(obj->isValidCameraFov(fov));
  968. else
  969. return(Parent::isValidCameraFov(fov));
  970. }
  971. void afxCamera::setCameraFov(F32 fov)
  972. {
  973. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  974. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  975. obj->setCameraFov(fov);
  976. else
  977. Parent::setCameraFov(fov);
  978. }
  979. F32 afxCamera::getDamageFlash() const
  980. {
  981. if (mode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  982. {
  983. const GameBase *castObj = mOrbitObject;
  984. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  985. if (psb)
  986. return psb->getDamageFlash();
  987. }
  988. return mDamageFlash;
  989. }
  990. F32 afxCamera::getWhiteOut() const
  991. {
  992. if (mode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  993. {
  994. const GameBase *castObj = mOrbitObject;
  995. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  996. if (psb)
  997. return psb->getWhiteOut();
  998. }
  999. return mWhiteOut;
  1000. }
  1001. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1002. void afxCamera::setControllingClient( GameConnection* client )
  1003. {
  1004. GameBase::setControllingClient( client );
  1005. }
  1006. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//