afxCamera.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. DefineEngineStringlyVariadicMethod(afxCamera, setOrbitMode, void, 7, 8,
  376. "(GameBase orbitObject, TransformF 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. DefineEngineMethod(afxCamera, setFlyMode, void, (),,
  403. "@brief Set the camera to be able to fly freely.")
  404. {
  405. object->setFlyMode();
  406. }
  407. DefineEngineMethod(afxCamera, getPosition, Point3F, (),,
  408. "@brief Get the position of the camera.\n\n"
  409. "@returns The position of the camera.")
  410. {
  411. return object->getPosition();
  412. }
  413. DefineEngineMethod(afxCamera, setCameraSubject, bool, (SceneObject* subject),, "")
  414. {
  415. if (!subject)
  416. {
  417. Con::errorf("Camera subject not found.");
  418. return false;
  419. }
  420. object->setCameraSubject(subject);
  421. return true;
  422. }
  423. DefineEngineMethod(afxCamera, setThirdPersonDistance, bool, (F32 distance),, "")
  424. {
  425. object->setThirdPersonDistance(distance);
  426. return true;
  427. }
  428. DefineEngineMethod(afxCamera, getThirdPersonDistance, F32, (),, "")
  429. {
  430. return object->getThirdPersonDistance();
  431. }
  432. DefineEngineMethod(afxCamera, setThirdPersonAngle, bool, (F32 distance),, "")
  433. {
  434. object->setThirdPersonAngle(distance);
  435. return true;
  436. }
  437. DefineEngineMethod(afxCamera, getThirdPersonAngle, F32, (),, "")
  438. {
  439. return object->getThirdPersonAngle();
  440. }
  441. DefineEngineMethod(afxCamera, setThirdPersonOffset, void, (Point3F offset, Point3F coi_offset), (Point3F::Max), "")
  442. {
  443. if (coi_offset == Point3F::Max)
  444. {
  445. object->setThirdPersonOffset(offset);
  446. }
  447. else
  448. {
  449. object->setThirdPersonOffset(offset, coi_offset);
  450. }
  451. }
  452. DefineEngineMethod(afxCamera, getThirdPersonOffset, Point3F, (),, "")
  453. {
  454. return object->getThirdPersonOffset();
  455. }
  456. DefineEngineMethod(afxCamera, getThirdPersonCOIOffset, Point3F, (),, "")
  457. {
  458. return object->getThirdPersonCOIOffset();
  459. }
  460. DefineEngineMethod(afxCamera, setThirdPersonMode, void, (),, "")
  461. {
  462. object->setThirdPersonMode();
  463. }
  464. DefineEngineMethod(afxCamera, setThirdPersonSnap, void, (),, "")
  465. {
  466. object->setThirdPersonSnap();
  467. }
  468. DefineEngineMethod(afxCamera, getMode, const char*, (),, "")
  469. {
  470. return object->getMode();
  471. }
  472. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  473. // 3POV SECTION
  474. void afxCamera::cam_update_3pov(F32 dt, bool on_server)
  475. {
  476. Point3F goal_pos;
  477. Point3F curr_pos = getRenderPosition();
  478. MatrixF xfm = cam_subject->getRenderTransform();
  479. Point3F coi = cam_subject->getRenderPosition() + coi_offset;
  480. // for player subjects, pitch is adjusted
  481. Player* player_subj = dynamic_cast<Player*>(cam_subject);
  482. if (player_subj)
  483. {
  484. if (cam_distance > 0.0f)
  485. {
  486. // rotate xfm by amount of cam_angle
  487. F32 look_yaw = player_subj->getHeadRotation().z + mDegToRad(-cam_angle);
  488. MatrixF look_yaw_mtx(EulerF(0,0,look_yaw));
  489. xfm.mul(look_yaw_mtx);
  490. // rotate xfm by amount of head pitch in player
  491. F32 head_pitch = player_subj->getHeadRotation().x;
  492. MatrixF head_pitch_mtx(EulerF(head_pitch,0,0));
  493. xfm.mul(head_pitch_mtx);
  494. VectorF behind_vec(0, -cam_distance, 0);
  495. xfm.mulP(behind_vec, &goal_pos);
  496. goal_pos += cam_offset;
  497. }
  498. else // old backwards-compatible method
  499. {
  500. // rotate xfm by amount of head pitch in player
  501. F32 head_pitch = player_subj->getHeadRotation().x;
  502. MatrixF head_pitch_mtx(EulerF(head_pitch,0,0));
  503. xfm.mul(head_pitch_mtx);
  504. VectorF behind_vec(0, cam_offset.y, 0);
  505. xfm.mulP(behind_vec, &goal_pos);
  506. goal_pos.z += cam_offset.z;
  507. }
  508. }
  509. // for non-player subjects, camera will follow, but pitch won't adjust.
  510. else
  511. {
  512. xfm.mulP(cam_offset, &goal_pos);
  513. }
  514. // avoid view occlusion
  515. if (avoid_blocked_view(coi, goal_pos, goal_pos) && !on_server)
  516. {
  517. // snap to final position if path to goal is blocked
  518. if (test_blocked_line(curr_pos, goal_pos))
  519. third_person_snap_c++;
  520. }
  521. // place camera into its final position
  522. // speed factor values
  523. // 15 -- tight
  524. // 10 -- normal
  525. // 5 -- loose
  526. // 1 -- very loose
  527. F32 speed_factor = 8.0f;
  528. F32 time_inc = 1.0f/speed_factor;
  529. // snap to final position
  530. if (on_server || (third_person_snap_c > 0 || dt > time_inc))
  531. {
  532. snapToPosition(goal_pos);
  533. if (!on_server && third_person_snap_c > 0)
  534. third_person_snap_c--;
  535. return;
  536. }
  537. // interpolate to final position
  538. else
  539. {
  540. // interpretation: always move a proportion of the distance
  541. // from current location to destination that would cover the
  542. // entire distance in time_inc duration at constant velocity.
  543. F32 t = (dt >= time_inc) ? 1.0f : dt*speed_factor;
  544. snapToPosition(goal_pos*t + curr_pos*(1.0-t));
  545. }
  546. }
  547. // See if the camera view is occluded by certain objects,
  548. // and move the camera closer to the subject in that case
  549. bool afxCamera::avoid_blocked_view(const Point3F& startpos, const Point3F& endpos, Point3F& newpos)
  550. {
  551. // cast ray to check for intersection with potential blocker objects
  552. RayInfo hit_info;
  553. if (!getContainer()->castRay(startpos, endpos, afxCameraData::sCameraCollisionMask, &hit_info))
  554. {
  555. // no hit: just return original endpos
  556. newpos = endpos;
  557. return false;
  558. }
  559. // did hit: return the hit location nudged forward slightly
  560. // to avoid seeing clipped portions of blocking object.
  561. Point3F sight_line = startpos - hit_info.point;
  562. sight_line.normalize();
  563. newpos = hit_info.point + sight_line*0.4f;
  564. return true;
  565. }
  566. bool afxCamera::test_blocked_line(const Point3F& startpos, const Point3F& endpos)
  567. {
  568. RayInfo hit_info;
  569. return getContainer()->castRay(startpos, endpos, afxCameraData::sCameraCollisionMask, &hit_info);
  570. }
  571. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  572. // STD OVERRIDES SECTION
  573. bool afxCamera::onAdd()
  574. {
  575. if(!Parent::onAdd())
  576. return false;
  577. mObjBox.maxExtents = mObjScale;
  578. mObjBox.minExtents = mObjScale;
  579. mObjBox.minExtents.neg();
  580. resetWorldBox();
  581. addToScene();
  582. return true;
  583. }
  584. void afxCamera::onRemove()
  585. {
  586. removeFromScene();
  587. Parent::onRemove();
  588. }
  589. void afxCamera::onDeleteNotify(SimObject *obj)
  590. {
  591. Parent::onDeleteNotify(obj);
  592. if (obj == (SimObject*)mOrbitObject)
  593. {
  594. mOrbitObject = NULL;
  595. if (mode == OrbitObjectMode)
  596. mode = OrbitPointMode;
  597. }
  598. if (obj == cam_subject)
  599. {
  600. cam_subject = NULL;
  601. }
  602. }
  603. void afxCamera::advanceTime(F32 dt)
  604. {
  605. Parent::advanceTime(dt);
  606. if (gSFX3DWorld)
  607. {
  608. if (mode == ThirdPersonMode && cam_subject)
  609. {
  610. if (gSFX3DWorld->getListener() != cam_subject)
  611. gSFX3DWorld->setListener(cam_subject);
  612. }
  613. else if (mode == FlyMode)
  614. {
  615. if (gSFX3DWorld->getListener() != this)
  616. gSFX3DWorld->setListener(this);
  617. }
  618. }
  619. cam_update(dt, false);
  620. }
  621. void afxCamera::processTick(const Move* move)
  622. {
  623. Parent::processTick(move);
  624. Point3F vec,pos;
  625. // move will be NULL unless camera becomes the control object as in FlyMode
  626. if (move)
  627. {
  628. // UPDATE ORIENTATION //
  629. delta.rotVec = mRot;
  630. mObjToWorld.getColumn(3, &delta.posVec);
  631. mRot.x = mClampF(mRot.x + move->pitch, -MaxPitch, MaxPitch);
  632. mRot.z += move->yaw;
  633. // ORBIT MODE //
  634. if (mode == OrbitObjectMode || mode == OrbitPointMode)
  635. {
  636. if(mode == OrbitObjectMode && bool(mOrbitObject))
  637. {
  638. // If this is a shapebase, use its render eye transform
  639. // to avoid jittering.
  640. GameBase *castObj = mOrbitObject;
  641. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  642. if( shape != NULL ) {
  643. MatrixF ret;
  644. shape->getRenderEyeTransform( &ret );
  645. mPosition = ret.getPosition();
  646. }
  647. else
  648. {
  649. // Hopefully this is a static object that doesn't move,
  650. // because the worldbox doesn't get updated between ticks.
  651. mOrbitObject->getWorldBox().getCenter(&mPosition);
  652. }
  653. }
  654. set_cam_pos(mPosition, mRot);
  655. validateEyePoint(1.0f, &mObjToWorld);
  656. pos = mPosition;
  657. }
  658. // NON-ORBIT MODE (FLY MODE) //
  659. else // if (mode == FlyMode)
  660. {
  661. // Update pos
  662. bool faster = move->trigger[0] || move->trigger[1];
  663. F32 scale = Camera::getMovementSpeed() * (faster + 1);
  664. mObjToWorld.getColumn(3,&pos);
  665. mObjToWorld.getColumn(0,&vec);
  666. pos += vec * move->x * TickSec * scale;
  667. mObjToWorld.getColumn(1,&vec);
  668. pos += vec * move->y * TickSec * scale;
  669. mObjToWorld.getColumn(2,&vec);
  670. pos += vec * move->z * TickSec * scale;
  671. set_cam_pos(pos,mRot);
  672. }
  673. // If on the client, calc delta for backstepping
  674. if (isClientObject())
  675. {
  676. delta.pos = pos;
  677. delta.rot = mRot;
  678. delta.posVec = delta.posVec - delta.pos;
  679. delta.rotVec = delta.rotVec - delta.rot;
  680. }
  681. else
  682. {
  683. setMaskBits(MoveMask);
  684. }
  685. }
  686. else // if (!move)
  687. {
  688. if (isServerObject())
  689. cam_update(1.0/32.0, true);
  690. }
  691. if (getControllingClient() && mContainer)
  692. updateContainer();
  693. }
  694. void afxCamera::interpolateTick(F32 dt)
  695. {
  696. Parent::interpolateTick(dt);
  697. if (mode == ThirdPersonMode)
  698. return;
  699. Point3F rot = delta.rot + delta.rotVec * dt;
  700. if(mode == OrbitObjectMode || mode == OrbitPointMode)
  701. {
  702. if(mode == OrbitObjectMode && bool(mOrbitObject))
  703. {
  704. // If this is a shapebase, use its render eye transform
  705. // to avoid jittering.
  706. GameBase *castObj = mOrbitObject;
  707. ShapeBase* shape = dynamic_cast<ShapeBase*>(castObj);
  708. if( shape != NULL )
  709. {
  710. MatrixF ret;
  711. shape->getRenderEyeTransform( &ret );
  712. mPosition = ret.getPosition();
  713. }
  714. else
  715. {
  716. // Hopefully this is a static object that doesn't move,
  717. // because the worldbox doesn't get updated between ticks.
  718. mOrbitObject->getWorldBox().getCenter(&mPosition);
  719. }
  720. }
  721. set_cam_pos(mPosition, rot);
  722. validateEyePoint(1.0f, &mObjToWorld);
  723. }
  724. else
  725. {
  726. // NOTE - posVec is 0,0,0 unless cam is control-object and process tick is
  727. // updating the delta
  728. Point3F pos = delta.pos + delta.posVec * dt;
  729. set_cam_pos(pos,rot);
  730. }
  731. }
  732. void afxCamera::writePacketData(GameConnection *connection, BitStream *bstream)
  733. {
  734. // Update client regardless of status flags.
  735. Parent::writePacketData(connection, bstream);
  736. Point3F pos; mObjToWorld.getColumn(3, &pos);
  737. bstream->setCompressionPoint(pos); // SET COMPRESSION POINT
  738. mathWrite(*bstream, pos); // SND POS
  739. bstream->write(mRot.x); // SND X ROT
  740. bstream->write(mRot.z); // SND Z ROT
  741. if (bstream->writeFlag(cam_dirty))
  742. {
  743. mathWrite(*bstream, cam_offset); // SND CAM_OFFSET
  744. mathWrite(*bstream, coi_offset); // SND COI_OFFSET
  745. bstream->write(cam_distance);
  746. bstream->write(cam_angle);
  747. cam_dirty = false;
  748. }
  749. U32 writeMode = mode;
  750. Point3F writePos = mPosition;
  751. S32 gIndex = -1;
  752. if (mode == OrbitObjectMode)
  753. {
  754. gIndex = bool(mOrbitObject) ? connection->getGhostIndex(mOrbitObject): -1;
  755. if(gIndex == -1)
  756. {
  757. writeMode = OrbitPointMode;
  758. mOrbitObject->getWorldBox().getCenter(&writePos);
  759. }
  760. }
  761. bstream->writeRangedU32(writeMode, CameraFirstMode, CameraLastMode); // SND MODE
  762. if (writeMode == ThirdPersonMode)
  763. {
  764. bstream->write(third_person_snap_s > 0); // SND SNAP
  765. if (third_person_snap_s > 0)
  766. third_person_snap_s--;
  767. }
  768. if (writeMode == OrbitObjectMode || writeMode == OrbitPointMode)
  769. {
  770. bstream->write(mMinOrbitDist); // SND ORBIT MIN DIST
  771. bstream->write(mMaxOrbitDist); // SND ORBIT MAX DIST
  772. bstream->write(mCurOrbitDist); // SND ORBIT CURR DIST
  773. if(writeMode == OrbitObjectMode)
  774. {
  775. bstream->writeFlag(mObservingClientObject); // SND OBSERVING CLIENT OBJ
  776. bstream->writeInt(gIndex, NetConnection::GhostIdBitSize); // SND ORBIT OBJ
  777. }
  778. if (writeMode == OrbitPointMode)
  779. bstream->writeCompressedPoint(writePos); // WRITE COMPRESSION POINT
  780. }
  781. }
  782. void afxCamera::readPacketData(GameConnection *connection, BitStream *bstream)
  783. {
  784. Parent::readPacketData(connection, bstream);
  785. Point3F pos,rot;
  786. mathRead(*bstream, &pos); // RCV POS
  787. bstream->setCompressionPoint(pos);
  788. bstream->read(&rot.x); // RCV X ROT
  789. bstream->read(&rot.z); // RCV Z ROT
  790. if (bstream->readFlag())
  791. {
  792. Point3F new_cam_offset, new_coi_offset;
  793. mathRead(*bstream, &new_cam_offset); // RCV CAM_OFFSET
  794. mathRead(*bstream, &new_coi_offset); // RCV COI_OFFSET
  795. bstream->read(&cam_distance);
  796. bstream->read(&cam_angle);
  797. setThirdPersonOffset(new_cam_offset, new_coi_offset);
  798. }
  799. GameBase* obj = 0;
  800. mode = bstream->readRangedU32(CameraFirstMode, // RCV MODE
  801. CameraLastMode);
  802. if (mode == ThirdPersonMode)
  803. {
  804. bool snap; bstream->read(&snap);
  805. if (snap)
  806. third_person_snap_c++;
  807. }
  808. mObservingClientObject = false;
  809. if (mode == OrbitObjectMode || mode == OrbitPointMode) {
  810. bstream->read(&mMinOrbitDist);
  811. bstream->read(&mMaxOrbitDist);
  812. bstream->read(&mCurOrbitDist);
  813. if(mode == OrbitObjectMode)
  814. {
  815. mObservingClientObject = bstream->readFlag();
  816. S32 gIndex = bstream->readInt(NetConnection::GhostIdBitSize);
  817. obj = static_cast<GameBase*>(connection->resolveGhost(gIndex));
  818. }
  819. if (mode == OrbitPointMode)
  820. bstream->readCompressedPoint(&mPosition);
  821. }
  822. if (obj != (GameBase*)mOrbitObject) {
  823. if (mOrbitObject) {
  824. clearProcessAfter();
  825. clearNotify(mOrbitObject);
  826. }
  827. mOrbitObject = obj;
  828. if (mOrbitObject) {
  829. processAfter(mOrbitObject);
  830. deleteNotify(mOrbitObject);
  831. }
  832. }
  833. if (mode == ThirdPersonMode)
  834. return;
  835. set_cam_pos(pos,rot);
  836. delta.pos = pos;
  837. delta.rot = rot;
  838. delta.rotVec.set(0,0,0);
  839. delta.posVec.set(0,0,0);
  840. }
  841. U32 afxCamera::packUpdate(NetConnection* conn, U32 mask, BitStream *bstream)
  842. {
  843. U32 retMask = Parent::packUpdate(conn,mask,bstream);
  844. // The rest of the data is part of the control object packet update.
  845. // If we're controlled by this client, we don't need to send it.
  846. //if(bstream->writeFlag(getControllingClient() == conn && !(mask & InitialUpdateMask)))
  847. // return 0;
  848. if (bstream->writeFlag(mask & MoveMask)) {
  849. Point3F pos;
  850. mObjToWorld.getColumn(3,&pos);
  851. bstream->write(pos.x);
  852. bstream->write(pos.y);
  853. bstream->write(pos.z);
  854. bstream->write(mRot.x);
  855. bstream->write(mRot.z);
  856. }
  857. if (bstream->writeFlag(mask & SubjectMask))
  858. {
  859. S32 ghost_id = (cam_subject) ? conn->getGhostIndex(cam_subject) : -1;
  860. if (bstream->writeFlag(ghost_id != -1))
  861. bstream->writeRangedU32(U32(ghost_id), 0, NetConnection::MaxGhostCount);
  862. else if (cam_subject)
  863. retMask |= SubjectMask;
  864. }
  865. return retMask;
  866. }
  867. void afxCamera::unpackUpdate(NetConnection *conn, BitStream *bstream)
  868. {
  869. Parent::unpackUpdate(conn,bstream);
  870. // controlled by the client?
  871. //if(bstream->readFlag())
  872. // return;
  873. if (bstream->readFlag()) {
  874. Point3F pos,rot;
  875. bstream->read(&pos.x);
  876. bstream->read(&pos.y);
  877. bstream->read(&pos.z);
  878. bstream->read(&rot.x);
  879. bstream->read(&rot.z);
  880. set_cam_pos(pos,rot);
  881. // New delta for client side interpolation
  882. delta.pos = pos;
  883. delta.rot = rot;
  884. delta.posVec = delta.rotVec = VectorF(0,0,0);
  885. }
  886. if (bstream->readFlag())
  887. {
  888. if (bstream->readFlag())
  889. {
  890. S32 ghost_id = bstream->readRangedU32(0, NetConnection::MaxGhostCount);
  891. cam_subject = dynamic_cast<GameBase*>(conn->resolveGhost(ghost_id));
  892. }
  893. else
  894. cam_subject = NULL;
  895. }
  896. }
  897. // Override to ensure both are kept in scope
  898. void afxCamera::onCameraScopeQuery(NetConnection* conn, CameraScopeQuery* query)
  899. {
  900. if (cam_subject)
  901. conn->objectInScope(cam_subject);
  902. Parent::onCameraScopeQuery(conn, query);
  903. }
  904. //----------------------------------------------------------------------------
  905. // check if the object needs to be observed through its own camera...
  906. void afxCamera::getCameraTransform(F32* pos, MatrixF* mat)
  907. {
  908. // The camera doesn't support a third person mode,
  909. // so we want to override the default ShapeBase behavior.
  910. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  911. if (obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  912. obj->getCameraTransform(pos, mat);
  913. else
  914. getEyeTransform(mat);
  915. }
  916. void afxCamera::setTransform(const MatrixF& mat)
  917. {
  918. // This method should never be called on the client.
  919. // This currently converts all rotation in the mat into
  920. // rotations around the z and x axis.
  921. Point3F pos,vec;
  922. mat.getColumn(1,&vec);
  923. mat.getColumn(3,&pos);
  924. Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)),0,-mAtan2(-vec.x,vec.y));
  925. set_cam_pos(pos,rot);
  926. }
  927. void afxCamera::onEditorEnable()
  928. {
  929. mNetFlags.set(Ghostable);
  930. }
  931. void afxCamera::onEditorDisable()
  932. {
  933. mNetFlags.clear(Ghostable);
  934. }
  935. F32 afxCamera::getCameraFov()
  936. {
  937. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  938. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  939. return(obj->getCameraFov());
  940. else
  941. return(Parent::getCameraFov());
  942. }
  943. F32 afxCamera::getDefaultCameraFov()
  944. {
  945. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  946. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  947. return(obj->getDefaultCameraFov());
  948. else
  949. return(Parent::getDefaultCameraFov());
  950. }
  951. bool afxCamera::isValidCameraFov(F32 fov)
  952. {
  953. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  954. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  955. return(obj->isValidCameraFov(fov));
  956. else
  957. return(Parent::isValidCameraFov(fov));
  958. }
  959. void afxCamera::setCameraFov(F32 fov)
  960. {
  961. ShapeBase * obj = dynamic_cast<ShapeBase*>(static_cast<SimObject*>(mOrbitObject));
  962. if(obj && static_cast<ShapeBaseData*>(obj->getDataBlock())->observeThroughObject)
  963. obj->setCameraFov(fov);
  964. else
  965. Parent::setCameraFov(fov);
  966. }
  967. F32 afxCamera::getDamageFlash() const
  968. {
  969. if (mode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  970. {
  971. const GameBase *castObj = mOrbitObject;
  972. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  973. if (psb)
  974. return psb->getDamageFlash();
  975. }
  976. return mDamageFlash;
  977. }
  978. F32 afxCamera::getWhiteOut() const
  979. {
  980. if (mode == OrbitObjectMode && isServerObject() && bool(mOrbitObject))
  981. {
  982. const GameBase *castObj = mOrbitObject;
  983. const ShapeBase* psb = dynamic_cast<const ShapeBase*>(castObj);
  984. if (psb)
  985. return psb->getWhiteOut();
  986. }
  987. return mWhiteOut;
  988. }
  989. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  990. void afxCamera::setControllingClient( GameConnection* client )
  991. {
  992. GameBase::setControllingClient( client );
  993. }
  994. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//