VPath.cpp 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "VPath.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/iTickable.h"
  26. #include "core/stream/bitStream.h"
  27. #include "math/mMathFn.h"
  28. #include "math/mathIO.h"
  29. #include "math/mTransform.h"
  30. //-----------------------------------------------------------------------------
  31. // Uncomment this definition to debug the network information.
  32. //#define VPATH_DEBUG_NET
  33. // Uncomment this definition to debug the time step information
  34. //#define VPATH_DEBUG_STEP
  35. //-----------------------------------------------------------------------------
  36. SimObjectPtr<SimSet> VPath::gServerSet = NULL;
  37. U32 VPath::gMaxNodeTransmit = 16;
  38. U32 VPath::gMaxNodeBits = 8;
  39. U32 VPath::gMaxNodeCount = 1 << gMaxNodeBits; // 256
  40. U32 VPath::gMaxObjectTransmit = 4;
  41. U32 VPath::gMaxObjectBits = 4;
  42. U32 VPath::gMaxObjectCount = 1 << gMaxObjectBits; // 16
  43. Point3F VPath::gBezierAxis( 0.f, 1.f, 0.f );
  44. Point3F VPath::gBezierUp( 0.f, 0.f, 1.f );
  45. //-----------------------------------------------------------------------------
  46. static U32 gPathTypeBits = getBinLog2( getNextPow2( VPath::k_PathTypeSize ) );
  47. static F32 gBezierInterpStep = 0.0001f;
  48. //-----------------------------------------------------------------------------
  49. // Path Type Table.
  50. //-----------------------------------------------------------------------------
  51. // Implement the Path Type enum list.
  52. ImplementEnumType( VPathType, "" )
  53. { VPath::k_PathBezier, "BEZIER" },
  54. { VPath::k_PathLinear, "LINEAR" },
  55. EndImplementEnumType;
  56. static VPath::ePathType getPathTypeEnum( const char *pLabel )
  57. {
  58. VPath::ePathType out;
  59. if ( !castConsoleTypeFromString( out, pLabel ) )
  60. {
  61. // Bah!
  62. return VPath::k_PathInvalid;
  63. }
  64. // Return.
  65. return out;
  66. }
  67. //-----------------------------------------------------------------------------
  68. IMPLEMENT_CO_NETOBJECT_V1( VPath );
  69. //-----------------------------------------------------------------------------
  70. VPath::VPath( void ) :
  71. mPathType( k_PathBezier )
  72. {
  73. // Marker Type.
  74. mTypeMask = MarkerObjectType;
  75. // Ghost & Scope.
  76. mNetFlags.set( Ghostable | ScopeAlways );
  77. // Process Ticks.
  78. setProcessTick( true );
  79. VECTOR_SET_ASSOCIATION( mNodeList );
  80. VECTOR_SET_ASSOCIATION( mObjectList );
  81. }
  82. VPath::~VPath( void )
  83. {
  84. // Void.
  85. }
  86. bool VPath::onAdd( void )
  87. {
  88. if ( !Parent::onAdd() )
  89. {
  90. return false;
  91. }
  92. // Add to Scene.
  93. addToScene();
  94. if ( isServerObject() )
  95. {
  96. // Read Fields.
  97. readFields();
  98. // Add to Set.
  99. getServerSet()->addObject( this );
  100. }
  101. return true;
  102. }
  103. void VPath::onDeleteNotify( SimObject *pObject )
  104. {
  105. // Parent Notify.
  106. Parent::onDeleteNotify( pObject );
  107. if ( SceneObject *sceneObject = dynamic_cast<SceneObject*>( pObject ) )
  108. {
  109. // Detach Object.
  110. detachObject( sceneObject );
  111. // Exit.
  112. return;
  113. }
  114. if ( NetConnection *connection = dynamic_cast<NetConnection*>( pObject ) )
  115. {
  116. // Clear Connection References.
  117. for ( VPathObjectIterator itr = mObjectList.begin(); itr != mObjectList.end(); itr++ )
  118. {
  119. // Erase Connection.
  120. ( *itr )->clearConnection( connection );
  121. }
  122. // Exit.
  123. return;
  124. }
  125. }
  126. void VPath::onRemove( void )
  127. {
  128. // Remove From Scene.
  129. removeFromScene();
  130. // Clear Everything.
  131. clear();
  132. Parent::onRemove();
  133. }
  134. void VPath::initPersistFields()
  135. {
  136. docsURL;
  137. Parent::initPersistFields();
  138. addProtectedField( "PathType", TYPEID<ePathType>(), Offset( mPathType, VPath ), &setPathType, &defaultProtectedGetFn, "The type of path this is." );
  139. }
  140. SimSet *VPath::getServerSet( void )
  141. {
  142. if ( !gServerSet )
  143. {
  144. gServerSet = new SimSet();
  145. gServerSet->registerObject( "ServerPathSet" );
  146. Sim::getRootGroup()->addObject( gServerSet );
  147. }
  148. return gServerSet;
  149. }
  150. DefineEngineFunction( getServerPathSet, S32, (),, "( void )" )
  151. {
  152. return VPath::getServerSet()->getId();
  153. }
  154. //-----------------------------------------------------------------------------
  155. //
  156. // Editor Methods.
  157. //
  158. //-----------------------------------------------------------------------------
  159. bool VPath::collideBox( const Point3F &pStart, const Point3F &pEnd, RayInfo* pInfo )
  160. {
  161. if ( mObjBox.isContained( pStart ) )
  162. {
  163. pInfo->t = 0.f;
  164. pInfo->object = this;
  165. pInfo->normal = VectorF( 0.f, 0.f, 1.f );
  166. pInfo->material = NULL;
  167. return true;
  168. }
  169. return Parent::collideBox( pStart, pEnd, pInfo );
  170. }
  171. //-----------------------------------------------------------------------------
  172. //
  173. // Update Methods.
  174. //
  175. //-----------------------------------------------------------------------------
  176. F32 VPath::getUpdatePriority( CameraScopeQuery *pFocusObject, U32 pUpdateMask, S32 pUpdateSkips )
  177. {
  178. if ( mObjectList.size() > 0 )
  179. {
  180. for ( VPathObjectIterator itr = mObjectList.begin(); itr != mObjectList.end(); itr++ )
  181. {
  182. // Fetch Object.
  183. VPathObject *pathObject = ( *itr );
  184. if ( pathObject->isActive() )
  185. {
  186. // High Priority.
  187. return 100.f;
  188. }
  189. }
  190. }
  191. // Normal Priority.
  192. return 0.f;
  193. }
  194. void VPath::updateContainer( void )
  195. {
  196. if ( mNodeList.size() == 0 )
  197. {
  198. // Sanity!.
  199. return;
  200. }
  201. // Init Min / Max.
  202. mObjBox.minExtents = ( mNodeList[0]->getLocalPosition() );
  203. mObjBox.maxExtents = mObjBox.minExtents;
  204. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  205. {
  206. // Fetch Node.
  207. VPathNode *node = ( *itr );
  208. // Node Position.
  209. const Point3F &nodeLocalPosition = node->getLocalPosition();
  210. // Update Object Box.
  211. mObjBox.minExtents.setMin( nodeLocalPosition );
  212. mObjBox.maxExtents.setMax( nodeLocalPosition );
  213. }
  214. // Adjust.
  215. mObjBox.minExtents -= Point3F( 1.f, 1.f, 1.f );
  216. mObjBox.maxExtents += Point3F( 1.f, 1.f, 1.f );
  217. // Reset Box.
  218. resetWorldBox();
  219. resetRenderWorldBox();
  220. }
  221. void VPath::updateNodeTransforms( void )
  222. {
  223. // Fetch Transform Details.
  224. const MatrixF &pathTransform = getTransform();
  225. const QuatF &pathRotation( pathTransform );
  226. const VectorF &pathScale = getScale();
  227. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  228. {
  229. // Fetch Node.
  230. VPathNode *node = ( *itr );
  231. // Fetch Node Spatials.
  232. const Point3F &nodePosition = node->getLocalPosition();
  233. const QuatF &nodeRotation = node->getLocalRotation();
  234. // Calculate the new Position.
  235. Point3F newPosition = nodePosition;
  236. newPosition.convolve( pathScale );
  237. pathTransform.mulP( newPosition );
  238. // Calculate the new Rotation.
  239. QuatF newRotation;
  240. newRotation.mul( nodeRotation, pathRotation );
  241. // Apply.
  242. node->setWorldPosition( newPosition );
  243. node->setWorldRotation( newRotation );
  244. }
  245. }
  246. void VPath::setTransform( const MatrixF &pMatrix )
  247. {
  248. // Parent Call.
  249. Parent::setTransform( pMatrix );
  250. // Update Nodes.
  251. updateNodeTransforms();
  252. if ( isServerObject() )
  253. {
  254. // Update Path.
  255. setMaskBits( PathUpdateMask );
  256. }
  257. }
  258. void VPath::setScale( const VectorF &pScale )
  259. {
  260. // Parent Call.
  261. Parent::setScale( pScale );
  262. // Update Nodes.
  263. updateNodeTransforms();
  264. if ( isServerObject() )
  265. {
  266. // Update Path.
  267. setMaskBits( PathUpdateMask );
  268. }
  269. }
  270. DefineEngineMethod( VPath, setPathType, void, (String pathType), ("LINEAR"), "( string pPathType ) - The path type dictates how attached objects move between nodes. There are currently two supported path types, \"BEZIER\" and \"LINEAR\".\n"
  271. "@return No return value." )
  272. {
  273. // Fetch Enum.
  274. const VPath::ePathType &type = getPathTypeEnum(pathType);
  275. // Update.
  276. object->setPathType( type );
  277. }
  278. void VPath::setPathType( const ePathType &pType )
  279. {
  280. // Apply Value.
  281. mPathType = pType;
  282. // Calculate Path.
  283. calculatePath();
  284. if ( isServerObject() )
  285. {
  286. // Update Path.
  287. setMaskBits( PathUpdateMask );
  288. }
  289. }
  290. bool VPath::setPathType( void *pObject, const char *pArray, const char *pData )
  291. {
  292. // Apply Type.
  293. static_cast<VPath*>( pObject )->setPathType( getPathTypeEnum( pData ) );
  294. return false;
  295. }
  296. //-----------------------------------------------------------------------------
  297. //
  298. // Mounting Methods.
  299. //
  300. //-----------------------------------------------------------------------------
  301. bool VPath::isMountIndex( const U32 &pIndex )
  302. {
  303. for ( SceneObject *itr = getMountList(); itr != NULL; itr = itr->getMountLink() )
  304. {
  305. if ( itr->getMountNode() == pIndex )
  306. {
  307. // Yes.
  308. return true;
  309. }
  310. }
  311. // No.
  312. return false;
  313. }
  314. U32 VPath::getAvailableMountIndex( void )
  315. {
  316. U32 i = 0;
  317. while( isMountIndex( i ) )
  318. {
  319. // Increment.
  320. i++;
  321. }
  322. // Return Index.
  323. return i;
  324. }
  325. void VPath::mountObject( SceneObject *pObject, S32 pIndex, const MatrixF &pTransform )
  326. {
  327. #ifdef VPATH_DEBUG_NET
  328. Con::printf( "VPath::mountObject() %d | %d, IsAttached %d", isServerObject(), pObject->getId(), isObjectAttached( pObject ) );
  329. #endif
  330. // Attached?
  331. if ( !isObjectAttached( pObject ) )
  332. {
  333. if ( isServerObject() )
  334. {
  335. // Shouldn't Use this Method.
  336. Con::warnf( "VPath::mountObject() - Use 'attachObject' instead." );
  337. }
  338. // Not Attached.
  339. return;
  340. }
  341. // Parent Call.
  342. Parent::mountObject( pObject, pIndex, pTransform );
  343. // Clear the mounted mask.
  344. // Note: This is so that we send the mounting information via the VPath
  345. // packets instead of letting T3D handle it.
  346. pObject->clearMaskBits( SceneObject::MountedMask );
  347. }
  348. void VPath::unmountObject( SceneObject *pObject )
  349. {
  350. // Fetch Path Object.
  351. VPathObject *pathObject = getPathObject( pObject );
  352. #ifdef VPATH_DEBUG_NET
  353. Con::printf( "VPath::unmountObject() %d | %d, IsAttached %d", isServerObject(), pObject->getId(), pathObject != NULL );
  354. #endif
  355. // Valid?
  356. if ( !pathObject || pObject->getObjectMount() != this )
  357. {
  358. // Warn.
  359. Con::warnf( "VPath::unmountObject() - Object is not attached to this Path. %d", pObject->getId() );
  360. // Not Mounted Here!
  361. return;
  362. }
  363. // Parent Call.
  364. Parent::unmountObject( pObject );
  365. // Clear the mounted mask.
  366. // Note: This is so that we send the mounting information via the VPath
  367. // packets instead of letting T3D handle it.
  368. pObject->clearMaskBits( SceneObject::MountedMask );
  369. }
  370. void VPath::getMountTransform( S32 pIndex, const MatrixF &pInTransform, MatrixF *pTransform )
  371. {
  372. // Fetch the Scene Object.
  373. VPathObject *pathObject = NULL;
  374. for ( SceneObject *itr = getMountList(); itr != NULL; itr = itr->getMountLink() )
  375. {
  376. if ( itr->getMountNode() == pIndex )
  377. {
  378. pathObject = getPathObject( itr );
  379. break;
  380. }
  381. }
  382. if ( !pathObject )
  383. {
  384. // Reset Transform.
  385. *pTransform = pInTransform;
  386. // Sanity!
  387. return;
  388. }
  389. // Advance the Object.
  390. advanceObject( pathObject, TickSec );
  391. // Apply Transform.
  392. *pTransform = pathObject->getTransform();
  393. }
  394. void VPath::getRenderMountTransform( F32 pDelta, S32 pIndex, const MatrixF &pInTransform, MatrixF *pTransform )
  395. {
  396. // Fetch the Scene Object.
  397. VPathObject *pathObject = NULL;
  398. for ( SceneObject *itr = getMountList(); itr != NULL; itr = itr->getMountLink() )
  399. {
  400. if ( itr->getMountNode() == pIndex )
  401. {
  402. pathObject = getPathObject( itr );
  403. break;
  404. }
  405. }
  406. if ( !pathObject )
  407. {
  408. // Reset Transform.
  409. *pTransform = pInTransform;
  410. // Sanity!
  411. return;
  412. }
  413. // Apply Transform.
  414. *pTransform = pathObject->getRenderTransform( pDelta );
  415. }
  416. VectorF VPath::getMountVelocity( const U32 &pIndex )
  417. {
  418. // Fetch the Scene Object.
  419. VPathObject *pathObject = NULL;
  420. for ( SceneObject *itr = getMountList(); itr != NULL; itr = itr->getMountLink() )
  421. {
  422. if ( itr->getMountNode() == pIndex )
  423. {
  424. pathObject = getPathObject( itr );
  425. break;
  426. }
  427. }
  428. if ( !pathObject )
  429. {
  430. // Sanity!
  431. return VectorF::Zero;
  432. }
  433. // Determine Velocity.
  434. return ( pathObject->getOrientation() * pathObject->getSpeed() );
  435. }
  436. //-----------------------------------------------------------------------------
  437. //
  438. // Persistence Methods.
  439. //
  440. //-----------------------------------------------------------------------------
  441. void VPath::readFields( void )
  442. {
  443. const char *nodeData = "";
  444. for ( S32 nodeIndex = 0; String::compare( nodeData = getDataField( StringTable->insert( avar( "Node%d", nodeIndex ) ), NULL ), "" ) != 0; nodeIndex++ )
  445. {
  446. // Create Node.
  447. VPathNode *node = createNode();
  448. // Deserialize the Node.
  449. node->fromString( nodeData );
  450. // Add the Node.
  451. addNode( node );
  452. // Clear Field.
  453. setDataField( StringTable->insert( avar( "Node%d", nodeIndex ) ), NULL, "" );
  454. }
  455. // Update Transforms.
  456. updateNodeTransforms();
  457. // Update Size.
  458. updateContainer();
  459. // Calculate Path.
  460. calculatePath();
  461. }
  462. void VPath::writeFields( Stream &pStream, U32 pTabStop )
  463. {
  464. // Field Name.
  465. StringTableEntry fieldName = StringTable->insert( "node" );
  466. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  467. {
  468. // Set Field.
  469. setDataField( fieldName, avar( "%d" , ( itr - mNodeList.begin() ) ), ( *itr )->toString().c_str() );
  470. }
  471. // Write Fields.
  472. Parent::writeFields( pStream, pTabStop );
  473. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  474. {
  475. // Clear Field.
  476. setDataField( fieldName, avar( "%d" , ( itr - mNodeList.begin() ) ), "" );
  477. }
  478. }
  479. U32 VPath::packUpdate( NetConnection *pConnection, U32 pMask, BitStream *pStream )
  480. {
  481. U32 retMask = Parent::packUpdate( pConnection, pMask, pStream );
  482. if ( pMask & InitialUpdateMask )
  483. {
  484. // Delete Notify.
  485. deleteNotify( pConnection );
  486. }
  487. if ( pStream->writeFlag( pMask & PathUpdateMask ) )
  488. {
  489. // Write Path Type.
  490. pStream->writeInt( mPathType, gPathTypeBits );
  491. // Write Transform.
  492. mathWrite( *pStream, mObjToWorld );
  493. // Write Scale.
  494. mathWrite( *pStream, mObjScale );
  495. }
  496. if ( pStream->writeFlag( pMask & NodeUpdateMask ) )
  497. {
  498. // Path needs recalculating?
  499. bool needsCalculating = false;
  500. // Delete Vector.
  501. Vector<U32> deleteVector;
  502. // Update Vector.
  503. Vector<U32> updateVector;
  504. for ( U32 i = 0; i < mNodeList.size(); i++ )
  505. {
  506. // Fetch Node.
  507. VPathNode *node = mNodeList[i];
  508. // Already In Map?
  509. if ( !node->isConnection( pConnection ) )
  510. {
  511. // Insert.
  512. node->addConnection( pConnection );
  513. }
  514. // Fetch State.
  515. VNetStateInfo *state = node->getState( pConnection );
  516. // Delete new node?
  517. if ( state->Mask & VPathNode::k_StateDelete
  518. && state->Mask & VPathNode::k_StateCreate )
  519. {
  520. // Remove Node.
  521. removeNode( i-- );
  522. // Flag true.
  523. needsCalculating = true;
  524. }
  525. // Delete?
  526. else if ( state->Mask & VPathNode::k_StateDelete )
  527. {
  528. // Add To List.
  529. deleteVector.push_front( i );
  530. }
  531. // Update?
  532. else if ( state->Mask & VPathNode::k_StateUpdate )
  533. {
  534. if ( updateVector.size() < gMaxNodeTransmit )
  535. {
  536. // Add To List.
  537. updateVector.push_back( i );
  538. }
  539. }
  540. }
  541. // More Updates?
  542. if ( updateVector.size() == gMaxNodeTransmit )
  543. {
  544. // More Updates.
  545. retMask |= NodeUpdateMask;
  546. }
  547. // Write Count.
  548. pStream->writeInt( updateVector.size(), gMaxNodeBits + 1 );
  549. for ( Vector<U32>::iterator itr = updateVector.begin(); itr != updateVector.end(); itr++ )
  550. {
  551. // Fetch Index.
  552. const U32 index = ( *itr );
  553. // Write Index.
  554. pStream->writeInt( index, gMaxNodeBits );
  555. // Pack Update.
  556. retMask |= mNodeList[index]->packNode( pConnection, pStream );
  557. }
  558. // Write Count.
  559. pStream->writeInt( deleteVector.size(), gMaxNodeBits + 1 );
  560. if ( deleteVector.size() > 0 )
  561. {
  562. for ( Vector<U32>::iterator itr = deleteVector.begin(); itr != deleteVector.end(); itr++ )
  563. {
  564. // Fetch Index.
  565. const U32 index = ( *itr );
  566. // Write Index.
  567. pStream->writeInt( index, gMaxNodeBits );
  568. // Remove Node.
  569. removeNode( index );
  570. }
  571. // Flag true.
  572. needsCalculating = true;
  573. // Clear Vector.
  574. deleteVector.clear();
  575. }
  576. // Recalculate path?
  577. if ( needsCalculating )
  578. {
  579. // Update Size.
  580. updateContainer();
  581. // Calculate Path.
  582. calculatePath();
  583. }
  584. }
  585. if ( pStream->writeFlag( pMask & ObjectUpdateMask ) )
  586. {
  587. // Detach Vector.
  588. Vector<U32> detachVector;
  589. // Update Vector.
  590. Vector<U32> updateVector;
  591. for ( U32 i = 0; i < mObjectList.size(); i++ )
  592. {
  593. // Fetch Node.
  594. VPathObject *pathObject = mObjectList[i];
  595. // Already In Map?
  596. if ( !pathObject->isConnection( pConnection ) )
  597. {
  598. // Insert.
  599. pathObject->addConnection( pConnection );
  600. }
  601. // Fetch State.
  602. VNetStateInfo *state = pathObject->getState( pConnection );
  603. // Detach newly attached object?
  604. if ( state->Mask & VPathObject::k_StateAttach
  605. && state->Mask & VPathObject::k_StateDetach )
  606. {
  607. // Process Detach.
  608. onDetachObject( pathObject );
  609. // Decrease index.
  610. i -= 1;
  611. // Skip.
  612. continue;
  613. }
  614. // Update?
  615. if ( state->Mask & VPathObject::k_StateUpdate )
  616. {
  617. if ( updateVector.size() < gMaxObjectTransmit )
  618. {
  619. // Add To List.
  620. updateVector.push_back( i );
  621. }
  622. }
  623. // Detach?
  624. if ( state->Mask & VPathObject::k_StateDetach )
  625. {
  626. // Add To List.
  627. detachVector.push_front( i );
  628. }
  629. }
  630. // More Updates?
  631. if ( updateVector.size() == gMaxObjectTransmit )
  632. {
  633. // More Updates.
  634. retMask |= ObjectUpdateMask;
  635. }
  636. // Write Count.
  637. pStream->writeInt( updateVector.size(), gMaxObjectBits + 1 );
  638. for ( Vector<U32>::iterator itr = updateVector.begin(); itr != updateVector.end(); itr++ )
  639. {
  640. // Fetch Index.
  641. const U32 index = ( *itr );
  642. // Write Index.
  643. pStream->writeInt( index, gMaxObjectBits );
  644. // Fetch the object.
  645. VPathObject *pathObject = mObjectList[index];
  646. // Fetch State.
  647. VNetStateInfo *state = pathObject->getState( pConnection );
  648. // Was the Object Attached?
  649. if ( pStream->writeFlag( state->Mask & VPathObject::k_StateAttach ) )
  650. {
  651. #ifdef VPATH_DEBUG_NET
  652. Con::printf( "VPath::packUpdate() - Attached - %d | %d", isServerObject(), index );
  653. #endif
  654. // Clear Update.
  655. state->Mask &= ~VPathObject::k_StateAttach;
  656. }
  657. // Pack Object.
  658. retMask |= mObjectList[index]->packUpdate( pConnection, pStream );
  659. }
  660. // Write Count.
  661. pStream->writeInt( detachVector.size(), gMaxObjectBits + 1 );
  662. if ( detachVector.size() > 0 )
  663. {
  664. for ( Vector<U32>::iterator itr = detachVector.begin(); itr != detachVector.end(); itr++ )
  665. {
  666. // Fetch Index.
  667. const U32 index = ( *itr );
  668. // Write Index.
  669. pStream->writeInt( index, gMaxObjectBits );
  670. // Process Detach.
  671. onDetachObject( mObjectList[index] );
  672. }
  673. // Clear Vector.
  674. detachVector.clear();
  675. }
  676. }
  677. // Return.
  678. return retMask;
  679. }
  680. void VPath::unpackUpdate( NetConnection *pConnection, BitStream *pStream )
  681. {
  682. Parent::unpackUpdate( pConnection, pStream );
  683. // Update Path?
  684. if ( pStream->readFlag() )
  685. {
  686. // Read Path Type.
  687. mPathType = pStream->readInt( gPathTypeBits );
  688. // Read Transform.
  689. mathRead( *pStream, &mObjToWorld );
  690. // Read Scale.
  691. mathRead( *pStream, &mObjScale );
  692. // Update Nodes.
  693. updateNodeTransforms();
  694. // Calculate Path.
  695. calculatePath();
  696. }
  697. // Update Nodes?
  698. if ( pStream->readFlag() )
  699. {
  700. // Number To Update.
  701. const U32 updateCount = pStream->readInt( gMaxNodeBits + 1 );
  702. for ( U32 i = 0; i < updateCount; i++ )
  703. {
  704. // Read Index.
  705. const U32 nodeIndex = pStream->readInt( gMaxNodeBits );
  706. // Was the Node Created?
  707. if ( pStream->readFlag() )
  708. {
  709. // Create Node.
  710. VPathNode *node = createNode();
  711. // Add the Node.
  712. addNode( node, nodeIndex );
  713. }
  714. // Reference Node.
  715. VPathNode *node = mNodeList[nodeIndex];
  716. // Apply Update.
  717. node->unpackNode( pConnection, pStream );
  718. }
  719. // Number To Delete.
  720. const U32 deleteCount = pStream->readInt( gMaxNodeBits + 1 );
  721. for ( U32 i = 0; i < deleteCount; i++ )
  722. {
  723. // Remove Node.
  724. removeNode( pStream->readInt( gMaxNodeBits ) );
  725. }
  726. // Update Size.
  727. updateContainer();
  728. // Calculate Path.
  729. calculatePath();
  730. }
  731. // Update Objects?
  732. if ( pStream->readFlag() )
  733. {
  734. // Number To Update.
  735. const U32 updateCount = pStream->readInt( gMaxObjectBits + 1 );
  736. for ( U32 i = 0; i < updateCount; i++ )
  737. {
  738. // Read Index.
  739. const U32 objectIndex = pStream->readInt( gMaxObjectBits );
  740. // Read Attached.
  741. // Note: The editor handles the both the server and client side attachment calls.
  742. // This is dangerous because there could be a mix up in indices, but it is
  743. // needed to ensure the editor runs smoothly :(
  744. const bool wasAttached = pStream->readFlag();
  745. if ( wasAttached && objectIndex >= mObjectList.size() )
  746. {
  747. #ifdef VPATH_DEBUG_NET
  748. Con::printf( "VPath::unpackUpdate() - WasAttached - %d | %d", isServerObject(), objectIndex );
  749. #endif
  750. // Create & Add to the List.
  751. attachObject( new VPathObject() );
  752. }
  753. // Reference Node.
  754. VPathObject *pathObject = mObjectList[objectIndex];
  755. // Unpack Update.
  756. pathObject->unpackUpdate( pConnection, pStream );
  757. // Object Attached this Unpack?
  758. if ( wasAttached )
  759. {
  760. // Reset.
  761. setPathObjectInterp( pathObject, pathObject->getTimeInterp() );
  762. }
  763. }
  764. // Number To Detach.
  765. const U32 detachCount = pStream->readInt( gMaxObjectBits + 1 );
  766. for ( U32 i = 0; i < detachCount; i++ )
  767. {
  768. // Fetch the path object.
  769. VPathObject *pathObject = mObjectList[pStream->readInt( gMaxObjectBits )];
  770. // Detach callback.
  771. onDetachObject( pathObject );
  772. }
  773. }
  774. }
  775. //-----------------------------------------------------------------------------
  776. //
  777. // Node Methods.
  778. //
  779. //-----------------------------------------------------------------------------
  780. VPathNode *VPath::createNode( void )
  781. {
  782. return new VPathNode();
  783. }
  784. void VPath::deleteNode( VPathNode *pNode )
  785. {
  786. delete pNode;
  787. }
  788. void VPath::clear( void )
  789. {
  790. for ( VPathObjectIterator itr = mObjectList.begin(); itr != mObjectList.end(); itr++ )
  791. {
  792. VPathObject *pathObject = ( *itr );
  793. // Fetch the attached object.
  794. SceneObject *refObject = pathObject->getObject();
  795. // Unmount Object.
  796. unmountObject( refObject );
  797. // Delete the Path Object.
  798. delete pathObject;
  799. }
  800. // Clear Object List.
  801. mObjectList.clear();
  802. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  803. {
  804. deleteNode( ( *itr ) );
  805. }
  806. // Clear Node List.
  807. mNodeList.clear();
  808. if ( isServerObject() )
  809. {
  810. // Update.
  811. setMaskBits( NodeUpdateMask );
  812. }
  813. }
  814. VPathNode *VPath::getNode( const S32 &pNodeIndex )
  815. {
  816. // Sanity!
  817. AssertFatal( pNodeIndex >= 0 && pNodeIndex < mNodeList.size(), "VPath::getNode() - Invalid Index" );
  818. // Return Node.
  819. return mNodeList[pNodeIndex];
  820. }
  821. DefineEngineMethod( VPath, addNode, void, (TransformF transform, F32 weight, S32 location), (MatrixF::Identity, 1.0, -1),
  822. "( transform pTransform, float pWeight, [int pLocation] ) - Add a node with the given properties. Nodes represent physical points that attached objects move towards or between, but the PathType determines \"how\" they move between them.\n"
  823. "@param pTransform The position and rotation of the new node.\n"
  824. "@param pWeight The weight of the new node.\n"
  825. "@param pLocation The index of the new node.\n"
  826. "@return No return value.")
  827. {
  828. // Fetch Invers Path Transform.
  829. MatrixF pathTransformInv = object->getTransform();
  830. pathTransformInv.setPosition( Point3F::Zero );
  831. pathTransformInv.inverse();
  832. Point3F pos;
  833. QuatF rot;
  834. AngAxisF aa;
  835. pos = transform.mPosition;
  836. aa = transform.mOrientation;
  837. // Set Rotation.
  838. rot.set( aa );
  839. // World to Local Position.
  840. Point3F nodePosition = ( pos - object->getPosition() );
  841. pathTransformInv.mulP( nodePosition );
  842. // World to Local Rotation.
  843. MatrixF nodeRotationMat;
  844. rot.setMatrix( &nodeRotationMat );
  845. pathTransformInv.mul( nodeRotationMat );
  846. // Set Quat.
  847. QuatF nodeRotation;
  848. nodeRotation.set( nodeRotationMat );
  849. // Add Node.
  850. VPathNode *node = object->addNode( nodePosition, nodeRotation, weight, location );
  851. // Valid Node?
  852. if ( node )
  853. {
  854. // Update Size.
  855. object->updateContainer();
  856. // Calculate Path.
  857. object->calculatePath();
  858. }
  859. }
  860. VPathNode *VPath::addNode( const Point3F &pPosition, const QuatF &pRotation, const F32 &pWeight, const S32 &pLocation )
  861. {
  862. // Reference Object.
  863. VPathNode *pathNode = createNode();
  864. // Store Properties.
  865. pathNode->setLocalPosition( pPosition );
  866. pathNode->setLocalRotation( pRotation );
  867. pathNode->setWeight( pWeight );
  868. // Add Node.
  869. return addNode( pathNode, pLocation );
  870. }
  871. VPathNode *VPath::addNode( VPathNode *pNode, const S32 &pLocation )
  872. {
  873. if ( pNode->getPath() )
  874. {
  875. // Error.
  876. Con::errorf( "VPath::addNode() - Node already belongs to a Path, '%d'", pNode->getPath()->getId() );
  877. return NULL;
  878. }
  879. else if ( mNodeList.size() == gMaxNodeCount )
  880. {
  881. // Error.
  882. Con::errorf( "VPath::addNode() - Reached Max Nodes (%d)", gMaxNodeCount );
  883. // Delete Node.
  884. deleteNode( pNode );
  885. return NULL;
  886. }
  887. // Set Path.
  888. pNode->setPath( this );
  889. // Update World Data.
  890. pNode->updateWorldData();
  891. if ( pLocation < 0 )
  892. {
  893. // Push Back.
  894. mNodeList.push_back( pNode );
  895. }
  896. else
  897. {
  898. // Fetch Size.
  899. const S32 nodeCount = mNodeList.size();
  900. if ( pLocation >= nodeCount )
  901. {
  902. // Push Back.
  903. mNodeList.push_back( pNode );
  904. }
  905. else
  906. {
  907. // Insert.
  908. mNodeList.insert( ( mNodeList.address() + pLocation ), pNode );
  909. }
  910. }
  911. if ( isServerObject() )
  912. {
  913. // Update.
  914. setMaskBits( NodeUpdateMask );
  915. }
  916. // Return Node.
  917. return pNode;
  918. }
  919. DefineEngineMethod( VPath, deleteNode, void, (S32 nodeIndex), (0), "( int pNodeIndex ) - Delete the node with the given index. If you delete a node that an attached object is moving to, or from then the object's movement will adjust so that it has a valid path.\n"
  920. "@param pNodeIndex The index of the node to be deleted.\n"
  921. "@return No return value." )
  922. {
  923. // Apply Update.
  924. object->deleteNode( nodeIndex );
  925. }
  926. void VPath::deleteNode( const S32 &pNodeIndex )
  927. {
  928. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  929. {
  930. // Woops!
  931. Con::warnf( "VPath::deleteNode() - Invalid Index Specified (%d).", pNodeIndex );
  932. return;
  933. }
  934. // Fetch Node.
  935. VPathNode *node = mNodeList[pNodeIndex];
  936. // Remove Node References.
  937. for ( VPathObjectIterator itr = mObjectList.begin(); itr != mObjectList.end(); itr++ )
  938. {
  939. // Fetch Object.
  940. VPathObject *pathObject = ( *itr );
  941. if ( ( pathObject->getSourceNode() >= pNodeIndex ) || ( pathObject->getDestinationNode() >= pNodeIndex ) )
  942. {
  943. S32 srcNode = pathObject->getSourceNode();
  944. S32 dstNode = pathObject->getDestinationNode();
  945. if ( pathObject->isForward() )
  946. {
  947. if ( srcNode >= pNodeIndex )
  948. {
  949. srcNode -= 1;
  950. }
  951. if ( dstNode > pNodeIndex )
  952. {
  953. dstNode -= 1;
  954. }
  955. }
  956. else
  957. {
  958. if ( srcNode > pNodeIndex )
  959. {
  960. srcNode -= 1;
  961. }
  962. if ( dstNode >= pNodeIndex )
  963. {
  964. dstNode -= 1;
  965. }
  966. }
  967. // Normalize indices.
  968. normalizeNodeIndex( srcNode, ( mNodeList.size() - 1 ) );
  969. normalizeNodeIndex( dstNode, ( mNodeList.size() - 1 ) );
  970. // Apply Update.
  971. pathObject->setNode( srcNode, dstNode );
  972. if ( isServerObject() )
  973. {
  974. // Update Objects.
  975. setMaskBits( ObjectUpdateMask );
  976. }
  977. }
  978. }
  979. if ( isServerObject() )
  980. {
  981. // Network Flags.
  982. setMaskBits( NodeUpdateMask );
  983. // Flag for Deletion.
  984. node->setMaskBits( VPathNode::k_StateDelete );
  985. }
  986. }
  987. void VPath::removeNode( const S32 &pNodeIndex )
  988. {
  989. // Fetch the node.
  990. VPathNode *node = getNode( pNodeIndex );
  991. if ( !node )
  992. {
  993. // Quit.
  994. return;
  995. }
  996. // Delete Node.
  997. deleteNode( node );
  998. // Erase Node.
  999. mNodeList.erase( pNodeIndex );
  1000. }
  1001. S32 VPath::normalizeNodeIndex( S32 &pNodeIndex )
  1002. {
  1003. const S32 nodeCount = mNodeList.size();
  1004. if ( nodeCount == 0 )
  1005. {
  1006. // No Nodex.
  1007. pNodeIndex = 0;
  1008. }
  1009. else
  1010. {
  1011. while ( pNodeIndex < 0 )
  1012. {
  1013. // Wrap Backwards.
  1014. pNodeIndex += nodeCount;
  1015. }
  1016. // Wrap Forwards.
  1017. pNodeIndex %= nodeCount;
  1018. }
  1019. // Return Index.
  1020. return pNodeIndex;
  1021. }
  1022. S32 VPath::normalizeNodeIndex( const S32 &pNodeIndex )
  1023. {
  1024. // Temp.
  1025. S32 nodeIndex = pNodeIndex;
  1026. // Return Index.
  1027. return normalizeNodeIndex( nodeIndex );
  1028. }
  1029. S32 VPath::normalizeNodeIndex( S32 &pNodeIndex, const S32 &pNodeCount )
  1030. {
  1031. if ( pNodeCount == 0 )
  1032. {
  1033. // No Nodex.
  1034. pNodeIndex = 0;
  1035. }
  1036. else
  1037. {
  1038. while ( pNodeIndex < 0 )
  1039. {
  1040. // Wrap Backwards.
  1041. pNodeIndex += pNodeCount;
  1042. }
  1043. // Wrap Forwards.
  1044. pNodeIndex %= pNodeCount;
  1045. }
  1046. // Return Index.
  1047. return pNodeIndex;
  1048. }
  1049. //-----------------------------------------------------------------------------
  1050. //
  1051. // Object Methods.
  1052. //
  1053. //-----------------------------------------------------------------------------
  1054. DefineEngineMethod( VPath, isObjectAttached, bool, (SceneObject* sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Is the object attached to this path?\n"
  1055. "@param pObject The SimObjectID of the object you wish to check.\n"
  1056. "@return Returns true if the object is attached to this path." )
  1057. {
  1058. if (sceneObject== nullptr)
  1059. {
  1060. Con::errorf( "VPath::isObjectAttached() - Invalid Target Object." );
  1061. return false;
  1062. }
  1063. // Attached?
  1064. return object->isObjectAttached( sceneObject );
  1065. }
  1066. bool VPath::isObjectAttached( SceneObject *pObject )
  1067. {
  1068. // Valid Object?
  1069. return ( getPathObject( pObject ) != NULL );
  1070. }
  1071. VPathObject *VPath::getPathObject( SceneObject *pObject )
  1072. {
  1073. for ( VPathObjectIterator itr = mObjectList.begin(); itr != mObjectList.end(); itr++ )
  1074. {
  1075. // Correct Object?
  1076. if ( ( *itr )->getObject() == pObject )
  1077. {
  1078. // Yes.
  1079. return ( *itr );
  1080. }
  1081. }
  1082. return NULL;
  1083. }
  1084. DefineEngineStringlyVariadicMethod( VPath, attachObject, void, 7, 8, "( SimObject pObject, bool pForward, float pSpeed, bool pRelative, int pStartNode, [int pEndNode] ) - Attach an object to this path with the given properties. If the object is already attached to a path, then a warning will be displayed and the object will *not* be attached to this path.\n"
  1085. "@param pObject The SimObjectID of the object to be attached.\n"
  1086. "@param pForward Should the object be moving forward?\n"
  1087. "@param pSpeed The speed that the object will travel around the path.\n"
  1088. "@param pRelative Offset the object based on the difference between the start node and its current position.\n"
  1089. "@param pStartNode The index of the node this object starts pathing from.\n"
  1090. "@param pEndNode The index of the node this object will stop pathing at."
  1091. "@return No return value." )
  1092. {
  1093. // Fetch Object.
  1094. SceneObject *sceneObject;
  1095. if ( !Sim::findObject( argv[2], sceneObject ) )
  1096. {
  1097. Con::errorf( "VPath::attachObject() - Invalid Target Object." );
  1098. return;
  1099. }
  1100. // Fetch Direction.
  1101. const bool forward = dAtob( argv[3] );
  1102. // Fetch Speed.
  1103. const F32 speed = dAtof( argv[4] );
  1104. // Fetch Relativity.
  1105. const bool relative = dAtob( argv[5] );
  1106. // Fetch Start Node.
  1107. const S32 startNode = dAtoi( argv[6] );
  1108. // Fetch End Node.
  1109. const S32 endNode = ( argc >= 8 ) ? dAtoi( argv[7] ) : -1;
  1110. // Attach Object.
  1111. object->attachObject( sceneObject, forward, speed, relative, startNode, endNode );
  1112. }
  1113. void VPath::attachObject( SceneObject *pObject, const bool &pForward, const F32 &pSpeed, const bool &pRelative, const S32 &pStartNode, const S32 &pEndNode )
  1114. {
  1115. attachObject( pObject, pForward, pSpeed, pRelative, pStartNode, pEndNode, VPathObject::k_OrientationToPath, NULL );
  1116. }
  1117. void VPath::attachObject( SceneObject *pObject, const bool &pForward, const F32 &pSpeed, const bool &pRelative, const S32 &pStartNode, const S32 &pEndNode, const VPathObject::eOrientationType &pOrientationMode )
  1118. {
  1119. attachObject( pObject, pForward, pSpeed, pRelative, pStartNode, pEndNode, pOrientationMode, NULL );
  1120. }
  1121. void VPath::attachObject( SceneObject *pObject, const bool &pForward, const F32 &pSpeed, const bool &pRelative, const S32 &pStartNode, const S32 &pEndNode, const VPathObject::eOrientationType &pOrientationMode, void *pOrientationData )
  1122. {
  1123. // Already Pathing?
  1124. if ( isObjectAttached( pObject ) )
  1125. {
  1126. Con::warnf( "VPath::attachObject() - Object Already Attached to a Path." );
  1127. return;
  1128. }
  1129. // Determine Target Nodes.
  1130. const S32 srcNode = normalizeNodeIndex( pStartNode );
  1131. const S32 dstNode = normalizeNodeIndex( ( pForward ) ? pStartNode + 1 : pStartNode - 1 );
  1132. const S32 endNode = ( pEndNode == -1 ) ? pEndNode : normalizeNodeIndex( pEndNode );
  1133. // Valid Source Node?
  1134. if ( getNodeCount() == 0 || !getNode( srcNode ) )
  1135. {
  1136. Con::warnf( "VPath::attachObject() - Invalid Start Node." );
  1137. return;
  1138. }
  1139. VPathObject *pathObject = new VPathObject();
  1140. // Init Properties.
  1141. pathObject->setActive( true );
  1142. pathObject->setObject( pObject );
  1143. pathObject->setForward( pForward );
  1144. pathObject->setTimeInterp( 0.f );
  1145. pathObject->setPathInterp( 0.f );
  1146. pathObject->setOffset( Point3F::Zero );
  1147. pathObject->setSpeed( pSpeed );
  1148. switch( pOrientationMode )
  1149. {
  1150. case VPathObject::k_OrientationFree :
  1151. case VPathObject::k_OrientationInterpolate :
  1152. case VPathObject::k_OrientationToPath :
  1153. {
  1154. pathObject->setOrientationMode( pOrientationMode );
  1155. } break;
  1156. case VPathObject::k_OrientationToObject :
  1157. {
  1158. pathObject->setOrientationMode( pOrientationMode, (SceneObject*)pOrientationData );
  1159. } break;
  1160. case VPathObject::k_OrientationToPoint :
  1161. {
  1162. pathObject->setOrientationMode( pOrientationMode, ( *(Point3F*)pOrientationData ) );
  1163. } break;
  1164. }
  1165. pathObject->setNode( srcNode, dstNode );
  1166. pathObject->setStartNode( srcNode );
  1167. pathObject->setEndNode( endNode );
  1168. // Fetch Init Node.
  1169. VPathNode *node = mNodeList[srcNode];
  1170. // Relative Position?
  1171. if ( pRelative )
  1172. {
  1173. // Set Position Offset.
  1174. pathObject->setOffset( pObject->getPosition() - node->getWorldPosition() );
  1175. }
  1176. // Set info.
  1177. setPathObjectInterp( pathObject, 0.f );
  1178. // Attach.
  1179. attachObject( pathObject );
  1180. }
  1181. void VPath::attachObject( VPathObject *pPathObject )
  1182. {
  1183. #ifdef VPATH_DEBUG_NET
  1184. Con::printf( "VPath::attachObject() - %d", isServerObject() );
  1185. #endif
  1186. if ( mObjectList.size() == gMaxObjectCount )
  1187. {
  1188. Con::errorf( "VPath::attachObject() - Reached Max Objects (%d)", gMaxObjectCount );
  1189. return;
  1190. }
  1191. // Add to List.
  1192. mObjectList.push_back( pPathObject );
  1193. // Callback.
  1194. onAttachObject( pPathObject );
  1195. if ( isServerObject() )
  1196. {
  1197. // Update.
  1198. setMaskBits( ObjectUpdateMask );
  1199. }
  1200. }
  1201. void VPath::onAttachObject( VPathObject *pPathObject )
  1202. {
  1203. // Valid Object?
  1204. SceneObject *refObject = pPathObject->getObject();
  1205. if ( !refObject )
  1206. {
  1207. return;
  1208. }
  1209. #ifdef VPATH_DEBUG_NET
  1210. Con::printf( "VPath::onAttachObject() - %d | %d", isServerObject(), refObject->getId() );
  1211. #endif
  1212. // Delete Notify.
  1213. deleteNotify( refObject );
  1214. if ( isServerObject() )
  1215. {
  1216. // Fetch the Available Mount Index.
  1217. U32 mountIndex = getAvailableMountIndex();
  1218. // Mount the Object to this Path.
  1219. mountObject( refObject, mountIndex );
  1220. // Return Buffer.
  1221. char buffer[1][32];
  1222. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", refObject->getId() );
  1223. // Callback.
  1224. // VPath::onAttachObject( %object );
  1225. Con::executef( this, "onAttachObject", buffer[0] );
  1226. }
  1227. }
  1228. DefineEngineMethod( VPath, detachObject, void, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Detach the object from this path in place.\n"
  1229. "@param pObject The SimObjectID of the object to be detached.\n"
  1230. "@return No return value." )
  1231. {
  1232. // Fetch Object.
  1233. if (sceneObject == nullptr)
  1234. {
  1235. Con::errorf( "VPath::detachObject() - Invalid Target Object." );
  1236. return;
  1237. }
  1238. // Detach Object.
  1239. object->detachObject( sceneObject );
  1240. }
  1241. void VPath::detachObject( SceneObject *pObject )
  1242. {
  1243. VPathObject *pathObject = getPathObject( pObject );
  1244. if ( !pathObject )
  1245. {
  1246. Con::warnf( "VPath::detachObject() - Object (%d) Not Attached to Path.", pObject->getId() );
  1247. return;
  1248. }
  1249. // Detach.
  1250. detachObject( pathObject );
  1251. }
  1252. void VPath::detachObject( VPathObject *pPathObject )
  1253. {
  1254. #ifdef VPATH_DEBUG_NET
  1255. Con::printf( "VPath::detachObject() - %d", isServerObject() );
  1256. #endif
  1257. if ( isServerObject() )
  1258. {
  1259. // Update Objects.
  1260. setMaskBits( ObjectUpdateMask );
  1261. // Detach.
  1262. pPathObject->setMaskBits( VPathObject::k_StateDetach );
  1263. }
  1264. /*
  1265. // Valid Object?
  1266. SceneObject *refObject = pPathObject->getObject();
  1267. if ( refObject )
  1268. {
  1269. // Unmount Object.
  1270. unmountObject( refObject );
  1271. }
  1272. */
  1273. }
  1274. void VPath::onDetachObject( VPathObject *pPathObject )
  1275. {
  1276. // Valid Object?
  1277. SceneObject *refObject = pPathObject->getObject();
  1278. if ( !refObject )
  1279. {
  1280. return;
  1281. }
  1282. #ifdef VPATH_DEBUG_NET
  1283. Con::printf( "VPath::onDetachObject() - %d | %d", isServerObject(), refObject->getId() );
  1284. #endif
  1285. // Reset.
  1286. setPathObjectInterp( pPathObject, pPathObject->getTimeInterp() );
  1287. // Unmount Object.
  1288. unmountObject( refObject );
  1289. // Delete the Path Object.
  1290. delete pPathObject;
  1291. // Remove from the Set.
  1292. mObjectList.erase( mObjectList.find_next( pPathObject ) );
  1293. // Clear Delete Notify.
  1294. clearNotify( refObject );
  1295. if ( isServerObject() )
  1296. {
  1297. // Return Buffer.
  1298. char buffer[1][32];
  1299. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", refObject->getId() );
  1300. // Callback.
  1301. // VPath::onDetachObject( %object );
  1302. Con::executef( this, "onDetachObject", buffer[0] );
  1303. }
  1304. }
  1305. void VPath::processTick( const Move *pMove )
  1306. {
  1307. }
  1308. void VPath::advanceObject( VPathObject *pPathObject, const F32 &pDelta )
  1309. {
  1310. SceneObject *refObject = pPathObject->getObject();
  1311. if ( !refObject || mIsZero( pDelta ) )
  1312. {
  1313. // Ignore.
  1314. return;
  1315. }
  1316. // Spatial Delta.
  1317. pPathObject->popDelta();
  1318. // Active and Moving?
  1319. if ( !pPathObject->isActive() || mIsZero( pPathObject->getSpeed() ) )
  1320. {
  1321. // Update Delta.
  1322. pPathObject->pushDelta( refObject->getPosition(), refObject->getTransform().getForwardVector() );
  1323. // Skip.
  1324. return;
  1325. }
  1326. // Fetch Nodes.
  1327. VPathNode *srcNode = mNodeList[pPathObject->getSourceNode()];
  1328. VPathNode *dstNode = mNodeList[pPathObject->getDestinationNode()];
  1329. VPathNode *lenNode = ( pPathObject->isForward() ) ? srcNode : dstNode;
  1330. // Calculate Interp Delta.
  1331. const F32 stepDistance = ( pPathObject->getSpeed() * pDelta );
  1332. const F32 speedMod = ( pPathObject->getSpeed() / lenNode->getLength() );
  1333. F32 timeInterp = pPathObject->getTimeInterp();
  1334. F32 timeInterpDelta = ( speedMod * pDelta );
  1335. F32 pathInterp = pPathObject->getPathInterp();
  1336. F32 pathInterpDelta = 0.f;
  1337. // Fetch the old position.
  1338. const Point3F oldPosition = pPathObject->getPosition();
  1339. // Calculate the new position and path delta.
  1340. Point3F newPosition = getAdvancedPathPosition( pPathObject, stepDistance, pathInterpDelta );
  1341. // Finished?
  1342. if ( ( timeInterp + timeInterpDelta ) >= 1.f )
  1343. {
  1344. // Finished?
  1345. if ( pPathObject->getDestinationNode() == pPathObject->getEndNode() )
  1346. {
  1347. // Stop Updates.
  1348. pPathObject->setActive( false );
  1349. }
  1350. else
  1351. {
  1352. // Update Nodes.
  1353. const S32 srcNodeIndex = pPathObject->getDestinationNode();
  1354. const S32 dstNodeIndex = normalizeNodeIndex( ( pPathObject->isForward() ) ? srcNodeIndex + 1 : srcNodeIndex - 1 );
  1355. #ifdef VPATH_DEBUG_STEP
  1356. if ( isServerObject() )
  1357. Con::errorf( "Change Node:\n Source, %d\n Destination, %d", srcNodeIndex, dstNodeIndex );
  1358. #endif
  1359. // Apply Changes.
  1360. pPathObject->setNode( srcNodeIndex, dstNodeIndex );
  1361. pPathObject->setTimeInterp( 0.f );
  1362. pPathObject->setPathInterp( 0.f );
  1363. pPathObject->setPosition( newPosition );
  1364. // Reset local interp information.
  1365. timeInterp = 0.f;
  1366. timeInterpDelta = 0.f;
  1367. pathInterp = 0.f;
  1368. pathInterpDelta = 0.f;
  1369. // Fetch the distance we've travelled.
  1370. const F32 &advanceDistance = ( newPosition - oldPosition ).len();
  1371. // Any remaining distance?
  1372. if ( ( stepDistance - advanceDistance ) > 0.0001f )
  1373. {
  1374. // Determine how much more we need to move.
  1375. Point3F newPosition0 = newPosition;
  1376. newPosition = getAdvancedPathPosition( pPathObject, ( stepDistance - advanceDistance ), pathInterpDelta );
  1377. #ifdef VPATH_DEBUG_STEP
  1378. if ( isServerObject() )
  1379. Con::errorf( "Transition Step: %f\nTransition Distance: %f + %f = %f", pathInterpDelta, advanceDistance, ( newPosition - newPosition0 ).len(), advanceDistance + ( newPosition - newPosition0 ).len() );
  1380. #endif
  1381. }
  1382. }
  1383. if ( isServerObject() )
  1384. {
  1385. // Return Buffer.
  1386. char buffer[3][32];
  1387. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", refObject->getId() );
  1388. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pPathObject->isActive() ? pPathObject->getSourceNode() : pPathObject->getDestinationNode() );
  1389. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", !pPathObject->isActive() );
  1390. // Callback.
  1391. // VPath::onReachNode( %object, %node, %finished );
  1392. Con::executef( this, "onReachNode", buffer[0], buffer[1], buffer[2] );
  1393. }
  1394. }
  1395. // Update Object Interp.
  1396. timeInterp = mClampF( timeInterp + timeInterpDelta, 0.f, 1.f );
  1397. pathInterp = mClampF( pathInterp + pathInterpDelta, 0.f, 1.f );
  1398. // Apply Changes.
  1399. pPathObject->setTimeInterp( timeInterp );
  1400. pPathObject->setPathInterp( pathInterp );
  1401. pPathObject->setPosition( newPosition );
  1402. #ifdef VPATH_DEBUG_STEP
  1403. if ( isServerObject() )
  1404. Con::printf( "Time / Distance: %f %f / %f %f", timeInterp, pathInterp, stepDistance, ( newPosition - oldPosition ).len() );
  1405. #endif
  1406. switch ( pPathObject->getOrientationMode().Type )
  1407. {
  1408. case VPathObject::k_OrientationInterpolate :
  1409. case VPathObject::k_OrientationToObject :
  1410. case VPathObject::k_OrientationToPoint :
  1411. {
  1412. // Update Orientation.
  1413. updateOrientation( pPathObject );
  1414. } break;
  1415. case VPathObject::k_OrientationToPath :
  1416. {
  1417. // Determine the path orientation.
  1418. VectorF pathOrientation = ( newPosition - oldPosition );
  1419. pathOrientation.normalize();
  1420. // Update Orientation.
  1421. updateOrientation( pPathObject, pathOrientation );
  1422. } break;
  1423. }
  1424. // Update Delta.
  1425. pPathObject->pushDelta( pPathObject->getPosition(), pPathObject->getOrientation() );
  1426. if ( isServerObject() )
  1427. {
  1428. // Update Objects.
  1429. setMaskBits( ObjectUpdateMask );
  1430. // Update This Object.
  1431. pPathObject->setMaskBits( VPathObject::k_StateUpdatePosition );
  1432. }
  1433. }
  1434. void VPath::updatePosition( VPathObject *pPathObject )
  1435. {
  1436. // Fetch Nodes.
  1437. VPathNode *srcNode = getNode( pPathObject->getSourceNode() );
  1438. VPathNode *dstNode = getNode( pPathObject->getDestinationNode() );
  1439. // Fetch Position.
  1440. F32 pathInterp = 0.f;
  1441. const Point3F newPosition = getPathPosition( srcNode, dstNode, pPathObject->getTimeInterp(), pPathObject->isForward(), pathInterp );
  1442. // Apply Position.
  1443. pPathObject->setPosition( newPosition );
  1444. pPathObject->setPathInterp( pathInterp );
  1445. }
  1446. void VPath::updateOrientation( VPathObject *pPathObject )
  1447. {
  1448. // Update Orientation?
  1449. if ( pPathObject->getOrientationMode().Type == VPathObject::k_OrientationFree )
  1450. {
  1451. // Skip.
  1452. return;
  1453. }
  1454. // Fetch Nodes.
  1455. VPathNode *srcNode = getNode( pPathObject->getSourceNode() );
  1456. VPathNode *dstNode = getNode( pPathObject->getDestinationNode() );
  1457. // Determine Path Orientation.
  1458. VectorF pathOrientation;
  1459. switch ( pPathObject->getOrientationMode().Type )
  1460. {
  1461. case VPathObject::k_OrientationInterpolate :
  1462. {
  1463. // Interpolate Between Transforms.
  1464. QuatF rot;
  1465. rot.interpolate( srcNode->getWorldRotation(), dstNode->getWorldRotation(), pPathObject->getPathInterp() );
  1466. // Set Matrix.
  1467. MatrixF mat;
  1468. rot.setMatrix( &mat );
  1469. // Fetch Orientation.
  1470. pathOrientation = mat.getColumn3F( 1 );
  1471. } break;
  1472. case VPathObject::k_OrientationToObject :
  1473. {
  1474. // Fetch Orientation.
  1475. pathOrientation = ( pPathObject->getOrientationMode().Object->getPosition() - pPathObject->getWorldPosition() );
  1476. pathOrientation.normalizeSafe();
  1477. } break;
  1478. case VPathObject::k_OrientationToPoint :
  1479. {
  1480. // Fetch Orientation.
  1481. pathOrientation = ( pPathObject->getOrientationMode().Point - pPathObject->getWorldPosition() );
  1482. pathOrientation.normalizeSafe();
  1483. } break;
  1484. case VPathObject::k_OrientationToPath :
  1485. {
  1486. // Fetch Orientation.
  1487. pathOrientation = getPathOrientation( srcNode, dstNode, pPathObject->getPathInterp(), pPathObject->isForward() );
  1488. } break;
  1489. }
  1490. // Update.
  1491. updateOrientation( pPathObject, pathOrientation );
  1492. }
  1493. void VPath::updateOrientation( VPathObject *pPathObject, const Point3F &pPathOrientation )
  1494. {
  1495. // Update Orientation?
  1496. if ( pPathObject->getOrientationMode().Type == VPathObject::k_OrientationFree )
  1497. {
  1498. // Skip.
  1499. return;
  1500. }
  1501. // Fetch Nodes.
  1502. VPathNode *srcNode = getNode( pPathObject->getSourceNode() );
  1503. VPathNode *dstNode = getNode( pPathObject->getDestinationNode() );
  1504. // Determine Source Orientation.
  1505. VectorF srcOrientation;
  1506. switch ( srcNode->getOrientationMode().Type )
  1507. {
  1508. case VPathNode::k_OrientationToPoint :
  1509. {
  1510. // Fetch Orientation.
  1511. srcOrientation = ( srcNode->getOrientationMode().Point - pPathObject->getWorldPosition() );
  1512. srcOrientation.normalize();
  1513. } break;
  1514. default :
  1515. {
  1516. // Use Path Orientation.
  1517. srcOrientation = pPathOrientation;
  1518. } break;
  1519. }
  1520. // Determine Destination Orientation.
  1521. VectorF dstOrientation;
  1522. switch ( dstNode->getOrientationMode().Type )
  1523. {
  1524. case VPathNode::k_OrientationToPoint :
  1525. {
  1526. // Fetch Orientation.
  1527. dstOrientation = ( dstNode->getOrientationMode().Point - pPathObject->getWorldPosition() );
  1528. dstOrientation.normalize();
  1529. } break;
  1530. default :
  1531. {
  1532. // Use Path Orientation.
  1533. dstOrientation = pPathOrientation;
  1534. } break;
  1535. }
  1536. // Determine Actual Orientation.
  1537. VectorF orientation;
  1538. orientation.interpolate( srcOrientation, dstOrientation, pPathObject->getTimeInterp() );
  1539. // Apply.
  1540. pPathObject->setOrientation( orientation );
  1541. }
  1542. //-----------------------------------------------------------------------------
  1543. //
  1544. // Path Methods.
  1545. //
  1546. //-----------------------------------------------------------------------------
  1547. void VPath::calculatePath( void )
  1548. {
  1549. if ( mNodeList.size() < 2 )
  1550. {
  1551. // No Path.
  1552. return;
  1553. }
  1554. switch ( mPathType )
  1555. {
  1556. case k_PathLinear :
  1557. {
  1558. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  1559. {
  1560. if ( itr == ( mNodeList.end() - 1 ) )
  1561. {
  1562. // Head, Front.
  1563. calculateLinearPath( ( *itr ), ( *( mNodeList.begin() ) ) );
  1564. }
  1565. else
  1566. {
  1567. // Head, Next.
  1568. calculateLinearPath( ( *itr ), ( *( itr + 1 ) ) );
  1569. }
  1570. }
  1571. } break;
  1572. case k_PathBezier :
  1573. {
  1574. for ( VPathNodeIterator itr = mNodeList.begin(); itr != mNodeList.end(); itr++ )
  1575. {
  1576. if ( itr == ( mNodeList.end() - 1 ) )
  1577. {
  1578. // Head, Prev, Front.
  1579. calculateBezierPath( ( *itr ), ( *( mNodeList.begin() ) ) );
  1580. }
  1581. else
  1582. {
  1583. // Head, Prev, Next.
  1584. calculateBezierPath( ( *itr ), ( *( itr + 1 ) ) );
  1585. }
  1586. }
  1587. } break;
  1588. }
  1589. }
  1590. Point3F VPath::getAdvancedPathPosition( VPathObject *pPathObject, const F32 &pTargetDistance, F32 &pPathInterpDelta )
  1591. {
  1592. switch( mPathType )
  1593. {
  1594. case k_PathLinear :
  1595. {
  1596. return getAdvancedLinearPathPosition( pPathObject, pTargetDistance, pPathInterpDelta );
  1597. } break;
  1598. case k_PathBezier :
  1599. {
  1600. return getAdvancedBezierPathPosition( pPathObject, pTargetDistance, pPathInterpDelta );
  1601. } break;
  1602. }
  1603. // Sanity!
  1604. AssertFatal( false, "Invalid path type!" );
  1605. return Point3F::Zero;
  1606. }
  1607. DefineEngineMethod( VPath, getPathTransform, const char *, (S32 srcNodeIndex, S32 dstNodeIndex, F32 timeInterp), (0,0,1.0), "( int pSrcNodeIndex, int pDstNodeIndex, float pTimeInterp ) - Get the transform of the path at the interp point between two nodes.\n"
  1608. "@param pSrcNodeIndex The first node.\n"
  1609. "@param pDstNodeIndex The second node.\n"
  1610. "@param pTimeInterp The time to interp between the two nodes. Value is between 0.0 and 1.0.\n"
  1611. "@return Returns the transform of the interp time between the two given nodes." )
  1612. {
  1613. // Fetch Nodes.
  1614. VPathNode *srcNode = object->getNode(srcNodeIndex);
  1615. VPathNode *dstNode = object->getNode(dstNodeIndex);
  1616. // Interp Time.
  1617. const F32 &interp = timeInterp;
  1618. // Fetch Position & Orientation.
  1619. const Point3F position = object->getPathPosition( srcNode, dstNode, interp, true );
  1620. const VectorF orientation = object->getPathOrientation( srcNode, dstNode, interp, true );
  1621. // Y-Axis.
  1622. VectorF yVec = orientation;
  1623. yVec.normalize();
  1624. // X-Axis.
  1625. VectorF xVec = mCross( yVec, VPath::gBezierUp );
  1626. xVec.normalize();
  1627. // Z-Axis.
  1628. VectorF zVec = mCross( xVec, yVec );
  1629. zVec.normalize();
  1630. // Setup Object Transform.
  1631. MatrixF mat( true );
  1632. mat.setColumn( 0, xVec );
  1633. mat.setColumn( 1, yVec );
  1634. mat.setColumn( 2, zVec );
  1635. // AngAxis.
  1636. AngAxisF aa( mat );
  1637. // Return Buffer;
  1638. char *buffer = Con::getReturnBuffer( 256 );
  1639. dSprintf( buffer, 256, "%g %g %g %g %g %g %g", position.x, position.y, position.z,
  1640. aa.axis.x, aa.axis.y, aa.axis.z, aa.angle );
  1641. // Return.
  1642. return buffer;
  1643. }
  1644. DefineEngineMethod( VPath, getPathPosition, const char *, (S32 srcNodeIndex, S32 dstNodeIndex, F32 timeInterp), (0, 0, 1.0), "( int pSrcNodeIndex, int pDstNodeIndex, int pTimeInterp ) - Get the world position of the path at the interp point between two nodes.\n"
  1645. "@param pSrcNodeIndex The first node.\n"
  1646. "@param pDstNodeIndex The second node.\n"
  1647. "@param pTimeInterp The time to interp between the two nodes. Value is between 0.0 and 1.0.\n"
  1648. "@return Returns the world position of the interp time between the two given nodes." )
  1649. {
  1650. // Fetch Nodes.
  1651. VPathNode *srcNode = object->getNode(srcNodeIndex);
  1652. VPathNode *dstNode = object->getNode(dstNodeIndex);
  1653. // Interp Time.
  1654. const F32 &interp = timeInterp;
  1655. // Find Position.
  1656. const Point3F position = object->getPathPosition( srcNode, dstNode, interp, true );
  1657. // Return Buffer;
  1658. char *buffer = Con::getReturnBuffer( 128 );
  1659. dSprintf( buffer, 128, "%g %g %g", position.x, position.y, position.z );
  1660. // Return.
  1661. return buffer;
  1662. }
  1663. Point3F VPath::getPathPosition( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward )
  1664. {
  1665. F32 pathInterp = 0.f;
  1666. return getPathPosition( pSourceNode, pDestinationNode, pTimeInterp, pForward, pathInterp );
  1667. }
  1668. Point3F VPath::getPathPosition( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward, F32 &pPathInterp )
  1669. {
  1670. switch( mPathType )
  1671. {
  1672. case k_PathBezier :
  1673. {
  1674. return getBezierPathPosition( pSourceNode, pDestinationNode, pTimeInterp, pForward, pPathInterp );
  1675. } break;
  1676. case k_PathLinear :
  1677. {
  1678. return getLinearPathPosition( pSourceNode, pDestinationNode, pTimeInterp, pForward, pPathInterp );
  1679. } break;
  1680. }
  1681. // NULL.
  1682. return Point3F::Zero;
  1683. }
  1684. VectorF VPath::getPathOrientation( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward )
  1685. {
  1686. switch( mPathType )
  1687. {
  1688. case k_PathBezier :
  1689. {
  1690. return getBezierPathOrientation( pSourceNode, pDestinationNode, pTimeInterp, pForward );
  1691. } break;
  1692. case k_PathLinear :
  1693. {
  1694. return getLinearPathOrientation( pSourceNode, pDestinationNode, pTimeInterp, pForward );
  1695. } break;
  1696. }
  1697. // NULL.
  1698. return VectorF::Zero;
  1699. }
  1700. //-----------------------------------------------------------------------------
  1701. //
  1702. // Linear Path Methods.
  1703. //
  1704. //-----------------------------------------------------------------------------
  1705. void VPath::calculateLinearPath( VPathNode *pNode, VPathNode *pNextNode )
  1706. {
  1707. // Calculate Segment Length.
  1708. pNode->setLength( ( pNextNode->getWorldPosition() - pNode->getWorldPosition() ).len() );
  1709. }
  1710. Point3F VPath::getAdvancedLinearPathPosition( VPathObject *pPathObject, const F32 &pTargetDistance, F32 &pPathInterpDelta )
  1711. {
  1712. // Fetch Nodes.
  1713. VPathNode *srcNode = mNodeList[pPathObject->getSourceNode()];
  1714. VPathNode *dstNode = mNodeList[pPathObject->getDestinationNode()];
  1715. // Fetch the length of the segment.
  1716. const F32 length = ( pPathObject->isForward() ) ? srcNode->getLength() : dstNode->getLength();
  1717. // Set the interp delta.
  1718. pPathInterpDelta = ( pTargetDistance / length );
  1719. // Return the position.
  1720. F32 pathInterp = 0.f;
  1721. return getLinearPathPosition( srcNode, dstNode, pPathObject->getPathInterp(), pPathObject->isForward(), pathInterp );
  1722. }
  1723. Point3F VPath::getLinearPathPosition( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward, F32 &pPathInterp )
  1724. {
  1725. // Set path interp to the time interp.
  1726. pPathInterp = pTimeInterp;
  1727. if ( pTimeInterp <= 0.f )
  1728. {
  1729. // Source Node.
  1730. return pSourceNode->getWorldPosition();
  1731. }
  1732. else if ( pTimeInterp >= 1.f )
  1733. {
  1734. // Destination Node.
  1735. return pDestinationNode->getWorldPosition();
  1736. }
  1737. // Calculate Position.
  1738. Point3F position;
  1739. position.interpolate( pSourceNode->getWorldPosition(), pDestinationNode->getWorldPosition(), pTimeInterp );
  1740. // Return.
  1741. return position;
  1742. }
  1743. VectorF VPath::getLinearPathOrientation( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward )
  1744. {
  1745. // Calculate Orientation.
  1746. VectorF newOrientation = ( pDestinationNode->getWorldPosition() - pSourceNode->getWorldPosition() );
  1747. newOrientation.normalizeSafe();
  1748. // Return.
  1749. return newOrientation;
  1750. }
  1751. //-----------------------------------------------------------------------------
  1752. //
  1753. // Bezier Path Methods.
  1754. //
  1755. //-----------------------------------------------------------------------------
  1756. void VPath::calculateBezierPath( VPathNode *pNode, VPathNode *pNextNode )
  1757. {
  1758. // Reset Length.
  1759. F32 segmentLength = 0.f;
  1760. // Positions.
  1761. const Point3F &pt0 = pNode->getWorldPosition();
  1762. const Point3F &pt3 = pNextNode->getWorldPosition();
  1763. // Fetch Node Rotation Matrices.
  1764. MatrixF mat0, mat1;
  1765. pNode->getWorldRotation().setMatrix( &mat0 );
  1766. pNextNode->getWorldRotation().setMatrix( &mat1 );
  1767. // Determine Tangent Axis.
  1768. Point3F pt1( gBezierAxis * pNode->getWeight() );
  1769. Point3F pt2( -gBezierAxis * pNextNode->getWeight() );
  1770. // Rotate Axis.
  1771. mat0.mulP( pt1 );
  1772. mat1.mulP( pt2 );
  1773. // Offset Points.
  1774. pt1 += pt0;
  1775. pt2 += pt3;
  1776. // Initial Position.
  1777. Point3F ptA = pt0;
  1778. const F32 i = gBezierInterpStep;
  1779. for ( F32 t = 0.f, it = ( 1.f - t ); t <= 1.f; t += i, it = ( 1.f - t ) )
  1780. {
  1781. // Calculate Position.
  1782. Point3F ptB = ( pt0 * it * it * it ) + ( 3 * pt1 * it * it * t ) + ( 3 * pt2 * it * t * t ) + ( pt3 * t * t * t );
  1783. // Add Segment.
  1784. segmentLength += ( ptB - ptA ).len();
  1785. // Store Position.
  1786. ptA = ptB;
  1787. }
  1788. // Apply Update.
  1789. pNode->setLength( segmentLength );
  1790. }
  1791. Point3F VPath::getAdvancedBezierPathPosition( VPathObject *pPathObject, const F32 &pTargetDistance, F32 &pPathInterpDelta )
  1792. {
  1793. // Fetch Nodes.
  1794. VPathNode *srcNode = mNodeList[pPathObject->getSourceNode()];
  1795. VPathNode *dstNode = mNodeList[pPathObject->getDestinationNode()];
  1796. // Fetch the delta position.
  1797. return getBezierPathPosition( srcNode, dstNode, pPathObject->getPathInterp(), pPathObject->getPosition(), pTargetDistance, pPathObject->isForward(), true, pPathInterpDelta );
  1798. }
  1799. Point3F VPath::getBezierPathPosition( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward, F32 &pPathInterp )
  1800. {
  1801. // Fetch the length of the segment.
  1802. const F32 length = ( pForward ) ? pSourceNode->getLength() : pDestinationNode->getLength();
  1803. // Determine the real interp time for the distance fraction.
  1804. return getBezierPathPosition( pSourceNode, pDestinationNode, 0.f, pSourceNode->getWorldPosition(), ( length * pTimeInterp ), pForward, false, pPathInterp );
  1805. }
  1806. Point3F VPath::getBezierPathPosition( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const Point3F &pReferencePosition, const F32 &pTargetDistance, const bool &pForward, const bool &pRelativeToReference, F32 &pPathInterpDelta )
  1807. {
  1808. // Positions.
  1809. const Point3F &pt0 = pSourceNode->getWorldPosition();
  1810. const Point3F &pt3 = pDestinationNode->getWorldPosition();
  1811. // Fetch Node Rotation Matrices.
  1812. MatrixF mat0, mat1;
  1813. pSourceNode->getWorldRotation().setMatrix( &mat0 );
  1814. pDestinationNode->getWorldRotation().setMatrix( &mat1 );
  1815. // Determine Tangent Axis.
  1816. Point3F pt1( gBezierAxis * pSourceNode->getWeight() );
  1817. Point3F pt2( -gBezierAxis * pDestinationNode->getWeight() );
  1818. if ( !pForward )
  1819. {
  1820. pt1 *= -1.f;
  1821. pt2 *= -1.f;
  1822. }
  1823. // Rotate Axis.
  1824. mat0.mulP( pt1 );
  1825. mat1.mulP( pt2 );
  1826. // Offset Points.
  1827. pt1 += pt0;
  1828. pt2 += pt3;
  1829. // Move Position.
  1830. Point3F movePosition = pReferencePosition;
  1831. // Movement Distance.
  1832. F32 moveDistance = 0.f;
  1833. // Determine the Real Delta.
  1834. const F32 i = gBezierInterpStep;
  1835. for ( F32 t = ( pTimeInterp + i ), it = ( 1.f - t ); t <= 1.f; t += i, it = ( 1.f - t ) )
  1836. {
  1837. // Calculate Step.
  1838. const Point3F stepPosition = ( pt0 * it * it * it ) + ( 3 * pt1 * it * it * t ) + ( 3 * pt2 * it * t * t ) + ( pt3 * t * t * t );
  1839. // Step Length.
  1840. const F32 &stepDistance = ( stepPosition - movePosition ).len();
  1841. if ( pRelativeToReference )
  1842. {
  1843. // Calculate Distance.
  1844. moveDistance = ( pReferencePosition - stepPosition ).len();
  1845. // Moved Target Distance?
  1846. if ( moveDistance >= pTargetDistance )
  1847. {
  1848. // Interpolate Step.
  1849. const F32 stepInterp = ( moveDistance - pTargetDistance ) / moveDistance;
  1850. // Store Interp Delta.
  1851. pPathInterpDelta = ( t - pTimeInterp ) * ( 1.f - stepInterp );
  1852. // Interpolate the step.
  1853. Point3F outPosition;
  1854. outPosition.interpolate( pReferencePosition, stepPosition, ( 1.f - stepInterp ) );
  1855. // Return the position.
  1856. return outPosition;
  1857. }
  1858. }
  1859. else
  1860. {
  1861. // Calculate Distance.
  1862. moveDistance += stepDistance;
  1863. // Moved Target Distance?
  1864. if ( moveDistance >= pTargetDistance )
  1865. {
  1866. // Interpolate Step.
  1867. const F32 stepInterp = ( moveDistance - pTargetDistance ) / stepDistance;
  1868. // Store Interp Delta.
  1869. pPathInterpDelta = ( t - pTimeInterp ) - ( stepInterp * i );
  1870. // Interpolate the step.
  1871. Point3F outPosition;
  1872. outPosition.interpolate( movePosition, stepPosition, ( 1.f - stepInterp ) );
  1873. // Return the position.
  1874. return outPosition;
  1875. }
  1876. }
  1877. // Apply New Position.
  1878. movePosition = stepPosition;
  1879. }
  1880. // Update.
  1881. pPathInterpDelta = ( 1.f - pTimeInterp );
  1882. // At the destination node?
  1883. return pt3;
  1884. }
  1885. VectorF VPath::getBezierPathOrientation( VPathNode *pSourceNode, VPathNode *pDestinationNode, const F32 &pTimeInterp, const bool &pForward )
  1886. {
  1887. // Positions.
  1888. const Point3F &pt0 = pSourceNode->getWorldPosition();
  1889. const Point3F &pt3 = pDestinationNode->getWorldPosition();
  1890. // Fetch Node Rotation Matrices.
  1891. MatrixF mat0, mat1;
  1892. pSourceNode->getWorldRotation().setMatrix( &mat0 );
  1893. pDestinationNode->getWorldRotation().setMatrix( &mat1 );
  1894. // Determine Tangent Axis.
  1895. Point3F pt1( gBezierAxis * pSourceNode->getWeight() );
  1896. Point3F pt2( -gBezierAxis * pDestinationNode->getWeight() );
  1897. if ( !pForward )
  1898. {
  1899. pt1 *= -1.f;
  1900. pt2 *= -1.f;
  1901. }
  1902. // Rotate Axis.
  1903. mat0.mulP( pt1 );
  1904. mat1.mulP( pt2 );
  1905. const F32 halfStep = ( gBezierInterpStep / 2.f );
  1906. if ( ( pTimeInterp - halfStep ) <= 0.f )
  1907. {
  1908. // Orientation From Node Tangent.
  1909. pt1.normalize();
  1910. // Return.
  1911. return pt1;
  1912. }
  1913. else if ( ( pTimeInterp + halfStep ) >= 1.f )
  1914. {
  1915. // Orientation From Node Tangent.
  1916. pt2.normalize();
  1917. // Return.
  1918. return -pt2;
  1919. }
  1920. // Offset Points.
  1921. pt1 += pt0;
  1922. pt2 += pt3;
  1923. // Interp Times.
  1924. const F32 t0 = ( pTimeInterp - halfStep );
  1925. const F32 it0 = ( 1.f - t0 );
  1926. const F32 t1 = ( pTimeInterp + halfStep );
  1927. const F32 it1 = ( 1.f - t1 );
  1928. // Calculate Position.
  1929. Point3F d0 = ( pt0 * it0 * it0 * it0 ) + ( 3 * pt1 * it0 * it0 * t0 ) + ( 3 * pt2 * it0 * t0 * t0 ) + ( pt3 * t0 * t0 * t0 );
  1930. Point3F d1 = ( pt0 * it1 * it1 * it1 ) + ( 3 * pt1 * it1 * it1 * t1 ) + ( 3 * pt2 * it1 * t1 * t1 ) + ( pt3 * t1 * t1 * t1 );
  1931. // Set Orientation.
  1932. Point3F orientation = ( d1 - d0 );
  1933. orientation.normalizeSafe();
  1934. // Return.
  1935. return orientation;
  1936. }
  1937. //-----------------------------------------------------------------------------
  1938. //
  1939. // Path Node Property Methods.
  1940. //
  1941. //-----------------------------------------------------------------------------
  1942. DefineEngineMethod( VPath, getNodeCount, S32, (),, "() - Get the number of nodes in this path.\n"
  1943. "@return Returns the number of nodes." )
  1944. {
  1945. // Return Count.
  1946. return object->getNodeCount();
  1947. }
  1948. S32 VPath::getNodeCount( void )
  1949. {
  1950. // Return the Size of the Node List.
  1951. return mNodeList.size();
  1952. }
  1953. DefineEngineMethod( VPath, getNodeLocalTransform, const char *, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the local transform (local position and rotation) of the given node.\n"
  1954. "@param pNodeIndex The index of the node.\n"
  1955. "@return Returns the transform of the given node." )
  1956. {
  1957. // Fetch Position.
  1958. const Point3F &position = object->getNodeLocalPosition(nodeIndex);
  1959. // Fetch Rotation.
  1960. const QuatF &rotation = object->getNodeLocalRotation(nodeIndex);
  1961. // Angle & Axis.
  1962. AngAxisF aa( rotation );
  1963. // Return Buffer.
  1964. char *buffer = Con::getReturnBuffer( 256 );
  1965. dSprintf( buffer, 128, "%.3g %.3g %.3g %.3g %.3g %.3g %.3g", position.x, position.y, position.z, aa.axis.x, aa.axis.y, aa.axis.z, aa.angle );
  1966. return buffer;
  1967. }
  1968. DefineEngineMethod( VPath, getNodeLocalPosition, Point3F, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the position of the given node.\n"
  1969. "@param pNodeIndex The index of the node.\n"
  1970. " @return Returns the Local Position of the given node." )
  1971. {
  1972. // Fetch Position.
  1973. const Point3F &position = object->getNodeLocalPosition(nodeIndex);
  1974. return position;
  1975. }
  1976. Point3F VPath::getNodeLocalPosition( const S32 &pNodeIndex )
  1977. {
  1978. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  1979. {
  1980. // Woops!
  1981. Con::warnf( "VPath::getNodeLocalPosition() - Invalid Index Specified (%d).", pNodeIndex );
  1982. return Point3F::Zero;
  1983. }
  1984. return mNodeList[pNodeIndex]->getLocalPosition();
  1985. }
  1986. DefineEngineMethod( VPath, getNodeLocalRotation, AngAxisF, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the Local Rotation of the given node.\n"
  1987. "@param pNodeIndex The index of the node.\n"
  1988. "@return Returns the Local Rotation of the given node." )
  1989. {
  1990. // Fetch Rotation.
  1991. const QuatF &rotation = object->getNodeLocalRotation(nodeIndex);
  1992. // Angle & Axis.
  1993. AngAxisF aa( rotation );
  1994. return aa;
  1995. }
  1996. QuatF VPath::getNodeLocalRotation( const S32 &pNodeIndex )
  1997. {
  1998. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  1999. {
  2000. // Woops!
  2001. Con::warnf( "VPath::getNodeLocalRotation() - Invalid Index Specified (%d).", pNodeIndex );
  2002. return QuatF( Point3F::Zero, 0.f );
  2003. }
  2004. return mNodeList[pNodeIndex]->getLocalRotation();
  2005. }
  2006. DefineEngineMethod( VPath, getNodeWorldTransform, TransformF, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the World Transform (position and rotation) of the given node.\n"
  2007. "@param pNodeIndex The index of the node.\n"
  2008. "@return Returns the transform of the given node." )
  2009. {
  2010. // Fetch Position.
  2011. const Point3F &position = object->getNodeWorldPosition(nodeIndex);
  2012. // Fetch Rotation.
  2013. const QuatF &rotation = object->getNodeWorldRotation(nodeIndex);
  2014. // Angle & Axis.
  2015. AngAxisF aa( rotation );
  2016. TransformF trans;
  2017. trans.mPosition = position;
  2018. trans.mOrientation = aa;
  2019. return trans;
  2020. }
  2021. DefineEngineMethod( VPath, getNodeWorldPosition, Point3F, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the position of the given node.\n"
  2022. "@param pNodeIndex The index of the node.\n"
  2023. "@return Returns the World Position of the given node." )
  2024. {
  2025. // Fetch Position.
  2026. const Point3F &position = object->getNodeWorldPosition(nodeIndex);
  2027. return position;
  2028. }
  2029. Point3F VPath::getNodeWorldPosition( const S32 &pNodeIndex )
  2030. {
  2031. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2032. {
  2033. // Woops!
  2034. Con::warnf( "VPath::getNodeWorldPosition() - Invalid Index Specified (%d).", pNodeIndex );
  2035. return Point3F::Zero;
  2036. }
  2037. return mNodeList[pNodeIndex]->getWorldPosition();
  2038. }
  2039. DefineEngineMethod( VPath, getNodeWorldRotation, AngAxisF, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the World Rotation of the given node.\n"
  2040. "@param pNodeIndex The index of the node.\n"
  2041. "@return Returns the World Rotation of the given node." )
  2042. {
  2043. // Fetch Rotation.
  2044. const QuatF &rotation = object->getNodeWorldRotation(nodeIndex);
  2045. // Angle & Axis.
  2046. AngAxisF aa( rotation );
  2047. return aa;
  2048. }
  2049. QuatF VPath::getNodeWorldRotation( const S32 &pNodeIndex )
  2050. {
  2051. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2052. {
  2053. // Woops!
  2054. Con::warnf( "VPath::getNodeWorldRotation() - Invalid Index Specified (%d).", pNodeIndex );
  2055. return QuatF( Point3F::Zero, 0.f );
  2056. }
  2057. return mNodeList[pNodeIndex]->getWorldRotation();
  2058. }
  2059. DefineEngineMethod( VPath, getNodeWeight, F32, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the weight of the given node.\n"
  2060. "@param pNodeIndex The index of the node.\n"
  2061. "@return Returns the weight of the given node." )
  2062. {
  2063. // Fetch Weight.
  2064. return object->getNodeWeight(nodeIndex);
  2065. }
  2066. F32 VPath::getNodeWeight( const S32 &pNodeIndex )
  2067. {
  2068. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2069. {
  2070. // Woops!
  2071. Con::warnf( "VPath::getNodeWeight() - Invalid Index Specified (%d).", pNodeIndex );
  2072. return 0.f;
  2073. }
  2074. return mNodeList[pNodeIndex]->getWeight();
  2075. }
  2076. DefineEngineMethod( VPath, getNodeLength, F32, (S32 nodeIndex), (0), "( int pNodeIndex ) - Get the length of the given node.\n"
  2077. "@param pNodeIndex The index of the node.\n"
  2078. "@return Returns the length of the given node." )
  2079. {
  2080. // Fetch Length.
  2081. return object->getNodeLength( nodeIndex );
  2082. }
  2083. F32 VPath::getNodeLength( const S32 &pNodeIndex )
  2084. {
  2085. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2086. {
  2087. // Woops!
  2088. Con::warnf( "VPath::getNodeLength() - Invalid Index Specified (%d).", pNodeIndex );
  2089. return 0.f;
  2090. }
  2091. return mNodeList[pNodeIndex]->getLength();
  2092. }
  2093. DefineEngineMethod( VPath, setNodeTransform, void, (S32 nodeIndex, TransformF transform), (0, MatrixF::Identity), "( int pNodeIndex, matrix pTransform ) - Set the transform of the given node.\n"
  2094. "@param pNodeIndex The index of the node.\n"
  2095. "@param pTransform The new transform to be applied to the node.\n"
  2096. "@return No return value." )
  2097. {
  2098. // Fetch Position & Rotation.
  2099. Point3F position = transform.mPosition;
  2100. AngAxisF aa = transform.mOrientation;
  2101. QuatF rotation;
  2102. // Set Rotation.
  2103. rotation.set( aa );
  2104. // Apply Update.
  2105. object->setNodePosition( nodeIndex, position );
  2106. object->setNodeRotation( nodeIndex, rotation );
  2107. }
  2108. DefineEngineMethod( VPath, setNodePosition, void, (S32 nodeIndex, Point3F position), (0, Point3F::Zero), "( int pNodeIndex, vector pPosition ) - Set the position of the given node.\n"
  2109. "@param pNodeIndex The index of the node.\n"
  2110. "@param pPosition The new position to be applied to the node.\n"
  2111. "@return No return value." )
  2112. {
  2113. // Apply Update.
  2114. object->setNodePosition( nodeIndex, position );
  2115. }
  2116. void VPath::setNodePosition( const S32 &pNodeIndex, const Point3F &pPosition )
  2117. {
  2118. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2119. {
  2120. // Woops!
  2121. Con::warnf( "VPath::setNodePosition() - Invalid Index Specified (%d).", pNodeIndex );
  2122. return;
  2123. }
  2124. // Fetch Node.
  2125. VPathNode *node = mNodeList[pNodeIndex];
  2126. // Apply Update.
  2127. node->setLocalPosition( pPosition );
  2128. // Update Size.
  2129. updateContainer();
  2130. // Calculate Path.
  2131. calculatePath();
  2132. if ( isServerObject() )
  2133. {
  2134. // Network Flags.
  2135. setMaskBits( NodeUpdateMask );
  2136. }
  2137. }
  2138. DefineEngineMethod( VPath, setNodeRotation, void, (S32 nodeIndex, AngAxisF aa), (0, AngAxisF()), "( int pNodeIndex, angAxis pRotation ) - Set the rotation of the given node.\n"
  2139. "@param pNodeIndex The index of the node.\n"
  2140. "@param pRotation The new rotation to be applied to the node.\n"
  2141. "@return No return value." )
  2142. {
  2143. QuatF rotation;
  2144. // Set Rotation.
  2145. rotation.set( aa );
  2146. // Apply Update.
  2147. object->setNodeRotation( nodeIndex, rotation );
  2148. }
  2149. void VPath::setNodeRotation( const S32 &pNodeIndex, const QuatF &pRotation )
  2150. {
  2151. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2152. {
  2153. // Woops!
  2154. Con::warnf( "VPath::setNodeRotation() - Invalid Index Specified (%d).", pNodeIndex );
  2155. return;
  2156. }
  2157. // Fetch Node.
  2158. VPathNode *node = mNodeList[pNodeIndex];
  2159. // Apply Update.
  2160. node->setLocalRotation( pRotation );
  2161. // Calculate Path.
  2162. calculatePath();
  2163. if ( isServerObject() )
  2164. {
  2165. // Network Flags.
  2166. setMaskBits( NodeUpdateMask );
  2167. }
  2168. }
  2169. DefineEngineMethod( VPath, setNodeWeight, void, (S32 nodeIndex, F32 nodeWeight), (0, 1.0), "( int pNodeIndex, float pWeight ) - Set the weight of the given node.\n"
  2170. "@param pNodeIndex The index of the node.\n"
  2171. "@param pWeight The new weight to be applied to the node.\n"
  2172. "@return No return value." )
  2173. {
  2174. // Apply Update.
  2175. object->setNodeWeight( nodeIndex, nodeWeight );
  2176. }
  2177. void VPath::setNodeWeight( const S32 &pNodeIndex, const F32 &pWeight )
  2178. {
  2179. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2180. {
  2181. // Woops!
  2182. Con::warnf( "VPath::setNodeWeight() - Invalid Index Specified (%d).", pNodeIndex );
  2183. return;
  2184. }
  2185. // Fetch Node.
  2186. VPathNode *node = mNodeList[pNodeIndex];
  2187. // Apply Update.
  2188. node->setWeight( pWeight );
  2189. // Calculate Path.
  2190. calculatePath();
  2191. if ( isServerObject() )
  2192. {
  2193. // Network Flags.
  2194. setMaskBits( NodeUpdateMask );
  2195. }
  2196. }
  2197. DefineEngineMethod( VPath, getNodeOrientationMode, const char *, (S32 nodeIndex), (0), "( int pNodeIndex ) - Gets the current orientation mode of the node.\n"
  2198. "@param pNodeIndex The index of the node.\n"
  2199. "@return Returns a string indicating the orientation mode and its properties." )
  2200. {
  2201. if ( nodeIndex < 0 || nodeIndex >= object->getNodeCount() )
  2202. {
  2203. // Woops!
  2204. Con::warnf( "VPath::getNodeOrientationMode() - Invalid Index Specified (%d).", nodeIndex );
  2205. return "";
  2206. }
  2207. // Fetch Object
  2208. VPathNode *node = object->getNode( nodeIndex );
  2209. // Fetch Orientation Mode.
  2210. const VPathNode::sOrientation &orientation = node->getOrientationMode();
  2211. // Determine the Type.
  2212. StringTableEntry type = VPathNode::getOrientationTypeLabel( orientation.Type );
  2213. // Buffer.
  2214. char *buffer = Con::getReturnBuffer( 128 );
  2215. switch( orientation.Type )
  2216. {
  2217. case VPathNode::k_OrientationFree :
  2218. {
  2219. // Buffer String.
  2220. dSprintf( buffer, 128, "%s", type );
  2221. } break;
  2222. case VPathNode::k_OrientationToPoint:
  2223. {
  2224. // Fetch Point.
  2225. const Point3F &lookAtPoint = orientation.Point;
  2226. // Buffer String.
  2227. dSprintf( buffer, 128, "%s\t%.2f %.2f %.2f", type, lookAtPoint.x, lookAtPoint.y, lookAtPoint.z );
  2228. } break;
  2229. }
  2230. // Return Buffer.
  2231. return buffer;
  2232. }
  2233. DefineEngineStringlyVariadicMethod( VPath, setNodeOrientationMode, void, 4, 5, "( int pNodeIndex, string pOrientationType, [vector pPoint] ) - Set the orientation mode of the node.\n"
  2234. "@param pNodeIndex The index of the node.\n"
  2235. "@param pOrientationType The new orientation type of the object.\n"
  2236. "@param pPoint If the orientation type is set to POINT, this parameter must be a vector.\n"
  2237. "@return No return value." )
  2238. {
  2239. // Fetch Index.
  2240. const S32 nodeIndex = dAtoi( argv[2] );
  2241. // Orient?
  2242. const VPathNode::eOrientationType type = VPathNode::getOrientationTypeEnum( argv[3] );
  2243. switch ( type )
  2244. {
  2245. case VPathNode::k_OrientationFree :
  2246. {
  2247. // Apply Mode.
  2248. object->setNodeOrientationMode( nodeIndex, type );
  2249. } break;
  2250. case VPathNode::k_OrientationToPoint:
  2251. {
  2252. // Fetch Point.
  2253. Point3F lookAtPoint( 0.f, 0.f, 0.f );
  2254. dSscanf( argv[4], "%g %g %g", &lookAtPoint.x, &lookAtPoint.y, &lookAtPoint.z );
  2255. // Apply Mode.
  2256. object->setNodeOrientationMode( nodeIndex, type, lookAtPoint );
  2257. } break;
  2258. default :
  2259. {
  2260. AssertFatal( false, "VPath::setNodeOrientationMode() - Invalid Orientation Mode Specified." );
  2261. } break;
  2262. }
  2263. }
  2264. void VPath::setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType )
  2265. {
  2266. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2267. {
  2268. // Woops!
  2269. Con::warnf( "VPath::setNodeOrientationMode() - Invalid Index Specified (%d).", pNodeIndex );
  2270. return;
  2271. }
  2272. // Fetch Node.
  2273. VPathNode *node = mNodeList[pNodeIndex];
  2274. // Apply.
  2275. node->setOrientationMode( pType );
  2276. // Network Flags.
  2277. setMaskBits( NodeUpdateMask );
  2278. }
  2279. void VPath::setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType, const Point3F pPoint )
  2280. {
  2281. if ( pNodeIndex < 0 || pNodeIndex >= mNodeList.size() )
  2282. {
  2283. // Woops!
  2284. Con::warnf( "VPath::setNodeOrientationMode() - Invalid Index Specified (%d).", pNodeIndex );
  2285. return;
  2286. }
  2287. // Fetch Node.
  2288. VPathNode *node = mNodeList[pNodeIndex];
  2289. // Apply.
  2290. node->setOrientationMode( pType, pPoint );
  2291. // Network Flags.
  2292. setMaskBits( NodeUpdateMask );
  2293. }
  2294. //-----------------------------------------------------------------------------
  2295. //
  2296. // Path Object Property Methods.
  2297. //
  2298. //-----------------------------------------------------------------------------
  2299. DefineEngineMethod( VPath, isPathObjectActive, bool, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Is the object actively traveling around this path?\n"
  2300. "@param pObject The SimObjectID of the object being observed.\n"
  2301. "@return Returns true of the object is active." )
  2302. {
  2303. // Fetch Object.
  2304. if (sceneObject == nullptr)
  2305. {
  2306. Con::errorf( "VPath::isPathObjectActive() - Invalid Target Object." );
  2307. return false;
  2308. }
  2309. // Fetch Object
  2310. VPathObject *pathObject = object->getPathObject( sceneObject );
  2311. // Return.
  2312. return pathObject->isActive();
  2313. }
  2314. DefineEngineMethod( VPath, setPathObjectActive, void, (SceneObject *sceneObject, bool isActive), (nullAsType<SceneObject*>(), true), "( SimObject pObject, bool pActive ) - Enable or disable the object from traveling around this path. Inactive objects are still attached to the path, but are not updated.\n"
  2315. "@param pObject The SimObjectID of the object being altered.\n"
  2316. "@param pActive The new status of the object.\n"
  2317. "@return No return value." )
  2318. {
  2319. // Fetch Object.
  2320. if (sceneObject == nullptr)
  2321. {
  2322. Con::errorf( "VPath::setPathObjectActive() - Invalid Target Object." );
  2323. return;
  2324. }
  2325. // Apply.
  2326. object->setPathObjectActive( sceneObject, isActive);
  2327. }
  2328. void VPath::setPathObjectActive( SceneObject *pObject, const bool &pActive )
  2329. {
  2330. VPathObject *pathObject = getPathObject( pObject );
  2331. if ( !pathObject )
  2332. {
  2333. Con::warnf( "VPath::setPathObjectActive() - Object (%d) Not Attached to Path.", pObject->getId() );
  2334. return;
  2335. }
  2336. // Apply.
  2337. pathObject->setActive( pActive );
  2338. // Network Flags.
  2339. setMaskBits( ObjectUpdateMask );
  2340. }
  2341. DefineEngineMethod( VPath, getPathObjectInterp, F32, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Get the current interp position of the path object.\n"
  2342. "@param pObject The SimObjectID of the object being observed.\n"
  2343. "@return Returns the current interp position." )
  2344. {
  2345. // Fetch Object.
  2346. if (sceneObject == nullptr)
  2347. {
  2348. Con::errorf( "VPath::getPathObjectInterp() - Invalid Target Object." );
  2349. return false;
  2350. }
  2351. // Fetch Object
  2352. VPathObject *pathObject = object->getPathObject( sceneObject );
  2353. // Return.
  2354. return pathObject->getTimeInterp();
  2355. }
  2356. DefineEngineMethod( VPath, setPathObjectInterp, void, (SceneObject *sceneObject, F32 timeInterp), (nullAsType<SceneObject*>(), 1.0), "( SimObject pObject, float pTimeInterp ) - Set the interp position of the object between its current nodes.\n"
  2357. "@param pObject The SimObjectID of the object being altered.\n"
  2358. "@param pTimeInterp The new interp position of the object.\n"
  2359. "@return No return value." )
  2360. {
  2361. // Fetch Object.
  2362. if (sceneObject == nullptr)
  2363. {
  2364. Con::errorf( "VPath::setPathObjectInterp() - Invalid Target Object." );
  2365. return;
  2366. }
  2367. // Apply.
  2368. object->setPathObjectInterp( sceneObject, timeInterp);
  2369. }
  2370. void VPath::setPathObjectInterp( SceneObject *pObject, const F32 &pTimeInterp )
  2371. {
  2372. VPathObject *pathObject = getPathObject( pObject );
  2373. if ( !pathObject )
  2374. {
  2375. Con::warnf( "VPath::setPathObjectInterp() - Object (%d) Not Attached to Path.", pObject->getId() );
  2376. return;
  2377. }
  2378. // Update.
  2379. setPathObjectInterp( pathObject, pTimeInterp );
  2380. }
  2381. void VPath::setPathObjectInterp( VPathObject *pPathObject, const F32 &pTimeInterp )
  2382. {
  2383. // Set Interp Time.
  2384. pPathObject->setTimeInterp( pTimeInterp );
  2385. // Update Position.
  2386. updatePosition( pPathObject );
  2387. // Update Orientation.
  2388. updateOrientation( pPathObject );
  2389. // Reset the delta.
  2390. pPathObject->resetDelta();
  2391. // Set the object transform.
  2392. pPathObject->getObject()->setTransform( pPathObject->getTransform() );
  2393. if ( isServerObject() )
  2394. {
  2395. // Update Objects.
  2396. setMaskBits( ObjectUpdateMask );
  2397. // Update This Object.
  2398. pPathObject->setMaskBits( VPathObject::k_StateUpdatePosition );
  2399. }
  2400. }
  2401. DefineEngineMethod( VPath, getPathObjectOffset, const char *, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Get the position offset assigned to this object.\n"
  2402. "@param pObject The SimObjectID of the object being observed.\n"
  2403. "@return Returns the position offset." )
  2404. {
  2405. // Fetch Object.
  2406. if (sceneObject == nullptr)
  2407. {
  2408. Con::errorf( "VPath::getPathObjectOffset() - Invalid Target Object." );
  2409. return "";
  2410. }
  2411. // Fetch Object
  2412. VPathObject *pathObject = object->getPathObject( sceneObject );
  2413. // Fetch Offset.
  2414. const Point3F &offset = pathObject->getOffset();
  2415. // Buffer.
  2416. char *buffer = Con::getReturnBuffer( 64 );
  2417. dSprintf( buffer, 64, "%f %f %f", offset.x, offset.y, offset.z );
  2418. return buffer;
  2419. }
  2420. DefineEngineMethod( VPath, setPathObjectOffset, void, (SceneObject *sceneObject, Point3F offset), (nullAsType<SceneObject*>(), Point3F::Zero), "( SimObject pObject, vector pOffset ) - Set the position offset of the object. As the object is moving along the path, its position is offset by this value. Setting the \"Relative\" parameter while attaching an object will automatically apply an offset value.\n"
  2421. "@param pObject The SimObjectID of the object being altered.\n"
  2422. "@param pOffset The new position offset of the object.\n"
  2423. "@return No return value." )
  2424. {
  2425. // Fetch Object.
  2426. if (sceneObject == nullptr)
  2427. {
  2428. Con::errorf( "VPath::setPathObjectOffset() - Invalid Target Object." );
  2429. return;
  2430. }
  2431. // Apply.
  2432. object->setPathObjectOffset( sceneObject, offset );
  2433. }
  2434. void VPath::setPathObjectOffset( SceneObject *pObject, const Point3F &pOffset )
  2435. {
  2436. VPathObject *pathObject = getPathObject( pObject );
  2437. if ( !pathObject )
  2438. {
  2439. Con::warnf( "VPath::setPathObjectOffset() - Object (%d) Not Attached to Path.", pObject->getId() );
  2440. return;
  2441. }
  2442. // Apply.
  2443. pathObject->setOffset( pOffset );
  2444. // Network Flags.
  2445. setMaskBits( ObjectUpdateMask );
  2446. }
  2447. DefineEngineMethod( VPath, getPathObjectSpeed, F32, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Get the speed this object is traveling along the path at.\n"
  2448. "@param pObject The SimObjectID of the object being observed.\n"
  2449. "@return Returns the speed of the object." )
  2450. {
  2451. // Fetch Object.
  2452. if (sceneObject == nullptr)
  2453. {
  2454. Con::errorf( "VPath::getPathObjectSpeed() - Invalid Target Object." );
  2455. return false;
  2456. }
  2457. // Fetch Object
  2458. VPathObject *pathObject = object->getPathObject( sceneObject );
  2459. // Return.
  2460. return pathObject->getSpeed();
  2461. }
  2462. DefineEngineMethod( VPath, setPathObjectSpeed, void, (SceneObject *sceneObject, F32 speed), (nullAsType<SceneObject*>(), 1.0), "( SimObject pObject, float pSpeed ) - Set the speed of the object.\n"
  2463. "@param pObject The SimObjectID of the object being altered.\n"
  2464. "@param pSpeed The new speed of the object.\n"
  2465. "@return No return value." )
  2466. {
  2467. // Fetch Object.
  2468. if (sceneObject == nullptr)
  2469. {
  2470. Con::errorf( "VPath::setPathObjectSpeed() - Invalid Target Object." );
  2471. return;
  2472. }
  2473. // Apply.
  2474. object->setPathObjectSpeed( sceneObject, speed );
  2475. }
  2476. void VPath::setPathObjectSpeed( SceneObject *pObject, const F32 &pSpeed )
  2477. {
  2478. VPathObject *pathObject = getPathObject( pObject );
  2479. if ( !pathObject )
  2480. {
  2481. Con::warnf( "VPath::setPathObjectSpeed() - Object (%d) Not Attached to Path.", pObject->getId() );
  2482. return;
  2483. }
  2484. // Apply.
  2485. pathObject->setSpeed( mFabs( pSpeed ) );
  2486. // Network Flags.
  2487. setMaskBits( ObjectUpdateMask );
  2488. }
  2489. DefineEngineMethod( VPath, getPathObjectOrientationMode, const char *, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Gets the current orientation mode of the object.\n"
  2490. "@param pObject The SimObjectID of the object being observed.\n"
  2491. "@return Returns a string indicating the orientation mode and its properties." )
  2492. {
  2493. // Fetch Object.
  2494. if (sceneObject == nullptr)
  2495. {
  2496. Con::errorf( "VPath::getPathObjectOrientationMode() - Invalid Target Object." );
  2497. return "";
  2498. }
  2499. // Fetch Object
  2500. VPathObject *pathObject = object->getPathObject( sceneObject );
  2501. // Fetch Orientation Mode.
  2502. const VPathObject::sOrientation &orientation = pathObject->getOrientationMode();
  2503. // Determine the Type.
  2504. StringTableEntry type = VPathObject::getOrientationTypeLabel( orientation.Type );
  2505. // Buffer.
  2506. char *buffer = Con::getReturnBuffer( 128 );
  2507. switch( orientation.Type )
  2508. {
  2509. case VPathObject::k_OrientationFree :
  2510. case VPathObject::k_OrientationInterpolate :
  2511. case VPathObject::k_OrientationToPath :
  2512. {
  2513. // Buffer String.
  2514. dSprintf( buffer, 128, "%s", type );
  2515. } break;
  2516. case VPathObject::k_OrientationToObject :
  2517. {
  2518. // Fetch the Object ID.
  2519. const S32 objId = ( ( orientation.Object ) ? orientation.Object->getId() : 0 );
  2520. // Buffer String.
  2521. dSprintf( buffer, 128, "%s %d", type, objId );
  2522. } break;
  2523. case VPathObject::k_OrientationToPoint:
  2524. {
  2525. // Fetch Point.
  2526. const Point3F &lookAtPoint = orientation.Point;
  2527. // Buffer String.
  2528. dSprintf( buffer, 128, "%s %f %f %f", type, lookAtPoint.x, lookAtPoint.y, lookAtPoint.z );
  2529. } break;
  2530. }
  2531. // Return Buffer.
  2532. return buffer;
  2533. }
  2534. DefineEngineStringlyVariadicMethod( VPath, setPathObjectOrientationMode, void, 4, 5, "( SimObject pObject, string pOrientationType, [SimObject pObject / vector pPoint] ) - Set the orientation mode of the object. This property affects the rotation of the object. If you wish to ignore the object's rotation altogether, set the mode to \"FREE\".\n"
  2535. "@param pObject The SimObjectID of the object being altered.\n"
  2536. "@param pOrientationType The new orientation type of the object.\n"
  2537. "@param pObject If the orientation type is set to OBJECT, this parameter must be the SimObjectID of a scene object.\n"
  2538. "@param pPoint If the orientation type is set to POINT, this parameter must be a vector.\n"
  2539. "@return No return value." )
  2540. {
  2541. // Fetch Object.
  2542. SceneObject *sceneObject;
  2543. if ( !Sim::findObject( argv[2], sceneObject ) )
  2544. {
  2545. Con::errorf( "VPath::setPathObjectOrientationMode() - Invalid Target Object." );
  2546. return;
  2547. }
  2548. // Orient?
  2549. const VPathObject::eOrientationType type = VPathObject::getOrientationTypeEnum( argv[3] );
  2550. switch ( type )
  2551. {
  2552. case VPathObject::k_OrientationFree :
  2553. case VPathObject::k_OrientationInterpolate :
  2554. case VPathObject::k_OrientationToPath :
  2555. {
  2556. // Apply Mode.
  2557. object->setPathObjectOrientationMode( sceneObject, type );
  2558. } break;
  2559. case VPathObject::k_OrientationToObject :
  2560. {
  2561. // Fetch Object.
  2562. SceneObject *lookAtObject = dynamic_cast<SceneObject*>( Sim::findObject( argv[4] ) );
  2563. if ( !lookAtObject )
  2564. {
  2565. Con::errorf( "VPath::setPathObjectOrientationMode() - Invalid LookAt Object." );
  2566. return;
  2567. }
  2568. // Apply Mode.
  2569. object->setPathObjectOrientationMode( sceneObject, type, lookAtObject );
  2570. } break;
  2571. case VPathObject::k_OrientationToPoint:
  2572. {
  2573. // Fetch Point.
  2574. Point3F lookAtPoint( 0.f, 0.f, 0.f );
  2575. dSscanf( argv[4], "%g %g %g", &lookAtPoint.x, &lookAtPoint.y, &lookAtPoint.z );
  2576. // Apply Mode.
  2577. object->setPathObjectOrientationMode( sceneObject, type, lookAtPoint );
  2578. } break;
  2579. default :
  2580. {
  2581. AssertFatal( false, "VPath::setPathObjectOrientationMode() - Invalid Orientation Mode Specified." );
  2582. } break;
  2583. }
  2584. }
  2585. void VPath::setPathObjectOrientationMode( SceneObject *pObject, const VPathObject::eOrientationType &pType )
  2586. {
  2587. VPathObject *pathObject = getPathObject( pObject );
  2588. if ( !pathObject )
  2589. {
  2590. Con::warnf( "VPath::setPathObjectOrientationMode() - Object (%d) Not Attached to Path.", pObject->getId() );
  2591. return;
  2592. }
  2593. // Apply.
  2594. pathObject->setOrientationMode( pType );
  2595. // Network Flags.
  2596. setMaskBits( ObjectUpdateMask );
  2597. }
  2598. void VPath::setPathObjectOrientationMode( SceneObject *pObject, const VPathObject::eOrientationType &pType, SceneObject *pLookAtObject )
  2599. {
  2600. VPathObject *pathObject = getPathObject( pObject );
  2601. if ( !pathObject )
  2602. {
  2603. Con::warnf( "VPath::setPathObjectOrientationMode() - Object (%d) Not Attached to Path.", pObject->getId() );
  2604. return;
  2605. }
  2606. // Apply.
  2607. pathObject->setOrientationMode( pType, pLookAtObject );
  2608. // Network Flags.
  2609. setMaskBits( ObjectUpdateMask );
  2610. }
  2611. void VPath::setPathObjectOrientationMode( SceneObject *pObject, const VPathObject::eOrientationType &pType, const Point3F pPoint )
  2612. {
  2613. VPathObject *pathObject = getPathObject( pObject );
  2614. if ( !pathObject )
  2615. {
  2616. Con::warnf( "VPath::setPathObjectOrientationMode() - Object (%d) Not Attached to Path.", pObject->getId() );
  2617. return;
  2618. }
  2619. // Apply.
  2620. pathObject->setOrientationMode( pType, pPoint );
  2621. // Network Flags.
  2622. setMaskBits( ObjectUpdateMask );
  2623. }
  2624. DefineEngineMethod( VPath, isPathObjectForward, bool, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Get if this object is traveling forwards along the path.\n"
  2625. "@param pObject The SimObjectID of the object being observed.\n"
  2626. "@return Returns true if the object is traveling forwards." )
  2627. {
  2628. // Fetch Object.
  2629. if (sceneObject == nullptr)
  2630. {
  2631. Con::errorf( "VPath::isPathObjectForward() - Invalid Target Object." );
  2632. return false;
  2633. }
  2634. // Fetch Object
  2635. VPathObject *pathObject = object->getPathObject( sceneObject );
  2636. // Return.
  2637. return pathObject->isForward();
  2638. }
  2639. DefineEngineMethod( VPath, setPathObjectForward, void, (SceneObject *sceneObject, bool forward), (nullAsType<SceneObject*>(), true), "( SimObject pObject, bool pForward ) - Set the travel direction of the object.\n"
  2640. "@param pObject The SimObjectID of the object being altered.\n"
  2641. "@param pForward The direction of the object.\n"
  2642. "@return No return value." )
  2643. {
  2644. // Fetch Object.
  2645. if (sceneObject == nullptr)
  2646. {
  2647. Con::errorf( "VPath::setPathObjectForward() - Invalid Target Object." );
  2648. return;
  2649. }
  2650. // Apply.
  2651. object->setPathObjectForward( sceneObject, forward);
  2652. }
  2653. void VPath::setPathObjectForward( SceneObject *pObject, const bool &pForward )
  2654. {
  2655. VPathObject *pathObject = getPathObject( pObject );
  2656. if ( !pathObject )
  2657. {
  2658. Con::warnf( "VPath::setPathObjectForward() - Object (%d) Not Attached to Path.", pObject->getId() );
  2659. return;
  2660. }
  2661. // Apply.
  2662. pathObject->setForward( pForward );
  2663. // Network Flags.
  2664. setMaskBits( ObjectUpdateMask );
  2665. }
  2666. DefineEngineMethod( VPath, getPathObjectNode, S32, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Gets the last node of the object.\n"
  2667. "@param pObject The SimObjectID of the object being observed.\n"
  2668. "@return Returns the node index." )
  2669. {
  2670. // Fetch Object.
  2671. if (sceneObject == nullptr)
  2672. {
  2673. Con::errorf( "VPath::getPathObjectNode() - Invalid Target Object." );
  2674. return false;
  2675. }
  2676. // Fetch Object
  2677. VPathObject *pathObject = object->getPathObject( sceneObject );
  2678. // Return.
  2679. return pathObject->getSourceNode();
  2680. }
  2681. DefineEngineMethod( VPath, setPathObjectNode, void, (SceneObject *sceneObject, S32 nodeIndex), (nullAsType<SceneObject*>(), 0), "( SimObject pObject, bool pNodeIndex ) - Move the object to the node's position. You may also want to observe the \"setPathObjectInterp\" method.\n"
  2682. "@param pObject The SimObjectID of the object being altered.\n"
  2683. "@param pNodeIndex The index of the node that the object will reposition to.\n"
  2684. "@return No return value." )
  2685. {
  2686. // Fetch Object.
  2687. if (sceneObject == nullptr)
  2688. {
  2689. Con::errorf( "VPath::setPathObjectNode() - Invalid Target Object." );
  2690. return;
  2691. }
  2692. // Apply.
  2693. object->setPathObjectNode( sceneObject, nodeIndex);
  2694. }
  2695. void VPath::setPathObjectNode( SceneObject *pObject, const S32 &pNodeIndex )
  2696. {
  2697. VPathObject *pathObject = getPathObject( pObject );
  2698. if ( !pathObject )
  2699. {
  2700. Con::warnf( "VPath::setPathObjectNode() - Object (%d) Not Attached to Path.", pObject->getId() );
  2701. return;
  2702. }
  2703. // Source & Destination Nodes.
  2704. const S32 srcNode = pNodeIndex;
  2705. const S32 dstNode = ( pathObject->isForward() ) ? ( pNodeIndex + 1 ) : ( pNodeIndex - 1 );
  2706. // Set Current Node.
  2707. pathObject->setNode( normalizeNodeIndex( srcNode ), normalizeNodeIndex( dstNode ) );
  2708. // Reset Interp.
  2709. pathObject->setTimeInterp( 0.f );
  2710. pathObject->setPathInterp( 0.f );
  2711. // Network Flags.
  2712. setMaskBits( ObjectUpdateMask );
  2713. }
  2714. DefineEngineMethod( VPath, getPathObjectEndNode, S32, (SceneObject *sceneObject), (nullAsType<SceneObject*>()), "( SimObject pObject ) - Get the index of the node this object is meant to stop upon reaching.\n"
  2715. "@param pObject The SimObjectID of the object being observed.\n"
  2716. "@return Returns the node index." )
  2717. {
  2718. // Fetch Object.
  2719. if (sceneObject == nullptr)
  2720. {
  2721. Con::errorf( "VPath::getPathObjectEndNode() - Invalid Target Object." );
  2722. return false;
  2723. }
  2724. // Fetch Object
  2725. VPathObject *pathObject = object->getPathObject( sceneObject );
  2726. // Return.
  2727. return pathObject->getEndNode();
  2728. }
  2729. DefineEngineMethod( VPath, setPathObjectEndNode, void, (SceneObject *sceneObject, S32 nodeIndex), (nullAsType<SceneObject*>(), 0), "( SimObject pObject, bool pNodeIndex ) - Set end node of the path object. If a value of \"-1\" is applied, the object will path indefinitely.\n"
  2730. "@param pObject The SimObjectID of the object being altered.\n"
  2731. "@param pNodeIndex The index of the node that the object will cease pathing upon reaching.\n"
  2732. "@return No return value." )
  2733. {
  2734. // Fetch Object.
  2735. if (sceneObject == nullptr)
  2736. {
  2737. Con::errorf( "VPath::setPathObjectEndNode() - Invalid Target Object." );
  2738. return;
  2739. }
  2740. // Apply.
  2741. object->setPathObjectEndNode( sceneObject, nodeIndex);
  2742. }
  2743. void VPath::setPathObjectEndNode( SceneObject *pObject, const S32 &pNodeIndex )
  2744. {
  2745. VPathObject *pathObject = getPathObject( pObject );
  2746. if ( !pathObject )
  2747. {
  2748. Con::warnf( "VPath::setPathObjectEndNode() - Object (%d) Not Attached to Path.", pObject->getId() );
  2749. return;
  2750. }
  2751. // Set index.
  2752. S32 index = pNodeIndex;
  2753. if ( index != -1 )
  2754. {
  2755. // Normalize index.
  2756. normalizeNodeIndex( index );
  2757. }
  2758. // Apply.
  2759. pathObject->setEndNode( index );
  2760. // Network Flags.
  2761. setMaskBits( ObjectUpdateMask );
  2762. }