Rotating a vector
Could someone tell me how to rotate a vector around an angle?
Sample code would be good.
~Phantom88~
you mean around an origen with an end at the relative 0,0,0? or rotate it around a point where its not contacting the origen?
This is a function i wrote some time ago, the input r is in degrees.
I have not been able to test it though, hope it works
void rotVector(float r[3], float v[3], float *ret)
{
float a[3];
r[0]=r[0]/(180/3.1415);
r[1]=r[1]/(180/3.1415);
r[2]=r[2]/(180/3.1415);
a[0]=(cos(r[1])*cos(r[2])*v[0])+(sin(r[2])*v[0])+(asin(r[1])*v[0]);
a[1]=(asin(r[2])*v[1])+(cos(r[0])*cos(r[2])*v[1])+(sin(r[0])*v[1]);
a[2]=(sin(r[1])*v[2])+(asin(r[0])*v[2])+(cos(r[0])*cos(r[1])*v[2]);
ret=a;
}
I have not been able to test it though, hope it works
void rotVector(float r[3], float v[3], float *ret)
{
float a[3];
r[0]=r[0]/(180/3.1415);
r[1]=r[1]/(180/3.1415);
r[2]=r[2]/(180/3.1415);
a[0]=(cos(r[1])*cos(r[2])*v[0])+(sin(r[2])*v[0])+(asin(r[1])*v[0]);
a[1]=(asin(r[2])*v[1])+(cos(r[0])*cos(r[2])*v[1])+(sin(r[0])*v[1]);
a[2]=(sin(r[1])*v[2])+(asin(r[0])*v[2])+(cos(r[0])*cos(r[1])*v[2]);
ret=a;
}
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
ah use a 4*4 matrix please!
http://www.8ung.at/basiror/theironcross.html
4X4 is for whimps, you only need a 3X3 + a float.
I wrote this function from another function i have that creates a new rotation matrix. it probobly dosn''t work anyway, and i do got other more solid functions.
I wrote this function from another function i have that creates a new rotation matrix. it probobly dosn''t work anyway, and i do got other more solid functions.
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
or.. you might want to look into quaternions.
Crispy
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Yes, i mean around the origin.
lc_overlord, your code doesn''t work. You said you have a other one?
Are there any good tutorials about matrices? Or does anyone else have a good function to do this?
~Phantom88~
lc_overlord, your code doesn''t work. You said you have a other one?
Are there any good tutorials about matrices? Or does anyone else have a good function to do this?
~Phantom88~
Is there actually a way to get OpenGL to do this? Somehow with glRotatef?
~Phantom88~
~Phantom88~
I know it dosn''t work.
the other one is realy a colection of 4 functions that really are basic some matrix tranformation fuctions.
These should work for what they where written for, i don''t know if that''s what you want.
The rotVector function should rotate a vector a specified nubmer of derees.
a matrix is a float[12]
void rot3_2_matrix(rot3 r, float *retM)
{
matrix a,b,c,d,e;
r[0]=r[0]/(180/3.1415);
r[1]=r[1]/(180/3.1415);
r[2]=r[2]/(180/3.1415);
a[0]=1; a[1]=0; a[2]=0; a[3]=0;
a[4]=0; a[5]=cos(r[0]); a[6]=-sin(r[0]);a[7]=0;
a[8]=0; a[9]=sin(r[0]);a[10]=cos(r[0]); a[11]=0;
b[0]=cos(r[1]); b[1]=0; b[2]=sin(r[1]);b[3]=0;
b[4]=0; b[5]=1; b[6]=0; b[7]=0;
b[8]=-sin(r[1]);b[9]=0; b[10]=cos(r[1]);b[11]=0;
c[0]=cos(r[2]); c[1]=-sin(r[2]);c[2]=0; c[3]=0;
c[4]=sin(r[2]); c[5]=cos(r[2]); c[6]=0; c[7]=0;
c[8]=0; c[9]=0; c[10]=1; c[11]=0;
Combine_Matrix(a,b,d);
Combine_Matrix(c,d,e);
memcpy(retM,e,48);
}
void rotVector(float r[3], float v[3], float *ret)
{
matrix a;
rot3_2_matrix(r,a);
Translate_Vertic(v,a,ret);
memcpy(ret,a,12);
}
void Translate_Vertic(vec3 vert, matrix Matrix, float *ret)
{
vec3 v;
v[0] = vert[0] * Matrix[0] + vert[1] * Matrix[1] + vert[2] * Matrix[2] +Matrix[3];
v[1] = vert[0] * Matrix[4] + vert[1] * Matrix[5] + vert[2] * Matrix[6] +Matrix[7];
v[2] = vert[0] * Matrix[8] + vert[1] * Matrix[9] + vert[2] * Matrix[10] +Matrix[11];
memcpy(ret,v,12);
}
void Combine_Matrix(matrix MatrixA,matrix MatrixB, float *retM)
{
matrix NewMatrix;
int i;
for(i = 0; i < 3; i++){ //Cycle through each vector of first matrix.
NewMatrix[i*4] = MatrixA[i*4] * MatrixB[0] + MatrixA[i*4+1] * MatrixB[4] + MatrixA[i*4+2] * MatrixB[8];
NewMatrix[i*4+1] = MatrixA[i*4] * MatrixB[1] + MatrixA[i*4+1] * MatrixB[5] + MatrixA[i*4+2] * MatrixB[9];
NewMatrix[i*4+2] = MatrixA[i*4] * MatrixB[2] + MatrixA[i*4+1] * MatrixB[6] + MatrixA[i*4+2] * MatrixB[10];
}
NewMatrix[3] = MatrixA[3] + MatrixB[3];
NewMatrix[7] = MatrixA[7] + MatrixB[7];
NewMatrix[11] = MatrixA[11] + MatrixB[11];
/*this should combine the matrixes*/
memcpy(retM,NewMatrix,48);
}
the other one is realy a colection of 4 functions that really are basic some matrix tranformation fuctions.
These should work for what they where written for, i don''t know if that''s what you want.
The rotVector function should rotate a vector a specified nubmer of derees.
a matrix is a float[12]
void rot3_2_matrix(rot3 r, float *retM)
{
matrix a,b,c,d,e;
r[0]=r[0]/(180/3.1415);
r[1]=r[1]/(180/3.1415);
r[2]=r[2]/(180/3.1415);
a[0]=1; a[1]=0; a[2]=0; a[3]=0;
a[4]=0; a[5]=cos(r[0]); a[6]=-sin(r[0]);a[7]=0;
a[8]=0; a[9]=sin(r[0]);a[10]=cos(r[0]); a[11]=0;
b[0]=cos(r[1]); b[1]=0; b[2]=sin(r[1]);b[3]=0;
b[4]=0; b[5]=1; b[6]=0; b[7]=0;
b[8]=-sin(r[1]);b[9]=0; b[10]=cos(r[1]);b[11]=0;
c[0]=cos(r[2]); c[1]=-sin(r[2]);c[2]=0; c[3]=0;
c[4]=sin(r[2]); c[5]=cos(r[2]); c[6]=0; c[7]=0;
c[8]=0; c[9]=0; c[10]=1; c[11]=0;
Combine_Matrix(a,b,d);
Combine_Matrix(c,d,e);
memcpy(retM,e,48);
}
void rotVector(float r[3], float v[3], float *ret)
{
matrix a;
rot3_2_matrix(r,a);
Translate_Vertic(v,a,ret);
memcpy(ret,a,12);
}
void Translate_Vertic(vec3 vert, matrix Matrix, float *ret)
{
vec3 v;
v[0] = vert[0] * Matrix[0] + vert[1] * Matrix[1] + vert[2] * Matrix[2] +Matrix[3];
v[1] = vert[0] * Matrix[4] + vert[1] * Matrix[5] + vert[2] * Matrix[6] +Matrix[7];
v[2] = vert[0] * Matrix[8] + vert[1] * Matrix[9] + vert[2] * Matrix[10] +Matrix[11];
memcpy(ret,v,12);
}
void Combine_Matrix(matrix MatrixA,matrix MatrixB, float *retM)
{
matrix NewMatrix;
int i;
for(i = 0; i < 3; i++){ //Cycle through each vector of first matrix.
NewMatrix[i*4] = MatrixA[i*4] * MatrixB[0] + MatrixA[i*4+1] * MatrixB[4] + MatrixA[i*4+2] * MatrixB[8];
NewMatrix[i*4+1] = MatrixA[i*4] * MatrixB[1] + MatrixA[i*4+1] * MatrixB[5] + MatrixA[i*4+2] * MatrixB[9];
NewMatrix[i*4+2] = MatrixA[i*4] * MatrixB[2] + MatrixA[i*4+1] * MatrixB[6] + MatrixA[i*4+2] * MatrixB[10];
}
NewMatrix[3] = MatrixA[3] + MatrixB[3];
NewMatrix[7] = MatrixA[7] + MatrixB[7];
NewMatrix[11] = MatrixA[11] + MatrixB[11];
/*this should combine the matrixes*/
memcpy(retM,NewMatrix,48);
}
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement