VMotionEvent.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "Verve/Core/VController.h"
  24. #include "Verve/Core/VGroup.h"
  25. #include "Verve/Extension/Motion/VMotionEvent.h"
  26. #include "Verve/Extension/Motion/VMotionTrack.h"
  27. #include "console/consoleTypes.h"
  28. #include "math/mMathFn.h"
  29. //-----------------------------------------------------------------------------
  30. IMPLEMENT_CONOBJECT( VMotionEvent );
  31. //-----------------------------------------------------------------------------
  32. VMotionEvent::VMotionEvent( void )
  33. {
  34. setLabel( "MotionEvent" );
  35. }
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Callback Methods.
  39. //
  40. //-----------------------------------------------------------------------------
  41. //-----------------------------------------------------------------------------
  42. //
  43. // VMotionEvent::onTrigger( pDelta, pDelta );
  44. //
  45. // The path object is told to move to the next node. If this event corresponds
  46. // to Node 0, the object will move to Node 1. If the object reaches the node
  47. // before the next event is triggered, then the object will stop moving.
  48. //
  49. // The object's position is only reset when the track is reset and not when an
  50. // event is triggered.
  51. //
  52. //-----------------------------------------------------------------------------
  53. void VMotionEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
  54. {
  55. Parent::onTrigger( pTime, pDelta );
  56. // Fetch Parent Track.
  57. VMotionTrack *track;
  58. if ( !getTrack( track ) )
  59. {
  60. // Invalid Track.
  61. return;
  62. }
  63. // Fetch Path & Reference Object.
  64. VTorque::PathObjectType *path = track->getPath();
  65. VTorque::SceneObjectType *object = getSceneObject();
  66. if ( !path || !object )
  67. {
  68. // Invalid.
  69. return;
  70. }
  71. // Valid Destination Node?
  72. if ( !isControllerLooping() && !getNextEvent() )
  73. {
  74. // Clear Active.
  75. VTorque::setPathObjectActive( path, object, false );
  76. // Quit.
  77. return;
  78. }
  79. // Set Active.
  80. VTorque::setPathObjectActive( path, object, true );
  81. // Apply Speed.
  82. VTorque::setPathObjectSpeed( path, object, getObjectSpeed() );
  83. }
  84. //-----------------------------------------------------------------------------
  85. //
  86. // Reference Methods.
  87. //
  88. //-----------------------------------------------------------------------------
  89. //-----------------------------------------------------------------------------
  90. //
  91. // VMotionTrack::getPath();
  92. //
  93. // Returns the path that this track is referencing.
  94. //
  95. //-----------------------------------------------------------------------------
  96. VTorque::PathObjectType *VMotionEvent::getPath( void )
  97. {
  98. // Fetch Track.
  99. VMotionTrack *track;
  100. if ( !getTrack( track ) )
  101. {
  102. // Invalid.
  103. return NULL;
  104. }
  105. // Return Path.
  106. return track->getPath();
  107. }
  108. //-----------------------------------------------------------------------------
  109. //
  110. // VMotionTrack::getObjectSpeed();
  111. //
  112. // Determine the Speed that an object must move at to travel over the segment
  113. // length of the Path.
  114. //
  115. //-----------------------------------------------------------------------------
  116. F32 VMotionEvent::getObjectSpeed( void )
  117. {
  118. // Fetch Parent Track.
  119. VMotionTrack *track;
  120. if ( !getTrack( track ) )
  121. {
  122. // Invalid Track.
  123. return 0.f;
  124. }
  125. // Fetch Path & Reference Object.
  126. VTorque::PathObjectType *path = track->getPath();
  127. VTorque::SceneObjectType *object = getSceneObject();
  128. if ( !path || !object )
  129. {
  130. // Invalid Object(s).
  131. return 0.f;
  132. }
  133. // Fetch Node Index.
  134. const S32 &srcNodeIndex = getNodeIndex( ( isControllerPlayingForward() ) ? 0 : -1 );
  135. // Fetch the Next Event.
  136. VEvent *nextEvent = getNextEvent();
  137. // Valid Destination Node?
  138. if ( !isControllerLooping() && !nextEvent )
  139. {
  140. // No Next Node.
  141. return 0.f;
  142. }
  143. // Valid Next Node?
  144. if ( nextEvent )
  145. {
  146. // Fetch Segment Length & Duration.
  147. const F32 &length = VTorque::getPathNodeLength( path, srcNodeIndex );
  148. const F32 &duration = mAbs( getTriggerTime() - nextEvent->getTriggerTime() );
  149. // Speed = Distance / Duration.
  150. return ( length / ( duration / 1000.f ) );
  151. }
  152. // Playing Forwards?
  153. if ( isControllerPlayingForward() )
  154. {
  155. // Fetch the First Event.
  156. VEvent *firstEvent = dynamic_cast<VEvent*>( track->getChild() );
  157. // Fetch Segment Length & Duration.
  158. const F32 &length = VTorque::getPathNodeLength( path, srcNodeIndex );
  159. const F32 &duration = ( getControllerDuration() - getTriggerTime() ) + firstEvent->getTriggerTime();
  160. // Speed = Distance / Duration.
  161. return ( length / ( duration / 1000.f ) );
  162. }
  163. // Fetch the Last Event.
  164. VEvent *lastEvent = dynamic_cast<VEvent*>( track->getLastChild() );
  165. // Fetch Segment Length & Duration.
  166. const F32 &length = VTorque::getPathNodeLength( path, srcNodeIndex );
  167. const F32 &duration = ( getControllerDuration() - lastEvent->getTriggerTime() ) + getTriggerTime();
  168. // Speed = Distance / Duration.
  169. return ( length / ( duration / 1000.f ) );
  170. }
  171. //-----------------------------------------------------------------------------
  172. //
  173. // VMotionEvent::getNodeIndex( pDelta );
  174. //
  175. // Returns the index of the path node associated with this event object.
  176. //
  177. //-----------------------------------------------------------------------------
  178. S32 VMotionEvent::getNodeIndex( const S32 &pDelta )
  179. {
  180. // Fetch Event Count.
  181. const S32 eventCount = ( ( VTreeNode* )getParent() )->size();
  182. // Return Index.
  183. return ( getIndex() + pDelta ) % eventCount;
  184. }