CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Velocity in rotation using 2D vectors???

1. Junior Member
Join Date
May 2008
Posts
13

## 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!

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:

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. Junior Member
Join Date
May 2008
Posts
13

## Re: Velocity in rotation using 2D vectors???

Nevermind, I've figured it out...

Swapped this:

//Adjust bearing angle according to the above key press
velocity.setBearing((dAngle/360*6.282), velocity.magnitude());

for this:

//Adjust bearing angle according to the above key press
acceleration.setBearing((dAngle/360*6.282), acceleration.magnitude());

Works like a gem now!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•