Rotating Points about origin
I need to know how i can get the resulting coordinate after a point has been rotated.
For example i have a point at (1,0).
After rotating it 90degrees, it should be at (0,1).
The problem is how do i get the new coordinate of (0,1) given it''s original position and 90 degrees.
July 06, 2003 08:17 AM
x_origin = 0.0;
y_origin = 0.0;
radius = 1.0;
angle = pi/2; //90 degrees
x = x_origin+(cos(angle)*radius);
y = y_origin+(sin(angle)*radius);
cos() and sin() work with radians
y_origin = 0.0;
radius = 1.0;
angle = pi/2; //90 degrees
x = x_origin+(cos(angle)*radius);
y = y_origin+(sin(angle)*radius);
cos() and sin() work with radians
Hmm, so for every point i have to calculate the distance it is from the origin(radius)?
Whats the most efficient way to calculate it?
Whats the most efficient way to calculate it?
July 06, 2003 09:24 AM
p1,p2; // p1 is the origin point in space and p2 is some other point anywhere.
distance = sqrt(((p1.x-p2.x)*(p1.x-p2.x))
+((p1.y-p2.y)*(p1.y-p2.y))
+((p1.z-p2.z)*(p1.z-p2.z)));
Can''t seem to find whats wrong.
void Scene::initCol() { double distance; for(int i=0;i<numOfObj;i++) { double xoff=sin(Objects[i].rotY*PI/180);//amount to rotate collission detection values double zoff=cos(Objects[i].rotY*PI/180);//amount to rotate collission detection values for(int j=0;j<Objects[i].numOfVertex;j++) { distance=sqrt(pow(Objects[i].vertex[j][0],2)+pow(Objects[i].vertex[j][2],2)); Objects[i].vertex[j][0]+=xoff*distance; //multiply offset to rotate Objects[i].vertex[j][2]+=zoff*distance; Objects[i].vertex[j][0]+=Objects[i].posX; Objects[i].vertex[j][1]+=Objects[i].posY; Objects[i].vertex[j][2]+=Objects[i].posZ; } } }
Anyone heard about matrixes?
--------------------------------
"I''m a half time coder, full time Britney Spears addict"
Targhan
--------------------------------
"I''m a half time coder, full time Britney Spears addict"
Targhan
--------------------------------"I'm a half time coder, full time Britney Spears addict" Targhan
push a matrix.
load idenity (to make sure your back at 0,0,0
translate over,
then rotate.
Would that not do the wame thing al those calculations do?
load idenity (to make sure your back at 0,0,0
translate over,
then rotate.
Would that not do the wame thing al those calculations do?
Sure it would! But sometimes you need the real coordinates, not only the result...
//x,y,z:original coordinates, x_a, y_a, z_a: angles for rotation
void vertex(float x, float y, float z, float x_a, float y_a, float z_a)
{
float x_alt, y_alt, z_alt;
x_a=(x_a/180)*PI;
y_a=(y_a/180)*PI;
z_a=(z_a/180)*PI;
//rotate z-axis
x_alt=x;
y_alt=y;
z_alt=z;
x=x_alt*cos(z_a)-y_alt*sin(z_a);
y=y_alt*cos(z_a)+x_alt*sin(z_a);
//rotate y-axis
x_alt=x;
y_alt=y;
z_alt=z;
x=(x_alt*cos(y_a)-z_alt*sin(y_a));
z=(z_alt*cos(y_a)+x_alt*sin(y_a));
//rotate x-axis
x_alt=x;
y_alt=y;
z_alt=z;
y=(y_alt*cos(x_a)-z_alt*sin(x_a));
z=(z_alt*cos(x_a)+y_alt*sin(x_a));
glVertex3f(x,y,z);
}
perhaps this will help you.
[edited by - Bero_Avrion on July 6, 2003 4:07:43 PM]
[edited by - Bero_Avrion on July 6, 2003 4:09:48 PM]
//x,y,z:original coordinates, x_a, y_a, z_a: angles for rotation
void vertex(float x, float y, float z, float x_a, float y_a, float z_a)
{
float x_alt, y_alt, z_alt;
x_a=(x_a/180)*PI;
y_a=(y_a/180)*PI;
z_a=(z_a/180)*PI;
//rotate z-axis
x_alt=x;
y_alt=y;
z_alt=z;
x=x_alt*cos(z_a)-y_alt*sin(z_a);
y=y_alt*cos(z_a)+x_alt*sin(z_a);
//rotate y-axis
x_alt=x;
y_alt=y;
z_alt=z;
x=(x_alt*cos(y_a)-z_alt*sin(y_a));
z=(z_alt*cos(y_a)+x_alt*sin(y_a));
//rotate x-axis
x_alt=x;
y_alt=y;
z_alt=z;
y=(y_alt*cos(x_a)-z_alt*sin(x_a));
z=(z_alt*cos(x_a)+y_alt*sin(x_a));
glVertex3f(x,y,z);
}
perhaps this will help you.
[edited by - Bero_Avrion on July 6, 2003 4:07:43 PM]
[edited by - Bero_Avrion on July 6, 2003 4:09:48 PM]
Ok this is really weird.
the cos of 90 degrees which is 1.5708 in radians.
When i cos(1.5708) and print out the results, i get 6.123....
But when i cos(1.5708) on my scientific calculator in radian mode i get almost 0.
Im using the math.h library and i don''t know why i am getting incorrect results.
the cos of 90 degrees which is 1.5708 in radians.
When i cos(1.5708) and print out the results, i get 6.123....
But when i cos(1.5708) on my scientific calculator in radian mode i get almost 0.
Im using the math.h library and i don''t know why i am getting incorrect results.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement