flyingVehicle.cpp 27 KB

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