Fast - Slow Movement Transition

Started by
3 comments, last by sckoobs 19 years, 5 months ago
I'm trying to find a way to move an object accross the screen smoothly from right to left with a start point of SCREEN_RESOLUTION_X and an end point of 0 but I want to move it fast at first and then slower as it reaches its destination, similar to:

x -= x / 2
but dividing by two means I'll never reach 0 plus I think it will move perhaps too fast at first. Has anyone imiplemented anything like this or have any suggestions? Any help is much appreciated!
Advertisement
You currently have the position of the object in x. Since you want to move it fast at first and then slower as it reaches its destination, you also need to store its speed, say s.

Try the following:
#define INITIAL_SPEED 10.0f;#define DECR_SPEED    0.99f;while(x > 0.0f){   x = x - s;            // Move the object by its speed   s = s * DECR_SPEED;   // Decrease the speed}

Of course, you'll need to tweak the values of INITIAL_SPEED and DECR_SPEED to get the desired results.

Hope this helps. :)

[Edited by - FrancisXavier on October 30, 2004 8:07:53 AM]
I suspect this is overkill for what you want to do, but the Game Programming Gems 4 book has got a really usefull snippet in it for "critically smoothed damping". This provides a time-step independant way of going from one value to another smoothly, including a smooth transition at the start and end.

My version of this (hmmm does this infringe copyright? Hope not!)

  template <typename T>  inline void SmoothCD(T &val,          // in/out: value to be smoothed                       T &valRate,      // in/out: rate of change of the value                       const tScalar timeDelta, // in: time interval                       const T &to,     // in: the target value                       const tScalar smoothTime)// in: timescale for smoothing  {    if (smoothTime > 0.0f)    {      tScalar omega = 2.0f / smoothTime;      tScalar x = omega * timeDelta;      tScalar exp = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);      T change = val - to;      T temp = (valRate + omega * change) * timeDelta;      valRate = (valRate - omega * temp) * exp;      val = to + (change + temp) * exp;    }    else if (timeDelta > 0.0f)    {      valRate = (to - val) / timeDelta;      val = to;    }    else    {      val = to;      valRate -= valRate; // zero it...    }  }

Or you could use f(t)=(1-t)3*a+3*(1-t)2*t*b+3*(1-t)*t2*d+t3 for 0<=t<=1. With that f(0)=a, f(1)=d, f'(0)=3(b-a), f'(1)=3(d-c). That in case you don't really care about the physics involved and simply want a curve in some general shape.
Keys to success: Ability, ambition and opportunity.
Thanks for the help guys, I went with this rather simple solution:

  float g_fBoardX = SCRN_WIDTH;  if(g_fBoardX < 1.0f)    g_fBoardX = 0.0f;  if(g_fBoardX > 0.0f)    g_fBoardX -= g_fBoardX / 10;

This topic is closed to new replies.

Advertisement