Public Group

# how to rotate vector? been looking for two days

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been looking for two days, and before that, I came up with two ways that I cant preform corectly, and threw out the code for. Does anybody have C++ code for this?

##### Share on other sites
What do you want to use as the rotation to rotate the vector by? A matrix? a quaternion? a set of euler angles? An exponential map?

##### Share on other sites
anything including quaternions. Euler Angles might work, because im making modular stuctures that are just placed about, theres no physics needed right now on them and ill still be happy.

##### Share on other sites
For quaternions: vr = q * vo * q-1, where vo is the vector "transmogrified" into a pseudo-quaternion by adding a 0 as the fourth element.

##### Share on other sites
i think i understand that.

##### Share on other sites
Using regular trig, you can derive that if (a , b) is your original vector and t is the angle you want to rotate it by (anticlockwise), then to rotate around the origin you can use:

x = a*cos(t) - b*sin(t)
y = a*sin(t) + b*cos(t)

to obtain a new vector (x , y).

That may be wrong, because I just derived it myself on a scrap bit of paper.

I imagine for a 3d vector (a, b, c), if that's what you want, then you can use that as a rotation around the z axis, with the c value staying constant, for rotation around the y axis you'd want to subsitute y for z and b for c and keep b constant, and for rotation around the x substitute x for z and a for c and a is constant.

That may be completely wrong though, like I say. It shouldn't be too hard to derive the matrices from here, if you want them.

 ignore the stuff after rotation around z; I got it wrong.

Rotation around the y is:
x = c*sin(t) + a*cos(t)
z = c*cos(t) - a*sin(t)
and y = c

and to rotate around the x:
y = b*cos(t) - c*sin(t)
z = b*sin(t) + c*cos(t)
and x = a

Must remember to think rather than guess in future!

##### Share on other sites
I found some code in a vector class at www.planet-source-code.com that seems to involve all the required techniques, and it works well for me, and i know it works well because im rotating a whole collision structure with it, and so for placing objects around in different orientations, its fine, but could i run into the "gimbal lock" that ive heard about before?

heres the function that rotates an input vector by the Y axis by an input Degree:

#define DAX_PI 3.141592653589793238462643383279
#define PiOver180 DAX_PI/180
#define d180OverPi 180/DAX_PI

Vector3 obj::rotateAboutY_Deg(Vector3& _Vec1,double _Angle)
{
Vector3 _Vec0;
double sinCalc = (double)sin(_Angle * PiOver180);
double cosCalc = (double)cos(_Angle * PiOver180);

_Vec0.x = (_Vec1.x * cosCalc) + (_Vec1.z * sinCalc);
if((_Vec0.x < .00000000001) && (_Vec0.x > -.00000000001))
_Vec0.x = 0.0;
_Vec0.y = _Vec1.y;
_Vec0.z = (_Vec1.z * cosCalc) - (_Vec1.x * sinCalc);
if((_Vec0.z < .00000000001) && (_Vec0.z > -.00000000001))
_Vec0.z = 0.0;
return (_Vec0);
}

Beacause i want my robot to throw the scenery around with out having a rotation lock up on me.

Oh and to get the original source code vector class, search for MultiVector at www.planet-source-code.com.

##### Share on other sites
I believe gimbal lock is when your forward vector is parallel to your up vector? Actually I guess if any of your axes are parallel that would be bad. The way I usually combat this is simply checking to see if my forward vector dotted with my up vector is too close to 1 or -1. If it is, I rotate back by the amount I just tried to rotate...

Here's a quick example
void RotateZ(float fRadians){        // do all your normal rotation stuff    // now we're going to check to make sure our axes are ok    Vector globalUp(0.0f, 1.0f, 0.0f);    // this is just a guess, you may need to play around    // to find which epsilon value works the best for your case    const float fEps = 1e-3;    // just take the absolute value because -1 is just as     // bad as 1 in this case...    float dotValue = fabs( Dot( camera.ZAxis(), globalUp) );    // if our two axes are too close, rotate the opposite amount    // to get back to where we started... This could be done differently    // perhaps by using temp variables to calculate the rotations    // and then assigning them to the real variables ONLY if this statement    // isn't true-- you get the idea...    if( 1.0f - dotValue < fEps )    {       RotateZ(-fRadians);    }

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015295
×