Torque3D Documentation / _generateds / VMotionEvent.cpp

VMotionEvent.cpp

Engine/source/Verve/Extension/Motion/VMotionEvent.cpp

More...

Detailed Description

Public Functions

IMPLEMENT_CONOBJECT(VMotionEvent )

  1
  2//-----------------------------------------------------------------------------
  3// Verve
  4// Copyright (C) 2014 - Violent Tulip
  5//
  6// Permission is hereby granted, free of charge, to any person obtaining a copy
  7// of this software and associated documentation files (the "Software"), to
  8// deal in the Software without restriction, including without limitation the
  9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10// sell copies of the Software, and to permit persons to whom the Software is
 11// furnished to do so, subject to the following conditions:
 12//
 13// The above copyright notice and this permission notice shall be included in
 14// all copies or substantial portions of the Software.
 15//
 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 22// IN THE SOFTWARE.
 23//-----------------------------------------------------------------------------
 24#include "Verve/Core/VController.h"
 25#include "Verve/Core/VGroup.h"
 26#include "Verve/Extension/Motion/VMotionEvent.h"
 27#include "Verve/Extension/Motion/VMotionTrack.h"
 28
 29#include "console/consoleTypes.h"
 30#include "math/mMathFn.h"
 31
 32//-----------------------------------------------------------------------------
 33IMPLEMENT_CONOBJECT( VMotionEvent );
 34//-----------------------------------------------------------------------------
 35
 36VMotionEvent::VMotionEvent( void )
 37{
 38    setLabel( "MotionEvent" );
 39}
 40
 41//-----------------------------------------------------------------------------
 42//
 43// Callback Methods.
 44//
 45//-----------------------------------------------------------------------------
 46
 47//-----------------------------------------------------------------------------
 48// 
 49// VMotionEvent::onTrigger( pDelta, pDelta );
 50// 
 51// The path object is told to move to the next node. If this event corresponds
 52// to Node 0, the object will move to Node 1. If the object reaches the node
 53// before the next event is triggered, then the object will stop moving.
 54// 
 55// The object's position is only reset when the track is reset and not when an
 56// event is triggered.
 57// 
 58//-----------------------------------------------------------------------------
 59void VMotionEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
 60{
 61    Parent::onTrigger( pTime, pDelta );
 62
 63    // Fetch Parent Track.
 64    VMotionTrack *track;
 65    if ( !getTrack( track ) )
 66    {
 67        // Invalid Track.
 68        return;
 69    }
 70
 71    // Fetch Path & Reference Object.
 72    VTorque::PathObjectType  *path   = track->getPath();
 73    VTorque::SceneObjectType *object = getSceneObject();
 74    if ( !path || !object )
 75    {
 76        // Invalid.
 77        return;
 78    }
 79
 80    // Valid Destination Node?
 81    if ( !isControllerLooping() && !getNextEvent() )
 82    {
 83        // Clear Active.
 84        VTorque::setPathObjectActive( path, object, false );
 85        // Quit.
 86        return;
 87    }
 88
 89    // Set Active.
 90    VTorque::setPathObjectActive( path, object, true );
 91
 92    // Apply Speed.
 93    VTorque::setPathObjectSpeed( path, object, getObjectSpeed() );
 94}
 95
 96//-----------------------------------------------------------------------------
 97//
 98// Reference Methods.
 99//
100//-----------------------------------------------------------------------------
101
102//-----------------------------------------------------------------------------
103//
104// VMotionTrack::getPath();
105// 
106// Returns the path that this track is referencing.
107//
108//-----------------------------------------------------------------------------
109VTorque::PathObjectType *VMotionEvent::getPath( void )
110{
111    // Fetch Track.
112    VMotionTrack *track;
113    if ( !getTrack( track ) )
114    {
115        // Invalid.
116        return NULL;
117    }
118
119    // Return Path.
120    return track->getPath();
121}
122
123//-----------------------------------------------------------------------------
124//
125// VMotionTrack::getObjectSpeed();
126// 
127// Determine the Speed that an object must move at to travel over the segment
128// length of the Path.
129//
130//-----------------------------------------------------------------------------
131F32 VMotionEvent::getObjectSpeed( void )
132{
133    // Fetch Parent Track.
134    VMotionTrack *track;
135    if ( !getTrack( track ) )
136    {
137        // Invalid Track.
138        return 0.f;
139    }
140
141    // Fetch Path & Reference Object.
142    VTorque::PathObjectType  *path   = track->getPath();
143    VTorque::SceneObjectType *object = getSceneObject();
144    if ( !path || !object )
145    {
146        // Invalid Object(s).
147        return 0.f;
148    }
149
150    // Fetch Node Index.
151    const S32 &srcNodeIndex = getNodeIndex( ( isControllerPlayingForward() ) ? 0 : -1 );
152
153    // Fetch the Next Event.
154    VEvent *nextEvent = getNextEvent();
155
156    // Valid Destination Node?
157    if ( !isControllerLooping() && !nextEvent )
158    {
159        // No Next Node.
160        return 0.f;
161    }
162
163    // Valid Next Node?
164    if ( nextEvent )
165    {
166        // Fetch Segment Length & Duration.
167        const F32 &length   = VTorque::getPathNodeLength( path, srcNodeIndex );
168        const F32 &duration = mAbs( getTriggerTime() - nextEvent->getTriggerTime() );
169
170        // Speed = Distance / Duration.
171        return ( length / ( duration / 1000.f ) );
172    }
173
174    // Playing Forwards?
175    if ( isControllerPlayingForward() )
176    {
177        // Fetch the First Event.
178        VEvent *firstEvent = dynamic_cast<VEvent*>( track->getChild() );
179
180        // Fetch Segment Length & Duration.
181        const F32 &length   = VTorque::getPathNodeLength( path, srcNodeIndex );
182        const F32 &duration = ( getControllerDuration() - getTriggerTime() ) + firstEvent->getTriggerTime();
183
184        // Speed = Distance / Duration.
185        return ( length / ( duration / 1000.f ) );
186    }
187
188    // Fetch the Last Event.
189    VEvent *lastEvent = dynamic_cast<VEvent*>( track->getLastChild() );
190
191    // Fetch Segment Length & Duration.
192    const F32 &length   = VTorque::getPathNodeLength( path, srcNodeIndex );
193    const F32 &duration = ( getControllerDuration() - lastEvent->getTriggerTime() ) + getTriggerTime();
194
195    // Speed = Distance / Duration.
196    return ( length / ( duration / 1000.f ) );
197}
198
199//-----------------------------------------------------------------------------
200// 
201// VMotionEvent::getNodeIndex( pDelta );
202// 
203// Returns the index of the path node associated with this event object.
204// 
205//-----------------------------------------------------------------------------
206S32 VMotionEvent::getNodeIndex( const S32 &pDelta )
207{
208    // Fetch Event Count.
209    const S32 eventCount = ( ( VTreeNode* )getParent() )->size();
210
211    // Return Index.
212    return ( getIndex() + pDelta ) % eventCount;
213}
214