flyingVehicle.cpp 26 KB

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