# vector rotation

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

## Recommended Posts

i need some help with rotating a 3D normal vector with nothing but three different angles. im still working on my particle system and im trying to find a way to rotate the emitter without rotating the particles that have already been emitted (ie glRotate). make sense? ive tried using the complex matrix thats used in glRotate but i cant get it to work. ive tried this too but the particle spread doesnt work.
                        float sp = rand()%(int)floor(speedVar);
sp = speed - sp;

//random angle

float x = sin(xy*PIOVER180);
float y = cos(xy*PIOVER180);
float z = sin(zy*PIOVER180);

//x-axis
x = x;
y = sin(emitRot.x);
z = cos(emitRot.x);
//y-axis
x = cos(emitRot.y);
y = y;
z = sin(emitRot.y);
//z-axis
x = cos(emitRot.z);
y = sin(emitRot.z);
z = z;

Vector3 vect = Normalize(x, y, z);

pArray[part].dir.x = vect.x * sp;
pArray[part].dir.y = vect.y * sp;
pArray[part].dir.z = vect.z * sp;


##### Share on other sites
maybe i'm confused but it looks to me like you are reassigning the x, y, and z values 3 times. the only x, y, and z values that are getting normalized are the last ones. What i mean is that in this code block, x will always be cos(emitRot.z), y will always be sin(emitRot.z), and z will always be assigned to its original value, sin(zy*PIOVER180). This may be part of your problem, but i may just be confused as to what you are trying to show here.

##### Share on other sites
wow duh i cant believe i did that, rotating the values only around the z-axis.

essentially what im trying to do is rotate the emitter around an axis (x, y, or z). all the user has to do is supply 3 angles that they want to rotate for example:

i want to rotate the emitter 45 degrees around the z-axis rotate(0, 0, 45)
and then i want to rotate the emitter 90 degrees around the y-axis rotate(0, 90, 0)

im not entirely sure of how to acheive this. im still kinda new to matrix rotations

##### Share on other sites
The simplest way to rotate 3d vector is to multiply it by rotation matrix.
Let rotation matrix (3x3) is M, and vector we want to rotate is V, then rotated vector is given by
M*V

Now, on angles. Your angles is very much like Euler angles. You can use euler angles-to-matrix to deal with that.
Also, you can look for axis-and-angle to matrix conversion. Then, you can do:
Matrix3x3 M=AxisAngleToMatrix(0,1,0,y_angle);
M*=AxisAngleToMatrix(0,0,1,z_angle);

Then you can multiply your vectors by this matrix to transform 'em.

It's the same as
glRotate(y_angle,0,1,0);// replaces current matrix M with M*AxisAngleToMatrix(0,1,0,y_angle);
glRotate(z_angle,0,0,1);// replaces current matrix M with M*AxisAngleToMatrix(0,0,z,z_angle);

So, when firing a particle, you can first compute particle_pos and particle_velocity, and then multiply it with that matrix. Like that:
particle.pos=M*parrticle.pos;
particle.vel=M*parrticle.vel;
(That's assuming particles start in local origin, of course)
Other particles will not be affected.

It's critical to learn matrices if you want to program such things, and particles is good example

##### Share on other sites
Dmytry-> im a little lost. i understand the function calls but im missing the math. ive tried changing it a little with this:
			float x = sin(xy*PIOVER180) + x + cos(emitRot.y) + cos(emitRot.z);			float y = cos(xy*PIOVER180) + sin(emitRot.x) + y + sin(emitRot.z);			float z = sin(zy*PIOVER180) + cos(emitRot.x) + sin(emitRot.z) + z;

it gives me some STRANGE effects. its as if its trying to rotate on all of the axises

• ### What is your GameDev Story?

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

• 11
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634078
• Total Posts
3015375
×