flyingVehicle.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. if (isServerObject())
  300. scriptOnAdd();
  301. return true;
  302. }
  303. bool FlyingVehicle::onNewDataBlock(GameBaseData* dptr, bool reload)
  304. {
  305. mDataBlock = dynamic_cast<FlyingVehicleData*>(dptr);
  306. if (!mDataBlock || !Parent::onNewDataBlock(dptr,reload))
  307. return false;
  308. // Sounds
  309. if ( isGhost() )
  310. {
  311. // Create the sounds ahead of time. This reduces runtime
  312. // costs and makes the system easier to understand.
  313. SFX_DELETE( mJetSound );
  314. SFX_DELETE( mEngineSound );
  315. if ( mDataBlock->getFlyingSounds(FlyingVehicleData::EngineSound) )
  316. mEngineSound = SFX->createSource( mDataBlock->getFlyingSoundProfile(FlyingVehicleData::EngineSound), &getTransform() );
  317. if ( mDataBlock->getFlyingSounds(FlyingVehicleData::JetSound))
  318. mJetSound = SFX->createSource( mDataBlock->getFlyingSoundProfile(FlyingVehicleData::JetSound), &getTransform() );
  319. }
  320. // Jet Sequences
  321. for (S32 i = 0; i < JetAnimCount; i++) {
  322. TSShape const* shape = mShapeInstance->getShape();
  323. mJetSeq[i] = shape->findSequence(sJetSequence[i]);
  324. if (mJetSeq[i] != -1) {
  325. if (i == BackActivate || i == BottomActivate) {
  326. mJetThread[i] = mShapeInstance->addThread();
  327. mShapeInstance->setSequence(mJetThread[i],mJetSeq[i],0);
  328. mShapeInstance->setTimeScale(mJetThread[i],0);
  329. }
  330. }
  331. else
  332. mJetThread[i] = 0;
  333. }
  334. scriptOnNewDataBlock();
  335. return true;
  336. }
  337. void FlyingVehicle::onRemove()
  338. {
  339. SFX_DELETE( mJetSound );
  340. SFX_DELETE( mEngineSound );
  341. scriptOnRemove();
  342. removeFromScene();
  343. Parent::onRemove();
  344. }
  345. //----------------------------------------------------------------------------
  346. void FlyingVehicle::advanceTime(F32 dt)
  347. {
  348. Parent::advanceTime(dt);
  349. updateEngineSound(1);
  350. updateJet(dt);
  351. }
  352. //----------------------------------------------------------------------------
  353. void FlyingVehicle::updateMove(const Move* move)
  354. {
  355. PROFILE_SCOPE( FlyingVehicle_UpdateMove );
  356. Parent::updateMove(move);
  357. if (move == &NullMove)
  358. mSteering.set(0,0);
  359. F32 speed = mRigid.linVelocity.len();
  360. if (speed < mDataBlock->maxAutoSpeed)
  361. mSteering *= mDataBlock->autoInputDamping;
  362. // Check the mission area to get the factor for the flight ceiling
  363. MissionArea * obj = MissionArea::getServerObject();
  364. mCeilingFactor = 1.0f;
  365. if (obj != NULL)
  366. {
  367. F32 flightCeiling = obj->getFlightCeiling();
  368. F32 ceilingRange = obj->getFlightCeilingRange();
  369. if (mRigid.linPosition.z > flightCeiling)
  370. {
  371. // Thrust starts to fade at the ceiling, and is 0 at ceil + range
  372. if (ceilingRange == 0)
  373. {
  374. mCeilingFactor = 0;
  375. }
  376. else
  377. {
  378. mCeilingFactor = 1.0f - ((mRigid.linPosition.z - flightCeiling) / (flightCeiling + ceilingRange));
  379. if (mCeilingFactor < 0.0f)
  380. mCeilingFactor = 0.0f;
  381. }
  382. }
  383. }
  384. mThrust.x = move->x;
  385. mThrust.y = move->y;
  386. if (mThrust.y != 0.0f)
  387. if (mThrust.y > 0)
  388. mThrustDirection = ThrustForward;
  389. else
  390. mThrustDirection = ThrustBackward;
  391. else
  392. mThrustDirection = ThrustDown;
  393. if (mCeilingFactor != 1.0f)
  394. mJetting = false;
  395. }
  396. //----------------------------------------------------------------------------
  397. void FlyingVehicle::updateForces(F32 /*dt*/)
  398. {
  399. PROFILE_SCOPE( FlyingVehicle_UpdateForces );
  400. if (mDisableMove) return;
  401. MatrixF currPosMat;
  402. mRigid.getTransform(&currPosMat);
  403. mRigid.atRest = false;
  404. Point3F massCenter;
  405. currPosMat.mulP(mDataBlock->massCenter,&massCenter);
  406. Point3F xv,yv,zv;
  407. currPosMat.getColumn(0,&xv);
  408. currPosMat.getColumn(1,&yv);
  409. currPosMat.getColumn(2,&zv);
  410. F32 speed = mRigid.linVelocity.len();
  411. Point3F force = Point3F(0, 0, mRigid.mass * mNetGravity);
  412. Point3F torque = Point3F(0, 0, 0);
  413. // Drag at any speed
  414. force -= mRigid.linVelocity * mDataBlock->minDrag;
  415. torque -= mRigid.angMomentum * mDataBlock->rotationalDrag;
  416. // Auto-stop at low speeds
  417. if (speed < mDataBlock->maxAutoSpeed) {
  418. F32 autoScale = 1 - speed / mDataBlock->maxAutoSpeed;
  419. // Gyroscope
  420. F32 gf = mDataBlock->autoAngularForce * autoScale;
  421. torque -= xv * gf * mDot(yv,Point3F(0,0,1));
  422. // Manuevering jets
  423. F32 sf = mDataBlock->autoLinearForce * autoScale;
  424. force -= yv * sf * mDot(yv, mRigid.linVelocity);
  425. force -= xv * sf * mDot(xv, mRigid.linVelocity);
  426. }
  427. // Hovering Jet
  428. F32 vf = mRigid.mass * -mNetGravity;
  429. F32 h = getHeight();
  430. if (h <= 1) {
  431. if (h > 0) {
  432. vf -= vf * h * 0.1;
  433. } else {
  434. vf += mDataBlock->jetForce * -h;
  435. }
  436. }
  437. force += zv * vf;
  438. // Damping "surfaces"
  439. force -= xv * mDot(xv,mRigid.linVelocity) * mDataBlock->horizontalSurfaceForce;
  440. force -= zv * mDot(zv,mRigid.linVelocity) * mDataBlock->verticalSurfaceForce;
  441. // Turbo Jet
  442. if (mJetting) {
  443. if (mThrustDirection == ThrustForward)
  444. force += yv * mDataBlock->jetForce * mCeilingFactor;
  445. else if (mThrustDirection == ThrustBackward)
  446. force -= yv * mDataBlock->jetForce * mCeilingFactor;
  447. else
  448. force += zv * mDataBlock->jetForce * mDataBlock->vertThrustMultiple * mCeilingFactor;
  449. }
  450. // Maneuvering jets
  451. force += yv * (mThrust.y * mDataBlock->maneuveringForce * mCeilingFactor);
  452. force += xv * (mThrust.x * mDataBlock->maneuveringForce * mCeilingFactor);
  453. // Steering
  454. Point2F steering;
  455. steering.x = mSteering.x / mDataBlock->maxSteeringAngle;
  456. steering.x *= mFabs(steering.x);
  457. steering.y = mSteering.y / mDataBlock->maxSteeringAngle;
  458. steering.y *= mFabs(steering.y);
  459. torque -= xv * steering.y * mDataBlock->steeringForce;
  460. torque -= zv * steering.x * mDataBlock->steeringForce;
  461. // Roll
  462. torque += yv * steering.x * mDataBlock->steeringRollForce;
  463. F32 ar = mDataBlock->autoAngularForce * mDot(xv,Point3F(0,0,1));
  464. ar -= mDataBlock->rollForce * mDot(xv, mRigid.linVelocity);
  465. torque += yv * ar;
  466. // Add in force from physical zones...
  467. force += mAppliedForce;
  468. force -= mRigid.linVelocity * mDrag;
  469. //
  470. mRigid.force = force;
  471. mRigid.torque = torque;
  472. }
  473. //----------------------------------------------------------------------------
  474. F32 FlyingVehicle::getHeight()
  475. {
  476. Point3F sp,ep;
  477. RayInfo collision;
  478. F32 height = (createHeightOn) ? mDataBlock->createHoverHeight : mDataBlock->hoverHeight;
  479. F32 r = 10 + height;
  480. getTransform().getColumn(3, &sp);
  481. ep.x = sp.x;
  482. ep.y = sp.y;
  483. ep.z = sp.z - r;
  484. disableCollision();
  485. if( !mContainer->castRay(sp, ep, sClientCollisionMask, &collision) == true )
  486. collision.t = 1;
  487. enableCollision();
  488. return (r * collision.t - height) / 10;
  489. }
  490. //----------------------------------------------------------------------------
  491. U32 FlyingVehicle::getCollisionMask()
  492. {
  493. if (isServerObject())
  494. return sServerCollisionMask;
  495. else
  496. return sClientCollisionMask;
  497. }
  498. //----------------------------------------------------------------------------
  499. void FlyingVehicle::updateEngineSound(F32 level)
  500. {
  501. if ( !mEngineSound )
  502. return;
  503. if ( !mEngineSound->isPlaying() )
  504. mEngineSound->play();
  505. mEngineSound->setTransform( getTransform() );
  506. mEngineSound->setVelocity( getVelocity() );
  507. mEngineSound->setPitch( level );
  508. }
  509. void FlyingVehicle::updateJet(F32 dt)
  510. {
  511. // Thrust Animation threads
  512. // Back
  513. if (mJetSeq[BackActivate] >=0 ) {
  514. if(!mBackMaintainOn || mThrustDirection != ThrustForward) {
  515. if(mBackMaintainOn) {
  516. mShapeInstance->setPos(mJetThread[BackActivate], 1);
  517. mShapeInstance->destroyThread(mJetThread[BackMaintain]);
  518. mBackMaintainOn = false;
  519. }
  520. mShapeInstance->setTimeScale(mJetThread[BackActivate],
  521. (mThrustDirection == ThrustForward)? 1.0f : -1.0f);
  522. mShapeInstance->advanceTime(dt,mJetThread[BackActivate]);
  523. }
  524. if(mJetSeq[BackMaintain] >= 0 && !mBackMaintainOn &&
  525. mShapeInstance->getPos(mJetThread[BackActivate]) >= 1.0) {
  526. mShapeInstance->setPos(mJetThread[BackActivate], 0);
  527. mShapeInstance->setTimeScale(mJetThread[BackActivate], 0);
  528. mJetThread[BackMaintain] = mShapeInstance->addThread();
  529. mShapeInstance->setSequence(mJetThread[BackMaintain],mJetSeq[BackMaintain],0);
  530. mShapeInstance->setTimeScale(mJetThread[BackMaintain],1);
  531. mBackMaintainOn = true;
  532. }
  533. if(mBackMaintainOn)
  534. mShapeInstance->advanceTime(dt,mJetThread[BackMaintain]);
  535. }
  536. // Thrust Animation threads
  537. // Bottom
  538. if (mJetSeq[BottomActivate] >=0 ) {
  539. if(!mBottomMaintainOn || mThrustDirection != ThrustDown || !mJetting) {
  540. if(mBottomMaintainOn) {
  541. mShapeInstance->setPos(mJetThread[BottomActivate], 1);
  542. mShapeInstance->destroyThread(mJetThread[BottomMaintain]);
  543. mBottomMaintainOn = false;
  544. }
  545. mShapeInstance->setTimeScale(mJetThread[BottomActivate],
  546. (mThrustDirection == ThrustDown && mJetting)? 1.0f : -1.0f);
  547. mShapeInstance->advanceTime(dt,mJetThread[BottomActivate]);
  548. }
  549. if(mJetSeq[BottomMaintain] >= 0 && !mBottomMaintainOn &&
  550. mShapeInstance->getPos(mJetThread[BottomActivate]) >= 1.0) {
  551. mShapeInstance->setPos(mJetThread[BottomActivate], 0);
  552. mShapeInstance->setTimeScale(mJetThread[BottomActivate], 0);
  553. mJetThread[BottomMaintain] = mShapeInstance->addThread();
  554. mShapeInstance->setSequence(mJetThread[BottomMaintain],mJetSeq[BottomMaintain],0);
  555. mShapeInstance->setTimeScale(mJetThread[BottomMaintain],1);
  556. mBottomMaintainOn = true;
  557. }
  558. if(mBottomMaintainOn)
  559. mShapeInstance->advanceTime(dt,mJetThread[BottomMaintain]);
  560. }
  561. // Jet particles
  562. for (S32 j = 0; j < NumThrustDirections; j++) {
  563. JetActivation& jet = sJetActivation[j];
  564. updateEmitter(mJetting && j == mThrustDirection,dt,mDataBlock->jetEmitter[jet.emitter],
  565. jet.node,FlyingVehicleData::MaxDirectionJets);
  566. }
  567. // Trail jets
  568. Point3F yv;
  569. mObjToWorld.getColumn(1,&yv);
  570. F32 speed = mFabs(mDot(yv,mRigid.linVelocity));
  571. F32 trail = 0;
  572. if (speed > mDataBlock->minTrailSpeed) {
  573. trail = dt;
  574. if (speed < mDataBlock->maxSpeed)
  575. trail *= (speed - mDataBlock->minTrailSpeed) / mDataBlock->maxSpeed;
  576. }
  577. updateEmitter(trail,trail,mDataBlock->jetEmitter[FlyingVehicleData::TrailEmitter],
  578. FlyingVehicleData::TrailNode,FlyingVehicleData::MaxTrails);
  579. // Allocate/Deallocate voice on demand.
  580. if ( !mJetSound )
  581. return;
  582. if ( !mJetting )
  583. mJetSound->stop();
  584. else
  585. {
  586. if ( !mJetSound->isPlaying() )
  587. mJetSound->play();
  588. mJetSound->setTransform( getTransform() );
  589. mJetSound->setVelocity( getVelocity() );
  590. }
  591. }
  592. //----------------------------------------------------------------------------
  593. void FlyingVehicle::updateEmitter(bool active,F32 dt,ParticleEmitterData *emitter,S32 idx,S32 count)
  594. {
  595. if (!emitter)
  596. return;
  597. for (S32 j = idx; j < idx + count; j++)
  598. if (active) {
  599. if (mDataBlock->jetNode[j] != -1) {
  600. if (!bool(mJetEmitter[j])) {
  601. mJetEmitter[j] = new ParticleEmitter;
  602. mJetEmitter[j]->onNewDataBlock(emitter,false);
  603. mJetEmitter[j]->registerObject();
  604. }
  605. MatrixF mat;
  606. Point3F pos,axis;
  607. mat.mul(getRenderTransform(),
  608. mShapeInstance->mNodeTransforms[mDataBlock->jetNode[j]]);
  609. mat.getColumn(1,&axis);
  610. mat.getColumn(3,&pos);
  611. mJetEmitter[j]->emitParticles(pos,true,axis,getVelocity(),(U32)(dt * 1000));
  612. }
  613. }
  614. else {
  615. for (S32 k = idx; k < idx + count; k++)
  616. if (bool(mJetEmitter[k])) {
  617. mJetEmitter[k]->deleteWhenEmpty();
  618. mJetEmitter[k] = 0;
  619. }
  620. }
  621. }
  622. //----------------------------------------------------------------------------
  623. void FlyingVehicle::writePacketData(GameConnection *connection, BitStream *stream)
  624. {
  625. Parent::writePacketData(connection, stream);
  626. }
  627. void FlyingVehicle::readPacketData(GameConnection *connection, BitStream *stream)
  628. {
  629. Parent::readPacketData(connection, stream);
  630. setPosition(mRigid.linPosition,mRigid.angPosition);
  631. mDelta.pos = mRigid.linPosition;
  632. mDelta.rot[1] = mRigid.angPosition;
  633. }
  634. U32 FlyingVehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  635. {
  636. U32 retMask = Parent::packUpdate(con, mask, stream);
  637. // The rest of the data is part of the control object packet update.
  638. // If we're controlled by this client, we don't need to send it.
  639. if(stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  640. return retMask;
  641. stream->writeFlag(createHeightOn);
  642. stream->writeInt(mThrustDirection,NumThrustBits);
  643. return retMask;
  644. }
  645. void FlyingVehicle::unpackUpdate(NetConnection *con, BitStream *stream)
  646. {
  647. Parent::unpackUpdate(con,stream);
  648. if(stream->readFlag())
  649. return;
  650. createHeightOn = stream->readFlag();
  651. mThrustDirection = ThrustDirection(stream->readInt(NumThrustBits));
  652. }
  653. void FlyingVehicle::initPersistFields()
  654. {
  655. Parent::initPersistFields();
  656. }
  657. DefineEngineMethod( FlyingVehicle, useCreateHeight, void, ( bool enabled ),,
  658. "@brief Set whether the vehicle should temporarily use the createHoverHeight "
  659. "specified in the datablock.\n\nThis can help avoid problems with spawning.\n"
  660. "@param enabled true to use the datablock createHoverHeight, false otherwise\n" )
  661. {
  662. object->useCreateHeight( enabled );
  663. }
  664. void FlyingVehicle::useCreateHeight(bool val)
  665. {
  666. createHeightOn = val;
  667. setMaskBits(HoverHeight);
  668. }