   Smooth time based transformation
  Related discussion in this post about using timers, and time of last frame opposed to a longer time period.So, I have changed up my timer class and am trying to get rid of spots in my code that used time of last frame.I can't do this...//----------------------------------------------------------------------------void TestApp::HandleKeyboardInput(){   const double cameraRotationSpeed =   0.50;  // 360 degree rotations per second   const double cameraStrafeSpeed   =  50.00;  // Units per second   const double cameraFowardSpeed   = 100.00;  // units per second   // W   { static Timer wTimer;    if( m_keystates[0x57] ) {    if( !wTimer.IsStarted() )    {  wTimer.Start();    }    else    {  float distance = static_cast<float>(cameraStrafeSpeed   * wTimer.GetElapsedTime());  m_camera->ApplyTranslation( distance, D3DXVECTOR3( 0, 1, 0), true);    } } else if( wTimer.IsStarted() ) {    wTimer.Stop(); }   }// SNIP other keysBecause, I am translating by distance at t1 + distance at t2 + distance at t3, etc. Instead of translation = disance at t1, then translation = distance at t2...I think I need some kind of method like slerp is for rotation, but for all possible transformations, including translate, rotate, and scale. I need to interpolate between to positions I guess. But, even if I could do that for translation and scale, what am I going to do if:1) I am interpolating between two positions based on movement along the x axis2) The the user hits a key and I need to also interpolate between two points on the y axis at the same time3) or I'm in the middle of 1 somewhere and 2 occurs. 4) Or worse, when I am using joystick, and have any number of axi to deal with.It was very simply when I just translated based on the last frame time, but like it is discussed in the linked post, that could lead to accumation of errors, since the timer will have a small error each time you take a time delta.How do you guys handle this?   
  I was thinking of adding something like this source and derive meshes and cameras from it.Keep in mind this is for transformation due to user input or AI, not from keyframes exported from a modeler.#ifndef TRANSFORMEX_H#define TRANSFORMEX_H#include "Graphics/3D/Transformable.h"// Common Lib Includes#include "Timer.h"//--------------------------------------------------------------------------------class TransformEx : public Transformable{public:   /**   *   **/   TransformEx(); /**   *   **/   virtual ~TransformEx();   /**   *   **/   void Translate(D3DXVECTOR3 unitsPerSecond, bool start);   /**   *   **/   void Rotate(D3DXVECTOR3 radiansPerSecond, bool start);   /**   *   **/   void Scale(D3DXVECTOR3 unitsPerSecond, bool start);protected:   common_lib_cpisz::Timer m_timer_translation;   common_lib_cpisz::Timer m_timer_rotation;   common_lib_cpisz::Timer m_timer_scale;   D3DXVECTOR3 m_translation_unitsPerSecond;   D3DXVECTOR3 m_rotation_radiansPerSecond;   D3DXVECTOR3 m_scale_unitsPerSecond;};#endifThen I'd have to, for example, add 1 to the x component of the unitsPerSecond argument of Translation when the key is pressed, and subtract 1 when it is let go. That blows using an array of keystates out the window and I also wonder if floating point error is going to leave it slightly moving in one direction or another.I am not sure if this is a good idea or not.Especially when using floats for various angles. There is probably still alot of floating point error that is going to occur and accumulate.My main concern is implementing a way to transform objects both from user input and from AI, in such a way that AI can predict foward in time without so much error accumulation that it wouldn't be able to ...for example calculate an angle of fire to hit an object moving in direction x at y units per second.I also worry about, telling the user, hey your ship rotates at a rate of 10 rotations per minute, then them coming back after errors accumulate and say,"It rotated 50 and 1/2 times in 5 minutes.Using start and end time along with interpolation is easy enough for a static animation, but this is hurting my brain for transformations that can change every frame based on input and AI.;  I came up with this for translation based on time//--------------------------------------------------------------------------------void TransformEx::UpdatePosition(){   // Get the time since translation velocity has changed   double elapsedTime = m_timer.GetElapsedTime() - m_startTimeTranslation; // Calculate the position   D3DXVECTOR3 change = m_velocityTranslation * static_cast<float>(elapsedTime);   m_position = m_startPosition + change;}Easy enough.Not sure if rotations will be so easy, but anyway.I kind of wanted to have acceleration too, for ships and the like. I figure everything should have an acceleration. If I want instant movement at a velocity, I'd just call my methods with acceleration = numerical_limits<float>::max()   // Calculate the translation velocity X component   double elapsedTime = m_timer.GetElapsedTime() - m_startTime_translation;   D3DXVECTOR3 translationVelocity;   if( m_startTranslationVelocity.x < m_targetTranslationVelocity.x )   { float change = m_accelerationTranslation.x * elapsedTime; translationVelocity.x = m_startTranslationVelocity.x + change; if( translationVelocity.x > m_targetTranslationVelocity.x ) {    translationVelocity.x = m_targetTranslationVelocity.x; }   }   // Modify position by velocity?This doesn't work, because acceration isn't really behaving the way acceleration should. I am just moving a position by an amount that is modified every frame, from the start position. Real acceleration would be moving the object in increments that grow larger, until that increment is at a maximum, every frame.Well, that is incremental. That induces the timer accuracy errors that we are trying to avoid. Not to mention my frame time is faster than the precision of the timer. I tested and spit out times to a log every frame, and my frame time is < 1ms, at least for now.How would you handle acceleration?   ;  After playing with this for a few days I've come to the conclusion that it is impossible to implement movement or rotation of game objects, in response to player input or AI, based on anything but the last frame time.Doing movement or rotation based on a (current time - start time) seems just fine for keyframed animations, or for constant translations and rotations. I've seen plenty of examples where a demo uses this for say a tire that continually spins in one direction relative to world space and never changes for the entire demo.However, if you ever want to move or rotate, relative to the current position or orientation, there is no conceivable way.Consider a simple input loop wherePlayer hits 'A' he rotates his ship in object space to the leftPlayer hits 'W' he rotates his ship in object space upwardI haven't seen one example in any book or any website where they haven't done this with the last frame time, or a locked time interval such as 30 ms. Does anyone have an example to do these transforms based on (current time - start time)?The rotations are relative to the current orientation, so you have to incrementally update the current orientation before you can perform the new rotation!So, all in all I've come to the conclusion that:I can use (current time - start time) for static animations, or translations and rotation where the position and orientation can be _predicted_ relative to world space. However, it cannot be used at all for transformations relative to object space, such as the mentioned transforms based on user input.For user input, I see no other way, but to use the last frame time, or lock updates to a time interval such as 30ms. All the XNA examples seem to do this, all my books seem to do this. Someone correct me and give an example if I am wrong please!   