   Having an object accelerate
  Hi guys. I want to make a Pong-like game and I want my paddle to accelerate en deccelerate instead of just having it go at its top speed right as it moves. How do you guys think I should implement this ? I have a fairly decent knowledge of maths and physics but I don't know much how to translate it to C++. What I want to do is to accelerate (positively or negatively depending on what key was pressed) and when it reaches its top speed stop accelerating. Here's what I have so far; I guess most of the work has to be done inside the Render() function:/*====================================================================   STRUCT POINT  This structure holds a coordinate point and represents the    top-left corner of each Sprite object.   CLASS VECTOR_2D    The Vector_2D class is a class that the programmer does not need    to deal with. It represents a mathematical/physical of which the    length represents the speed(includes direction) of a Sprite object.    ====================================================================*/struct Point {    int x, y;   Point() : x(0), y(0) { }    Point& operator+=(const Vector_2D&);    Point& operator-=(const Vector_2D&);    Point& operator=(const Point&);    };class Vector_2D {    public:   Vector_2D();   Vector_2D(int, int);   Vector_2D(const Vector_2D&); int GetLength();   int GetX();   int GetY();   void SetX(int);   void SetY(int);   void SetVec(int, int); operator int();   Vector_2D& operator=(const Vector_2D&);   Vector_2D& operator+=(const Vector_2D&);   Vector_2D& operator-=(const Vector_2D&);   bool operator==(const Vector_2D&);   bool operator!=(const Vector_2D&);   friend Point& Point::operator+=(const Vector_2D&);   friend Point& Point::operator-=(const Vector_2D&);   friend Vector_2D operator*(int, Vector_2D&);    private:   int i, j;};/*====================================================================   CLASS SPRITE    The Sprite class regroups a set of various operations to allow    the programmer to deal easily with bitmap, png or jpeg image    loading and move the image. All these operations are done    internally, through the interface.    ====================================================================*/class Sprite {    public:   Sprite();   Sprite(HDC, const char*, int, int);   Sprite(const Sprite&);   ~Sprite(); void Create(HDC, const char*, int, int);   void KillSelf(); void Render(HDC, KEY_STATE);    private:   HDC device_context;   HBITMAP default_bmp;   HBITMAP loaded_bmp;   int width;   int height;   Point position;};/*====================================================================   STRUCT POINT  - Provide overriden operators to update easily each Sprite object position.   CLASS VECTOR_2D    - Provide overriden operators to simulate a mathematical vector;    - Edit i or j parameter through SetXSpeed and SetYSpeed;    - Use SetSpeed if both i and j need to be modified.====================================================================*/Point& Point::operator+=(const Vector_2D& v) {    x += v.i;    y += v.j;    return (*this);}Point& Point::operator-=(const Vector_2D& v) {    x -= v.i;    y -= v.j;    return (*this);}Point& Point::operator=(const Point& pt) {    x = pt.x;    y = pt.y;}Vector_2D::Vector_2D() : i(0), j(0) { }Vector_2D::Vector_2D(int x, int y) : i(x), j(y) { }Vector_2D::Vector_2D(const Vector_2D& v) : i(v.i), j(v.j) { }int Vector_2D::GetLength() { return (int)sqrt(pow(i, 2) + pow(j, 2)); }int Vector_2D::GetX() { return i; }int Vector_2D::GetY() { return j; }void Vector_2D::SetX(int x) { i = x; }void Vector_2D::SetY(int y) { j = y; }void Vector_2D::SetVec(int x, int y) { i = x; j = y; }Vector_2D::operator int() {    // A conversion to an int returns    // the length of the vector    return GetLength();}Vector_2D& Vector_2D::operator=(const Vector_2D& v) {    i = v.i;    j = v.j;    return (*this);}Vector_2D& Vector_2D::operator+=(const Vector_2D& v) {    i += v.i;    j += v.j;    return (*this);}Vector_2D& Vector_2D::operator-=(const Vector_2D& v) {    i -= v.i;    j -= v.j;    return (*this);}bool Vector_2D::operator==(const Vector_2D& v) { return (i == v.i && j == v.j); }bool Vector_2D::operator!=(const Vector_2D& v) { return !(i == v.i && j == v.j); }/*====================================================================   CLASS SPRITE    - Provide the create() method as an alternative to the constructor;    - Automatically loads and set up the image;    - Set the speed of the sprite through SetXSpeed, SetYSpeed and SetSpeed.====================================================================*/Sprite::Sprite() : width(0), height(0) { position.x = position.y = 0; }Sprite::Sprite(HDC hdc, const char* file, int x, int y) {    Create(hdc, file, x, y);}Sprite::Sprite(const Sprite& sprite) {    // Initialize all variables to the copy's    device_context = sprite.device_context;    default_bmp = sprite.default_bmp;    loaded_bmp = sprite.loaded_bmp;    width = sprite.width;    height = sprite.height;    position = sprite.position;}Sprite::~Sprite() {    KillSelf();}void Sprite::Create(HDC hdc, const char* file, int x, int y) {    // Load the bitmap into the compatible device context    device_context = CreateCompatibleDC(hdc);    loaded_bmp = (HBITMAP)LoadImage(0, file, IMAGE_BITMAP, x, y, LR_LOADFROMFILE);    default_bmp = (HBITMAP)SelectObject(device_context, loaded_bmp);   // Retrieve the height and width    BITMAP bmp;    GetObject(loaded_bmp, sizeof(bmp), &bmp);    width = bmp.bmWidth;    height = bmp.bmHeight;   position.x = 0;    position.y = 0;}void Sprite::KillSelf() {    // Release resources    if(default_bmp != NULL) SelectObject(device_context, default_bmp);    if(device_context != NULL) DeleteDC(device_context);}void Sprite::Render(HDC hdc, KEY_STATE state) {    // TODO : Add physics    BitBlt(hdc, position.x, position.y, width, height, device_context, 0, 0, SRCCOPY);}Any idea would be welcome.Thanks.   
  A simple solution would be to keep track of the following quantities throughout the game:- Current position (obviously),- Current speed (positive = rightwards, negative = leftwards),- Acceleration, and- Max speed (i.e., max value for absolute value of speed).Then, do something along the lines of this pseudocode:// -- Update speedif (rightArrowDown)currSpeed += acceleration;else if (leftArrowDown)currSpeed -= acceleration;// -- Ensure validity of new speed valueif (currSpeed < -maxSpeed)currSpeed = -maxSpeed;else if (currSpeed > maxSpeed)currSpeed = maxSpeed;// -- Update positionpos.x += currSpeed; // currSpeed only affects motion on the x-axis.HTH,nilkn   ;  I had tried a much more complicated approach but I realised that it was too complicated uselessly. I believe your solution should fit my needs. Thank you :)   ;  Upon directional button press, apply a force F to your pad in the appropriate direction. If the pad's velocity is smaller than the allowed maximum, use the equation 'DeltaV == F*t/mass', to find the change of speed directly since you don't need to know the actual acceleration. DeltaV is the change of speed: "ThisFrame'sSpeed- PreviousFrame'sSpeed", 't' is the amount of time during which this force was applied to the pad and it should be the time between two consecutive frames: 1/FPS.When you have the new velocity use the equation 'NewPosition == OldPosition + Velocity*(1/FPS)' to find the new position.The above are Newton's laws combined with Euler intergration, and I believe it should be as realistic as a pong pad can get. :);  One Caveat WhiteScorpion:Now that your paddle simulates Velocity and Acceleration (instead of instant 'Arcade' motion)Once the user lets go of the button, the paddle will still have a velocity and continue sliding.Do deal with this, when the user is no longer using one of the Accelerate buttons, a dampening friction is applied to the paddle to make it slow to a stop. However the overall innacuracy of the button timing vs the decceleration time might make control difficult for the Player.May I suggest:Instead of using buttons for the paddle, use the mouse cursor.Have the mousePosition indicate where you want the paddle to move to, then have the paddle automatically accelerate and start to deccelerate(at the right time) so that it stops moving right where the mousePosition is set.Then you'd have the realistic paddle motions, while also keeping accurate controls.   