PDA

View Full Version : how can I rotate a vector around x axis

wf
03-06-2002, 05:34 AM
I want to rotate a cyliner. To accomplish this, I have to rotate a vector around x axis. how can I do this?

I wrote some code, seems not working fine now.

void CObjPosition::rotate_cylinder(struct static_obj_cylinder* cylinder,
float around_x, float around_y)
{
TRACE("rotate cyliner!\n");
float x,y,z;
float delta_x1, delta_y1, delta_z1;
float delta_x2, delta_y2, delta_z2;
float delta_y_z1, delta_y_z2;
double theta1, theta2;

x = (cylinder->center_x1 + cylinder->center_x2)/2;
y = (cylinder->center_y1 + cylinder->center_y2)/2;
z = (cylinder->center_z1 + cylinder->center_z2)/2;

delta_x1 = cylinder->center_x1 - x;
delta_y1 = cylinder->center_y1 - y;
delta_z1 = cylinder->center_z1 - z;
delta_y_z1 = sqrt(delta_y1*delta_y1+delta_z1*delta_z1);
if(delta_y_z1 != 0)
theta1 = asin(delta_y1/delta_y_z1);
else
{
theta1 = 0;
g_out.Insert("something wrong! delta_y_z_1=0 in rotate_cylinder!\n");
}

delta_x2 = cylinder->center_x2 - x;
delta_y2 = cylinder->center_y2 - y;
delta_z2 = cylinder->center_z2 - z;
delta_y_z2 = sqrt(delta_y2*delta_y2+delta_z2*delta_z2);

theta2 = theta1+PI/2;
/*
if(delta_y_z2 != 0)
theta2 = asin(delta_y1/delta_y_z2);
else
{
theta2 = 0;
g_out.Insert("something wrong! delta_y_z_2=0 in rotate_cylinder!\n");
}*/
theta1 += around_x;
theta2 += around_x;

//rotate around x axis, so not change x value
cylinder->center_x1 = cylinder->center_x1;
cylinder->center_y1 = y+delta_y_z1*sin(theta1);
cylinder->center_z1 = z+delta_y_z1*cos(theta1);

cylinder->center_x2 = cylinder->center_x2;
cylinder->center_y2 = y+delta_y_z2*sin(theta2);
cylinder->center_z2 = z+delta_y_z2*cos(theta2);

}

wf
03-06-2002, 08:06 AM
I got it through, my mistake.
I got a three helps here, so the code here to share with all :).

void CObjPosition::rotate_cylinder(struct static_obj_cylinder* cylinder,
float around_x, float around_y)
{
float x,y,z;
float delta_x1, delta_y1, delta_z1;
float delta_x2, delta_y2, delta_z2;
float delta_y_z1, delta_y_z2;
double theta1, theta2;

static float p_x1, p_y1, p_z1;
static float p_x2, p_y2, p_z2;
static float pdelta_x1, pdelta_y1, pdelta_z1;
static float pdelta_x2, pdelta_y2, pdelta_z2;
static float pdelta_y_z1, pdelta_y_z2;
static double ptheta1, ptheta2;

x = (cylinder->center_x1 + cylinder->center_x2)/2;
y = (cylinder->center_y1 + cylinder->center_y2)/2;
z = (cylinder->center_z1 + cylinder->center_z2)/2;

delta_x1 = cylinder->center_x1 - x;
delta_y1 = cylinder->center_y1 - y;
delta_z1 = cylinder->center_z1 - z;
delta_y_z1 = sqrt(delta_y1*delta_y1+delta_z1*delta_z1);

if(delta_z1==0)
{
if(delta_y1>=0)
theta1=PI/2;
else
theta1=PI/2+PI;
}
else
{
//if(delta_y1>=0)
if(delta_z1>=0)
theta1=atan(delta_y1/delta_z1);
else
theta1=atan(delta_y1/delta_z1)+PI;
}

/*
if(delta_y_z1 != 0)
theta1 = asin(delta_y1/delta_y_z1);
else
{
theta1 = 0;
g_out.Insert("something wrong! delta_y_z_1=0 in rotate_cylinder!\n");
}*/

delta_x2 = cylinder->center_x2 - x;
delta_y2 = cylinder->center_y2 - y;
delta_z2 = cylinder->center_z2 - z;
delta_y_z2 = sqrt(delta_y2*delta_y2+delta_z2*delta_z2);

if(delta_z2==0)
{
if(delta_y2>=0)
theta2=PI/2;
else
theta2=PI/2+PI;
}
else
{
//if(delta_y2>=0)
if(delta_z2>=0)
theta2=atan(delta_y2/delta_z2);
else
theta2=atan(delta_y2/delta_z2)+PI;
}

/*
if(delta_y_z2 != 0)
theta2 = asin(delta_y1/delta_y_z2);
else
{
theta2 = 0;
g_out.Insert("something wrong! delta_y_z_2=0 in rotate_cylinder!\n");
}*/
theta1 += around_x;
theta2 += around_x;

TRACE("theta1:%f, theta2:%f, dy1:%f,dz1:%f,dy2:%f dz2:%f\n"
,theta1, theta2, delta_y1, delta_z1, delta_y2, delta_z2);

//rotate around x axis, so not change x value
cylinder->center_x1 = cylinder->center_x1;
cylinder->center_y1 = y+delta_y_z1*sin(theta1);
cylinder->center_z1 = z+delta_y_z1*cos(theta1);

cylinder->center_x2 = cylinder->center_x2;
cylinder->center_y2 = y+delta_y_z2*sin(theta2);
cylinder->center_z2 = z+delta_y_z2*cos(theta2);

if(ptheta1*theta1 < 0 &#0124; &#0124; ptheta2*theta2 < 0)
{
TRACE("x1:%f, y1:%f, z1:%f,x2:%f,y2:%f, z2:%f\n"
,cylinder->center_x1, cylinder->center_y1, cylinder->center_z1
,cylinder->center_x2, cylinder->center_y2, cylinder->center_z2);
TRACE("theta1:%f, theta2:%f, dy1:%f,dz1:%f,dy2:%f dz2:%f\n"
,theta1, theta2, delta_y1, delta_z1, delta_y2, delta_z2);
TRACE("px1:%f, py1:%f, pz1:%f,px2:%f,py2:%f, pz2:%f\n"
,p_x1, p_y1, p_z1,p_x2, p_y2, p_z2);
TRACE("ptheta1:%f, ptheta2:%f, pdy1:%f,pdz1:%f,pdy2:%f pdz2:%f\n"
,ptheta1, ptheta2, pdelta_y1, pdelta_z1, pdelta_y2, pdelta_z2);
}

p_x1=cylinder->center_x1;
p_y1=cylinder->center_y1;
p_z1=cylinder->center_z1;

p_x2=cylinder->center_x2;
p_y2=cylinder->center_y2;
p_z2=cylinder->center_z2;

pdelta_x1=delta_x1;
pdelta_y1=delta_y1;
pdelta_z1=delta_z1;

pdelta_x2=delta_x2;
pdelta_y2=delta_y2;
pdelta_z2=delta_z2;

pdelta_y_z1=delta_y_z1;
pdelta_y_z2=delta_y_z2;

ptheta1 = theta1;
ptheta2 = theta2;
}