flyingVehicle.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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/vehicles/flyingVehicle.h"
  24. #include "app/game.h"
  25. #include "math/mMath.h"
  26. #include "console/simBase.h"
  27. #include "console/console.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/engineAPI.h"
  30. #include "collision/clippedPolyList.h"
  31. #include "collision/planeExtractor.h"
  32. #include "core/stream/bitStream.h"
  33. #include "core/dnet.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "T3D/fx/particleEmitter.h"
  37. #include "sfx/sfxSystem.h"
  38. #include "sfx/sfxProfile.h"
  39. #include "sfx/sfxSource.h"
  40. #include "T3D/missionArea.h"
  41. //----------------------------------------------------------------------------
  42. const static U32 sCollisionMoveMask = ( TerrainObjectType | InteriorObjectType |
  43. WaterObjectType | PlayerObjectType |
  44. StaticShapeObjectType | VehicleObjectType |
  45. VehicleBlockerObjectType );
  46. static U32 sServerCollisionMask = sCollisionMoveMask; // ItemObjectType
  47. static U32 sClientCollisionMask = sCollisionMoveMask;
  48. static F32 sFlyingVehicleGravity = -20.0f;
  49. //
  50. const char* FlyingVehicle::sJetSequence[FlyingVehicle::JetAnimCount] =
  51. {
  52. "activateBack",
  53. "maintainBack",
  54. "activateBot",
  55. "maintainBot",
  56. };
  57. const char* FlyingVehicleData::sJetNode[FlyingVehicleData::MaxJetNodes] =
  58. {
  59. "JetNozzle0", // Thrust Forward
  60. "JetNozzle1",
  61. "JetNozzleX", // Thrust Backward
  62. "JetNozzleY",
  63. "JetNozzle2", // Thrust Downward
  64. "JetNozzle3",
  65. "contrail0", // Trail
  66. "contrail1",
  67. "contrail2",
  68. "contrail3",
  69. };
  70. // Convert thrust direction into nodes & emitters
  71. FlyingVehicle::JetActivation FlyingVehicle::sJetActivation[NumThrustDirections] = {
  72. { FlyingVehicleData::ForwardJetNode, FlyingVehicleData::ForwardJetEmitter },
  73. { FlyingVehicleData::BackwardJetNode, FlyingVehicleData::BackwardJetEmitter },
  74. { FlyingVehicleData::DownwardJetNode, FlyingVehicleData::DownwardJetEmitter },
  75. };
  76. //----------------------------------------------------------------------------
  77. IMPLEMENT_CO_DATABLOCK_V1(FlyingVehicleData);
  78. ConsoleDocClass( FlyingVehicleData,
  79. "@brief Defines the properties of a FlyingVehicle.\n\n"
  80. "@ingroup Vehicles\n"
  81. );
  82. FlyingVehicleData::FlyingVehicleData()
  83. {
  84. maneuveringForce = 0;
  85. horizontalSurfaceForce = 0;
  86. verticalSurfaceForce = 0;
  87. autoInputDamping = 1;
  88. steeringForce = 1;
  89. steeringRollForce = 1;
  90. rollForce = 1;
  91. autoAngularForce = 0;
  92. rotationalDrag = 0;
  93. autoLinearForce = 0;
  94. maxAutoSpeed = 0;
  95. hoverHeight = 2;
  96. createHoverHeight = 2;
  97. maxSteeringAngle = M_PI_F;
  98. minTrailSpeed = 1;
  99. maxSpeed = 100;
  100. for (S32 k = 0; k < MaxJetNodes; k++)
  101. jetNode[k] = -1;
  102. for (S32 j = 0; j < MaxJetEmitters; j++)
  103. jetEmitter[j] = 0;
  104. for (S32 i = 0; i < MaxSounds; i++)
  105. sound[i] = 0;
  106. vertThrustMultiple = 1.0;
  107. }
  108. bool FlyingVehicleData::preload(bool server, String &errorStr)
  109. {
  110. if (!Parent::preload(server, errorStr))
  111. return false;
  112. TSShapeInstance* si = new TSShapeInstance(mShape, false);
  113. // Resolve objects transmitted from server
  114. if (!server) {
  115. for (S32 i = 0; i < MaxSounds; i++)
  116. if (sound[i])
  117. Sim::findObject(SimObjectId(sound[i]),sound[i]);
  118. for (S32 j = 0; j < MaxJetEmitters; j++)
  119. if (jetEmitter[j])
  120. Sim::findObject(SimObjectId(jetEmitter[j]),jetEmitter[j]);
  121. }
  122. // Extract collision planes from shape collision detail level
  123. if (collisionDetails[0] != -1)
  124. {
  125. MatrixF imat(1);
  126. PlaneExtractorPolyList polyList;
  127. polyList.mPlaneList = &rigidBody.mPlaneList;
  128. polyList.setTransform(&imat, Point3F(1,1,1));
  129. si->animate(collisionDetails[0]);
  130. si->buildPolyList(&polyList,collisionDetails[0]);
  131. }
  132. // Resolve jet nodes
  133. for (S32 j = 0; j < MaxJetNodes; j++)
  134. jetNode[j] = mShape->findNode(sJetNode[j]);
  135. //
  136. maxSpeed = maneuveringForce / minDrag;
  137. delete si;
  138. return true;
  139. }
  140. void FlyingVehicleData::initPersistFields()
  141. {
  142. addField( "jetSound", TYPEID< SFXProfile >(), Offset(sound[JetSound], FlyingVehicleData),
  143. "Looping sound to play while the vehicle is jetting." );
  144. addField( "engineSound", TYPEID< SFXProfile >(), Offset(sound[EngineSound], FlyingVehicleData),
  145. "Looping engine sound." );
  146. addField( "maneuveringForce", TypeF32, Offset(maneuveringForce, FlyingVehicleData),
  147. "@brief Maximum X and Y (horizontal plane) maneuvering force.\n\n"
  148. "The actual force applied depends on the current thrust." );
  149. addField( "horizontalSurfaceForce", TypeF32, Offset(horizontalSurfaceForce, FlyingVehicleData),
  150. "@brief Damping force in the opposite direction to sideways velocity.\n\n"
  151. "Provides \"bite\" into the wind for climbing/diving and turning)." );
  152. addField( "verticalSurfaceForce", TypeF32, Offset(verticalSurfaceForce, FlyingVehicleData),
  153. "@brief Damping force in the opposite direction to vertical velocity.\n\n"
  154. "Controls side slip; lower numbers give more slide." );
  155. addField( "vertThrustMultiple", TypeF32, Offset(vertThrustMultiple, FlyingVehicleData),
  156. "Multiplier applied to the jetForce (defined in VehicleData) when thrusting vertically." );
  157. addField( "steeringForce", TypeF32, Offset(steeringForce, FlyingVehicleData),
  158. "@brief Maximum X and Z (sideways and vertical) steering force.\n\n"
  159. "The actual force applied depends on the current steering input." );
  160. addField( "steeringRollForce", TypeF32, Offset(steeringRollForce, FlyingVehicleData),
  161. "Roll force induced by sideways steering input value (controls how much "
  162. "the vehicle rolls when turning)." );
  163. addField( "rollForce", TypeF32, Offset(rollForce, FlyingVehicleData),
  164. "@brief Damping torque against rolling maneuvers (rotation about the y-axis), "
  165. "proportional to linear velocity.\n\n"
  166. "Acts to adjust roll to a stable position over time as the vehicle moves." );
  167. addField( "rotationalDrag", TypeF32, Offset(rotationalDrag, FlyingVehicleData),
  168. "Rotational drag factor (slows vehicle rotation speed in all axes)." );
  169. addField( "maxAutoSpeed", TypeF32, Offset(maxAutoSpeed, FlyingVehicleData),
  170. "Maximum speed for automatic vehicle control assistance - vehicles "
  171. "travelling at speeds above this value do not get control assitance." );
  172. addField( "autoInputDamping", TypeF32, Offset(autoInputDamping, FlyingVehicleData),
  173. "@brief Scale factor applied to steering input if speed is less than "
  174. "maxAutoSpeed to.improve handling at very low speeds.\n\n"
  175. "Smaller values make steering less sensitive." );
  176. addField( "autoLinearForce", TypeF32, Offset(autoLinearForce, FlyingVehicleData),
  177. "@brief Corrective force applied to slow the vehicle when moving at less than "
  178. "maxAutoSpeed.\n\n"
  179. "The force is inversely proportional to vehicle speed." );
  180. addField( "autoAngularForce", TypeF32, Offset(autoAngularForce, FlyingVehicleData),
  181. "@brief Corrective torque applied to level out the vehicle when moving at less "
  182. "than maxAutoSpeed.\n\n"
  183. "The torque is inversely proportional to vehicle speed." );
  184. addField( "hoverHeight", TypeF32, Offset(hoverHeight, FlyingVehicleData),
  185. "The vehicle's height off the ground when at rest." );
  186. addField( "createHoverHeight", TypeF32, Offset(createHoverHeight, FlyingVehicleData),
  187. "@brief The vehicle's height off the ground when useCreateHeight is active.\n\n"
  188. "This can help avoid problems with spawning the vehicle." );
  189. addField( "forwardJetEmitter",TYPEID< ParticleEmitterData >(), Offset(jetEmitter[ForwardJetEmitter], FlyingVehicleData),
  190. "@brief Emitter to generate particles for forward jet thrust.\n\n"
  191. "Forward jet thrust particles are emitted from model nodes JetNozzle0 "
  192. "and JetNozzle1." );
  193. addField( "backwardJetEmitter",TYPEID< ParticleEmitterData >(), Offset(jetEmitter[BackwardJetEmitter], FlyingVehicleData),
  194. "@brief Emitter to generate particles for backward jet thrust.\n\n"
  195. "Backward jet thrust particles are emitted from model nodes JetNozzleX "
  196. "and JetNozzleY." );
  197. addField( "downJetEmitter",TYPEID< ParticleEmitterData >(), Offset(jetEmitter[DownwardJetEmitter], FlyingVehicleData),
  198. "@brief Emitter to generate particles for downward jet thrust.\n\n"
  199. "Downward jet thrust particles are emitted from model nodes JetNozzle2 "
  200. "and JetNozzle3." );
  201. addField( "trailEmitter",TYPEID< ParticleEmitterData >(), Offset(jetEmitter[TrailEmitter], FlyingVehicleData),
  202. "Emitter to generate contrail particles from model nodes contrail0 - contrail3." );
  203. addField( "minTrailSpeed", TypeF32, Offset(minTrailSpeed, FlyingVehicleData),
  204. "Minimum speed at which to start generating contrail particles." );
  205. Parent::initPersistFields();
  206. }
  207. void FlyingVehicleData::packData(BitStream* stream)
  208. {
  209. Parent::packData(stream);
  210. for (S32 i = 0; i < MaxSounds; i++)
  211. {
  212. if (stream->writeFlag(sound[i]))
  213. {
  214. SimObjectId writtenId = packed ? SimObjectId(sound[i]) : sound[i]->getId();
  215. stream->writeRangedU32(writtenId, DataBlockObjectIdFirst, DataBlockObjectIdLast);
  216. }
  217. }
  218. for (S32 j = 0; j < MaxJetEmitters; j++)
  219. {
  220. if (stream->writeFlag(jetEmitter[j]))
  221. {
  222. SimObjectId writtenId = packed ? SimObjectId(jetEmitter[j]) : jetEmitter[j]->getId();
  223. stream->writeRangedU32(writtenId, DataBlockObjectIdFirst,DataBlockObjectIdLast);
  224. }
  225. }
  226. stream->write(maneuveringForce);
  227. stream->write(horizontalSurfaceForce);
  228. stream->write(verticalSurfaceForce);
  229. stream->write(autoInputDamping);
  230. stream->write(steeringForce);
  231. stream->write(steeringRollForce);
  232. stream->write(rollForce);
  233. stream->write(autoAngularForce);
  234. stream->write(rotationalDrag);
  235. stream->write(autoLinearForce);
  236. stream->write(maxAutoSpeed);
  237. stream->write(hoverHeight);
  238. stream->write(createHoverHeight);
  239. stream->write(minTrailSpeed);
  240. stream->write(vertThrustMultiple);
  241. }
  242. void FlyingVehicleData::unpackData(BitStream* stream)
  243. {
  244. Parent::unpackData(stream);
  245. for (S32 i = 0; i < MaxSounds; i++) {
  246. sound[i] = NULL;
  247. if (stream->readFlag())
  248. sound[i] = (SFXProfile*)stream->readRangedU32(DataBlockObjectIdFirst,
  249. DataBlockObjectIdLast);
  250. }
  251. for (S32 j = 0; j < MaxJetEmitters; j++) {
  252. jetEmitter[j] = NULL;
  253. if (stream->readFlag())
  254. jetEmitter[j] = (ParticleEmitterData*)stream->readRangedU32(DataBlockObjectIdFirst,
  255. DataBlockObjectIdLast);
  256. }
  257. stream->read(&maneuveringForce);
  258. stream->read(&horizontalSurfaceForce);
  259. stream->read(&verticalSurfaceForce);
  260. stream->read(&autoInputDamping);
  261. stream->read(&steeringForce);
  262. stream->read(&steeringRollForce);
  263. stream->read(&rollForce);
  264. stream->read(&autoAngularForce);
  265. stream->read(&rotationalDrag);
  266. stream->read(&autoLinearForce);
  267. stream->read(&maxAutoSpeed);
  268. stream->read(&hoverHeight);
  269. stream->read(&createHoverHeight);
  270. stream->read(&minTrailSpeed);
  271. stream->read(&vertThrustMultiple);
  272. }
  273. //----------------------------------------------------------------------------
  274. IMPLEMENT_CO_NETOBJECT_V1(FlyingVehicle);
  275. ConsoleDocClass( FlyingVehicle,
  276. "@brief A flying vehicle.\n\n"
  277. "@ingroup Vehicles\n"
  278. );
  279. FlyingVehicle::FlyingVehicle()
  280. {
  281. mSteering.set(0,0);
  282. mThrottle = 0;
  283. mJetting = false;
  284. mJetSound = 0;
  285. mEngineSound = 0;
  286. mBackMaintainOn = false;
  287. mBottomMaintainOn = false;
  288. createHeightOn = false;
  289. for (S32 i = 0; i < JetAnimCount; i++)
  290. mJetThread[i] = 0;
  291. }
  292. FlyingVehicle::~FlyingVehicle()
  293. {
  294. }
  295. //----------------------------------------------------------------------------
  296. bool FlyingVehicle::onAdd()
  297. {
  298. if(!Parent::onAdd())
  299. return false;
  300. addToScene();
  301. if (isServerObject())
  302. scriptOnAdd();
  303. return true;
  304. }
  305. bool FlyingVehicle::onNewDataBlock(GameBaseData* dptr, bool reload)
  306. {
  307. mDataBlock = dynamic_cast<FlyingVehicleData*>(dptr);
  308. if (!mDataBlock || !Parent::onNewDataBlock(dptr,reload))
  309. return false;
  310. // Sounds
  311. if ( isGhost() )
  312. {
  313. // Create the sounds ahead of time. This reduces runtime
  314. // costs and makes the system easier to understand.
  315. SFX_DELETE( mJetSound );
  316. SFX_DELETE( mEngineSound );
  317. if ( mDataBlock->sound[FlyingVehicleData::EngineSound] )
  318. mEngineSound = SFX->createSource( mDataBlock->sound[FlyingVehicleData::EngineSound], &getTransform() );
  319. if ( mDataBlock->sound[FlyingVehicleData::JetSound] )
  320. mJetSound = SFX->createSource( mDataBlock->sound[FlyingVehicleData::JetSound], &getTransform() );
  321. }
  322. // Jet Sequences
  323. for (S32 i = 0; i < JetAnimCount; i++) {
  324. TSShape const* shape = mShapeInstance->getShape();
  325. mJetSeq[i] = shape->findSequence(sJetSequence[i]);
  326. if (mJetSeq[i] != -1) {
  327. if (i == BackActivate || i == BottomActivate) {
  328. mJetThread[i] = mShapeInstance->addThread();
  329. mShapeInstance->setSequence(mJetThread[i],mJetSeq[i],0);
  330. mShapeInstance->setTimeScale(mJetThread[i],0);
  331. }
  332. }
  333. else
  334. mJetThread[i] = 0;
  335. }
  336. scriptOnNewDataBlock();
  337. return true;
  338. }
  339. void FlyingVehicle::onRemove()
  340. {
  341. SFX_DELETE( mJetSound );
  342. SFX_DELETE( mEngineSound );
  343. scriptOnRemove();
  344. removeFromScene();
  345. Parent::onRemove();
  346. }
  347. //----------------------------------------------------------------------------
  348. void FlyingVehicle::advanceTime(F32 dt)
  349. {
  350. Parent::advanceTime(dt);
  351. updateEngineSound(1);
  352. updateJet(dt);
  353. }
  354. //----------------------------------------------------------------------------
  355. void FlyingVehicle::updateMove(const Move* move)
  356. {
  357. PROFILE_SCOPE( FlyingVehicle_UpdateMove );
  358. Parent::updateMove(move);
  359. if (move == &NullMove)
  360. mSteering.set(0,0);
  361. F32 speed = mRigid.linVelocity.len();
  362. if (speed < mDataBlock->maxAutoSpeed)
  363. mSteering *= mDataBlock->autoInputDamping;
  364. // Check the mission area to get the factor for the flight ceiling
  365. MissionArea * obj = MissionArea::getServerObject();
  366. mCeilingFactor = 1.0f;
  367. if (obj != NULL)
  368. {
  369. F32 flightCeiling = obj->getFlightCeiling();
  370. F32 ceilingRange = obj->getFlightCeilingRange();
  371. if (mRigid.linPosition.z > flightCeiling)
  372. {
  373. // Thrust starts to fade at the ceiling, and is 0 at ceil + range
  374. if (ceilingRange == 0)
  375. {
  376. mCeilingFactor = 0;
  377. }
  378. else
  379. {
  380. mCeilingFactor = 1.0f - ((mRigid.linPosition.z - flightCeiling) / (flightCeiling + ceilingRange));
  381. if (mCeilingFactor < 0.0f)
  382. mCeilingFactor = 0.0f;
  383. }
  384. }
  385. }
  386. mThrust.x = move->x;
  387. mThrust.y = move->y;
  388. if (mThrust.y != 0.0f)
  389. if (mThrust.y > 0)
  390. mThrustDirection = ThrustForward;
  391. else
  392. mThrustDirection = ThrustBackward;
  393. else
  394. mThrustDirection = ThrustDown;
  395. if (mCeilingFactor != 1.0f)
  396. mJetting = false;
  397. }
  398. //----------------------------------------------------------------------------
  399. void FlyingVehicle::updateForces(F32 /*dt*/)
  400. {
  401. PROFILE_SCOPE( FlyingVehicle_UpdateForces );
  402. MatrixF currPosMat;
  403. mRigid.getTransform(&currPosMat);
  404. mRigid.atRest = false;
  405. Point3F massCenter;
  406. currPosMat.mulP(mDataBlock->massCenter,&massCenter);
  407. Point3F xv,yv,zv;
  408. currPosMat.getColumn(0,&xv);
  409. currPosMat.getColumn(1,&yv);
  410. currPosMat.getColumn(2,&zv);
  411. F32 speed = mRigid.linVelocity.len();
  412. Point3F force = Point3F(0, 0, sFlyingVehicleGravity * mRigid.mass * mGravityMod);
  413. Point3F torque = Point3F(0, 0, 0);
  414. // Drag at any speed
  415. force -= mRigid.linVelocity * mDataBlock->minDrag;
  416. torque -= mRigid.angMomentum * mDataBlock->rotationalDrag;
  417. // Auto-stop at low speeds
  418. if (speed < mDataBlock->maxAutoSpeed) {
  419. F32 autoScale = 1 - speed / mDataBlock->maxAutoSpeed;
  420. // Gyroscope
  421. F32 gf = mDataBlock->autoAngularForce * autoScale;
  422. torque -= xv * gf * mDot(yv,Point3F(0,0,1));
  423. // Manuevering jets
  424. F32 sf = mDataBlock->autoLinearForce * autoScale;
  425. force -= yv * sf * mDot(yv, mRigid.linVelocity);
  426. force -= xv * sf * mDot(xv, mRigid.linVelocity);
  427. }
  428. // Hovering Jet
  429. F32 vf = -sFlyingVehicleGravity * mRigid.mass * mGravityMod;
  430. F32 h = getHeight();
  431. if (h <= 1) {
  432. if (h > 0) {
  433. vf -= vf * h * 0.1;
  434. } else {
  435. vf += mDataBlock->jetForce * -h;
  436. }
  437. }
  438. force += zv * vf;
  439. // Damping "surfaces"
  440. force -= xv * mDot(xv,mRigid.linVelocity) * mDataBlock->horizontalSurfaceForce;
  441. force -= zv * mDot(zv,mRigid.linVelocity) * mDataBlock->verticalSurfaceForce;
  442. // Turbo Jet
  443. if (mJetting) {
  444. if (mThrustDirection == ThrustForward)
  445. force += yv * mDataBlock->jetForce * mCeilingFactor;
  446. else if (mThrustDirection == ThrustBackward)
  447. force -= yv * mDataBlock->jetForce * mCeilingFactor;
  448. else
  449. force += zv * mDataBlock->jetForce * mDataBlock->vertThrustMultiple * mCeilingFactor;
  450. }
  451. // Maneuvering jets
  452. force += yv * (mThrust.y * mDataBlock->maneuveringForce * mCeilingFactor);
  453. force += xv * (mThrust.x * mDataBlock->maneuveringForce * mCeilingFactor);
  454. // Steering
  455. Point2F steering;
  456. steering.x = mSteering.x / mDataBlock->maxSteeringAngle;
  457. steering.x *= mFabs(steering.x);
  458. steering.y = mSteering.y / mDataBlock->maxSteeringAngle;
  459. steering.y *= mFabs(steering.y);
  460. torque -= xv * steering.y * mDataBlock->steeringForce;
  461. torque -= zv * steering.x * mDataBlock->steeringForce;
  462. // Roll
  463. torque += yv * steering.x * mDataBlock->steeringRollForce;
  464. F32 ar = mDataBlock->autoAngularForce * mDot(xv,Point3F(0,0,1));
  465. ar -= mDataBlock->rollForce * mDot(xv, mRigid.linVelocity);
  466. torque += yv * ar;
  467. // Add in force from physical zones...
  468. force += mAppliedForce;
  469. // Container buoyancy & drag
  470. force -= Point3F(0, 0, 1) * (mBuoyancy * sFlyingVehicleGravity * mRigid.mass * mGravityMod);
  471. force -= mRigid.linVelocity * mDrag;
  472. //
  473. mRigid.force = force;
  474. mRigid.torque = torque;
  475. }
  476. //----------------------------------------------------------------------------
  477. F32 FlyingVehicle::getHeight()
  478. {
  479. Point3F sp,ep;
  480. RayInfo collision;
  481. F32 height = (createHeightOn) ? mDataBlock->createHoverHeight : mDataBlock->hoverHeight;
  482. F32 r = 10 + height;
  483. getTransform().getColumn(3, &sp);
  484. ep.x = sp.x;
  485. ep.y = sp.y;
  486. ep.z = sp.z - r;
  487. disableCollision();
  488. if( !mContainer->castRay(sp, ep, sClientCollisionMask, &collision) == true )
  489. collision.t = 1;
  490. enableCollision();
  491. return (r * collision.t - height) / 10;
  492. }
  493. //----------------------------------------------------------------------------
  494. U32 FlyingVehicle::getCollisionMask()
  495. {
  496. if (isServerObject())
  497. return sServerCollisionMask;
  498. else
  499. return sClientCollisionMask;
  500. }
  501. //----------------------------------------------------------------------------
  502. void FlyingVehicle::updateEngineSound(F32 level)
  503. {
  504. if ( !mEngineSound )
  505. return;
  506. if ( !mEngineSound->isPlaying() )
  507. mEngineSound->play();
  508. mEngineSound->setTransform( getTransform() );
  509. mEngineSound->setVelocity( getVelocity() );
  510. mEngineSound->setPitch( level );
  511. }
  512. void FlyingVehicle::updateJet(F32 dt)
  513. {
  514. // Thrust Animation threads
  515. // Back
  516. if (mJetSeq[BackActivate] >=0 ) {
  517. if(!mBackMaintainOn || mThrustDirection != ThrustForward) {
  518. if(mBackMaintainOn) {
  519. mShapeInstance->setPos(mJetThread[BackActivate], 1);
  520. mShapeInstance->destroyThread(mJetThread[BackMaintain]);
  521. mBackMaintainOn = false;
  522. }
  523. mShapeInstance->setTimeScale(mJetThread[BackActivate],
  524. (mThrustDirection == ThrustForward)? 1.0f : -1.0f);
  525. mShapeInstance->advanceTime(dt,mJetThread[BackActivate]);
  526. }
  527. if(mJetSeq[BackMaintain] >= 0 && !mBackMaintainOn &&
  528. mShapeInstance->getPos(mJetThread[BackActivate]) >= 1.0) {
  529. mShapeInstance->setPos(mJetThread[BackActivate], 0);
  530. mShapeInstance->setTimeScale(mJetThread[BackActivate], 0);
  531. mJetThread[BackMaintain] = mShapeInstance->addThread();
  532. mShapeInstance->setSequence(mJetThread[BackMaintain],mJetSeq[BackMaintain],0);
  533. mShapeInstance->setTimeScale(mJetThread[BackMaintain],1);
  534. mBackMaintainOn = true;
  535. }
  536. if(mBackMaintainOn)
  537. mShapeInstance->advanceTime(dt,mJetThread[BackMaintain]);
  538. }
  539. // Thrust Animation threads
  540. // Bottom
  541. if (mJetSeq[BottomActivate] >=0 ) {
  542. if(!mBottomMaintainOn || mThrustDirection != ThrustDown || !mJetting) {
  543. if(mBottomMaintainOn) {
  544. mShapeInstance->setPos(mJetThread[BottomActivate], 1);
  545. mShapeInstance->destroyThread(mJetThread[BottomMaintain]);
  546. mBottomMaintainOn = false;
  547. }
  548. mShapeInstance->setTimeScale(mJetThread[BottomActivate],
  549. (mThrustDirection == ThrustDown && mJetting)? 1.0f : -1.0f);
  550. mShapeInstance->advanceTime(dt,mJetThread[BottomActivate]);
  551. }
  552. if(mJetSeq[BottomMaintain] >= 0 && !mBottomMaintainOn &&
  553. mShapeInstance->getPos(mJetThread[BottomActivate]) >= 1.0) {
  554. mShapeInstance->setPos(mJetThread[BottomActivate], 0);
  555. mShapeInstance->setTimeScale(mJetThread[BottomActivate], 0);
  556. mJetThread[BottomMaintain] = mShapeInstance->addThread();
  557. mShapeInstance->setSequence(mJetThread[BottomMaintain],mJetSeq[BottomMaintain],0);
  558. mShapeInstance->setTimeScale(mJetThread[BottomMaintain],1);
  559. mBottomMaintainOn = true;
  560. }
  561. if(mBottomMaintainOn)
  562. mShapeInstance->advanceTime(dt,mJetThread[BottomMaintain]);
  563. }
  564. // Jet particles
  565. for (S32 j = 0; j < NumThrustDirections; j++) {
  566. JetActivation& jet = sJetActivation[j];
  567. updateEmitter(mJetting && j == mThrustDirection,dt,mDataBlock->jetEmitter[jet.emitter],
  568. jet.node,FlyingVehicleData::MaxDirectionJets);
  569. }
  570. // Trail jets
  571. Point3F yv;
  572. mObjToWorld.getColumn(1,&yv);
  573. F32 speed = mFabs(mDot(yv,mRigid.linVelocity));
  574. F32 trail = 0;
  575. if (speed > mDataBlock->minTrailSpeed) {
  576. trail = dt;
  577. if (speed < mDataBlock->maxSpeed)
  578. trail *= (speed - mDataBlock->minTrailSpeed) / mDataBlock->maxSpeed;
  579. }
  580. updateEmitter(trail,trail,mDataBlock->jetEmitter[FlyingVehicleData::TrailEmitter],
  581. FlyingVehicleData::TrailNode,FlyingVehicleData::MaxTrails);
  582. // Allocate/Deallocate voice on demand.
  583. if ( !mJetSound )
  584. return;
  585. if ( !mJetting )
  586. mJetSound->stop();
  587. else
  588. {
  589. if ( !mJetSound->isPlaying() )
  590. mJetSound->play();
  591. mJetSound->setTransform( getTransform() );
  592. mJetSound->setVelocity( getVelocity() );
  593. }
  594. }
  595. //----------------------------------------------------------------------------
  596. void FlyingVehicle::updateEmitter(bool active,F32 dt,ParticleEmitterData *emitter,S32 idx,S32 count)
  597. {
  598. if (!emitter)
  599. return;
  600. for (S32 j = idx; j < idx + count; j++)
  601. if (active) {
  602. if (mDataBlock->jetNode[j] != -1) {
  603. if (!bool(mJetEmitter[j])) {
  604. mJetEmitter[j] = new ParticleEmitter;
  605. mJetEmitter[j]->onNewDataBlock(emitter,false);
  606. mJetEmitter[j]->registerObject();
  607. }
  608. MatrixF mat;
  609. Point3F pos,axis;
  610. mat.mul(getRenderTransform(),
  611. mShapeInstance->mNodeTransforms[mDataBlock->jetNode[j]]);
  612. mat.getColumn(1,&axis);
  613. mat.getColumn(3,&pos);
  614. mJetEmitter[j]->emitParticles(pos,true,axis,getVelocity(),(U32)(dt * 1000));
  615. }
  616. }
  617. else {
  618. for (S32 j = idx; j < idx + count; j++)
  619. if (bool(mJetEmitter[j])) {
  620. mJetEmitter[j]->deleteWhenEmpty();
  621. mJetEmitter[j] = 0;
  622. }
  623. }
  624. }
  625. //----------------------------------------------------------------------------
  626. void FlyingVehicle::writePacketData(GameConnection *connection, BitStream *stream)
  627. {
  628. Parent::writePacketData(connection, stream);
  629. }
  630. void FlyingVehicle::readPacketData(GameConnection *connection, BitStream *stream)
  631. {
  632. Parent::readPacketData(connection, stream);
  633. setPosition(mRigid.linPosition,mRigid.angPosition);
  634. mDelta.pos = mRigid.linPosition;
  635. mDelta.rot[1] = mRigid.angPosition;
  636. }
  637. U32 FlyingVehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  638. {
  639. U32 retMask = Parent::packUpdate(con, mask, stream);
  640. // The rest of the data is part of the control object packet update.
  641. // If we're controlled by this client, we don't need to send it.
  642. if(stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  643. return retMask;
  644. stream->writeFlag(createHeightOn);
  645. stream->writeInt(mThrustDirection,NumThrustBits);
  646. return retMask;
  647. }
  648. void FlyingVehicle::unpackUpdate(NetConnection *con, BitStream *stream)
  649. {
  650. Parent::unpackUpdate(con,stream);
  651. if(stream->readFlag())
  652. return;
  653. createHeightOn = stream->readFlag();
  654. mThrustDirection = ThrustDirection(stream->readInt(NumThrustBits));
  655. }
  656. void FlyingVehicle::initPersistFields()
  657. {
  658. Parent::initPersistFields();
  659. }
  660. DefineEngineMethod( FlyingVehicle, useCreateHeight, void, ( bool enabled ),,
  661. "@brief Set whether the vehicle should temporarily use the createHoverHeight "
  662. "specified in the datablock.\n\nThis can help avoid problems with spawning.\n"
  663. "@param enabled true to use the datablock createHoverHeight, false otherwise\n" )
  664. {
  665. object->useCreateHeight( enabled );
  666. }
  667. void FlyingVehicle::useCreateHeight(bool val)
  668. {
  669. createHeightOn = val;
  670. setMaskBits(HoverHeight);
  671. }