Velocity in rotation using 2D vectors???

This is a discussion on Velocity in rotation using 2D vectors??? within the Game Programming forums, part of the General Programming Boards category; I'm rotating a sprite using the following, but when the spaceship, which is circular, is facing towards the left side ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Velocity in rotation using 2D vectors???

    I'm rotating a sprite using the following, but when the spaceship, which is circular, is facing towards the left side of the screen and I press the thrust key it slows down? Any help? Velocity, acceleration and positon are all vectors. Can't think of anymore code you may need!
    Code:
    void drawObject() {
    ...
    ...
    
      //Acceletion moves the spaceship across the screen 
      velocity += acceleration * pTheGameTimer->mdFrameTime;
    
      //Adjust bearing angle according to the above key press
      velocity.setBearing((dAngle/360*6.282), velocity.magnitude());
    
      //Move helicopter based upon velocity
      position += velocity * pTheGameTimer->mdFrameTime;
    }
    
    
    void Vector2D::setBearing(double angle, double magnitude)
    {
      XValue = magnitude*sin(angle);
      YValue = magnitude*cos(angle);
    }
    
    double Vector2D::magnitude() const
    {
      return sqrt(XValue*XValue + YValue*YValue);
    }
    I didn't develop the 2D class, but it also has:
    Code:
    Vector2D Vector2D::rotatedBy(double angle) const
    {
      double s = sin(angle);
      double c = cos(angle);
      return Vector2D(this->XValue * c - this->YValue*s, this->XValue * s + this->YValue * c);
    
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Nevermind, I've figured it out...


    Swapped this:

    Code:
           //Adjust bearing angle according to the above key press
           velocity.setBearing((dAngle/360*6.282), velocity.magnitude());
    for this:
    Code:
            //Adjust bearing angle according to the above key press
    	acceleration.setBearing((dAngle/360*6.282), acceleration.magnitude());
    Works like a gem now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 09:16 AM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 07:31 PM
  3. Velocity vector troubles
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 01-18-2005, 11:40 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. 2d [STL] Vectors
    By cozman in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2002, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21