#### Archived

This topic is now archived and is closed to further replies.

# Rotatin'

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

## Recommended Posts

Howdy. I was wondering if anyone could give me a hand with rotating a 3D vector given pitch yaw and roll...without having to implement matrices. (just the calculation on one line or whatever) If anyone has this lying around, it would be a big help.

##### Share on other sites
Hmmm... Here's an old piece of code I found lying around:

    void CPoint::transform (CAngle angle){    double tempx = this->x;    double tempy = this->y;    this->x =   (tempy * angle.cos()) -                 (tempx * angle.sin()));    this->y =   (tempy * angle.sin()) +                 (tempx * angle.cos()));}

This will do a rotation around an axis at (0, 0). You can take the transformed values and rotate them again around the pitch axis and yet again around the yaw. The order in which you do this depends on how you want your 3D point (or object) to react to it's position. It's really better to use matrices though.

Actually... I just found some even older code that I wrote when I was a kid... man, this is a mess but see if YOU can make heads or tails of it. I can't =b hehehe but this is the full transformation you are looking for.

  tempX = x;tempY = y;tempZ = z;pntX = (tempX * cosRoll) - (tempY * sinRoll);pntY = (tempX * sinRoll) + (tempY * cosRoll);tempX = pntX;tempY = pntY;world[objNum].y = (tempY * cosPitch) - (tempZ * sinPitch) + py;pntZ = (tempY * sinPitch) + (tempZ * cosPitch);tempZ = pntZ;world[objNum].x = (tempZ * sinDir) + (tempX * cosDir) + px;world[objNum].z = (tempZ * cosDir) - (tempX * sinDir) + pz;

From what I can tell, I'm using Dir, Pitch and Roll with the sine and cosine values of each stored in temporary variables such as sinRoll and cosDir. The values start as x, y, and z and end as world[objNum].x, etc. This is some seriously ugly code.

Good luck =b

- Jay

"Strictly speaking, there is no need to teach the student, because the student himself is Buddha, even though he may not be aware of it." - Shunryu Suzuki

Get Tranced!

[edited by - coderx75 on June 3, 2002 2:05:45 AM]

##### Share on other sites
Z axis Z = Amount of Rotation
NEWx = (cos (Z) * X)- (sin (Z) * y);
NEWy = (sin (Z) * X) + (cos (Z) * y);

Y axis Y= Amount of Rotation
NEWx = (cos (Y) * x) + (sin (Y) * Z);
NEWz = -(sin (Y) * x) + (cos (Y) * Z);

X axis X= Amount of Rotation
NEWy = cos(X)*Y - sin(X)*Z;
NEWz = cos(X)*Z + sin(X)*Y;

##### Share on other sites
Thanks for the ideas...Hmm..not quite sure what you mean by that last bit of code. Is that supposed to be for one vector, or an axis? The X, Y, Z variables are coming from the original vector? .. and what are the NEWX, Y, Z variables for.. (they get overwritten, or are they supposed to be saved to another structure before the next block?)

##### Share on other sites
Z axis Z = Amount of Rotation
the x and y variables are your x and y parts of your vector then you use Z as your Amount of rotaion and then what you do is you the NEWx and NEWy are then the values that you will use if they are the same x and y then the the second line will fail because it needs the original x value to work the same works for the rest of the Axes

you use the diffrent 2 lines for diffrent rotations. in C++ you can use this to rotate a Vertex.

// lowercase are CORDS and Upper case are ROTATION AMOUNTS
void rotateanddrawvertex(float x, float y, float z,float X, float Y, float Z)
{
float y2;
float z2;
float x2;

//Z AXIS
x2 = (cos (Z) * x) - (sin (Z) * y);
y2 = (sin (Z) * x) + (cos (Z) * y);
x=x2;
y=y2;
//Y AXIS
x2 = (cos (Y) * x) + (sin (Y) * z);
z2 = -(sin (Y) * x) + (cos (Y) * z);
x=x2;
z=z2;
//X AXIS
y2 = cos(X)*y - sin(X)*z;
z2 = cos(X)*z + sin(X)*y;
y=y2;
z=z2;

DRAWVERTEX(x,y,z);
}

[edited by - Xero-X2 on June 3, 2002 2:44:50 AM]

Thanks a lot!