Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 871
user3075425 Avatar asked Sep 06 '25 01:09

user3075425


1 Answers

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.

like image 130
Andrey Mishchenko Avatar answered Sep 10 '25 08:09

Andrey Mishchenko