user3075425
user3075425

Reputation: 362

Rotating a 3D Vector without a Matrix (OpenGL)

I want to rotate a Vector(0, -10, 0) around the X and Y axis. I want to rotate X rotX degrees(0 - 360°) and Y rotY degrees(0 - 180°). I tried much but I couldnt get it done. Coudld anybody give me a solution for that? Some of my code I tried:

LightRotX = -posX;
LightRotY = -10 * cos((rotY) * PI / 180.0) - posZ * sin((rotY) * PI / 180.0);
LightRotZ = -10 * sin((rotY) * PI / 180.0) + posZ * cos((rotY) * PI / 180.0);

float bZ = LightRotZ;
float bX = LightRotX;

LightRotX = bZ * sin((rotX) * PI / 180.0) + bX * cos((rotX) * PI / 180.0);
LightRotZ = bZ * cos((rotX) * PI / 180.0) - bX * sin((rotX) * PI / 180.0);

Upvotes: 4

Views: 3090

Answers (3)

Andrey Mishchenko
Andrey Mishchenko

Reputation: 4206

To rotate the point (x,y,z) around the z axis, the following works:

First, note that the z coordinate will stay the same. So, the problem boils down to rotating the point (x,y) in the plane around the origin. Let theta be the angle from the x axis defined as usual, and let r be the length of the vector (x,y) in the plane. These can be computed as:

  • r = sqrt(x*x + y*y)
  • theta = atan(y/x)

Say you want to rotate by phi degrees around the z axis. You want the point having length r but angle theta + phi. Its coordinates are:

  • (r * cos(theta + phi), r * sin(theta + phi))

Rotating around the y axis can be done similarly, by replacing every y in the above calculations with a z.

Upvotes: 4

Valentin
Valentin

Reputation: 664

If you want to rotate a point(x,y,z) around the y axis, then here is what you need to do if you don't want to use Matrices. Think in terms of planes rather than axis. For the case of y, the plane of choice would be the x-z plane. Imagine a circle drawn on the x-z plane, when you rotate this point around the y-axis, it position can be defined as a point P(x,z) on the circle drawn on the x-z plane.

So in order to rotate around the y-axis, you need to change the x and z coordinates for your vector/point. To rotate 90 degrees:

NewX=cos(pi/2)*r
NewZ=sin(pi/2)*r

I can't gurantee the correctness of the above two lines of pseudo code, but if you get the idea, you'll be able to implement it.

If you change X and Y, you'll be rotating around the Z axis, which is the X-Y plane.

Upvotes: 1

Antonio Romano
Antonio Romano

Reputation: 285

Your question is not really clear... Have you tried with the method glRotatef(angle, x_axis, y_axis, z_axis) where x_axis, y_axis, z_axis = {1.0, 0.0}; ?

Upvotes: 0

Related Questions