开发者

Rotating objects

I have an object (a cone) with a base on a vertex X1 (the circle base of the con开发者_StackOverflow中文版e has it's center on X1) and points to Oz. I would like to rotate it, so it would point to a given vertex X2. I tried many combinations, I made many calculations still can't get it working. It would be nice if you could give an answer using the C++ OpenGL function glRotatef, but a general answer would be also appreciated. I'm trying this on a cone now, but it should be working on any mesh.

So far I have this:

h1 = sqrt(pow(p2.y - p1.y, 2) + pow(p2.z - p1.z, 2));
h2 = sqrt(pow(p2.x - p1.x, 2) + pow(p2.z - p1.z, 2));
h3 = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2) + pow(p2.z - p1.z, 2));

r.x = sign(p1.y-p2.y)*acos((p1.z-p2.z)/h1) * 180/M_PI;
r.y = sign(p1.x-p2.x)*acos((p1.z-p2.z)/h2) * 180/M_PI;
r.z = 0;

cone.SetPosition(p2);
cone.SetHeight(h3);
cone.SetRotation(r);

Where sign() is what it's name, returns -1 for negative and 1 for positive parameter. And the drawing function of the Cone object looks like this:

void drawCone()
{
    glPushMatrix();
    glTranslatef(pos.x, pos.y, pos.z);
    glRotatef(rot.x, 1, 0, 0);
    glRotatef(rot.y, 0, 1, 0);
    glRotatef(rot.z, 0, 0, 1);
    glColor3f(col.red, col.green, col.blue);
    gluDisk(cone, 0, radius, 10, 10);
    gluCylinder(cone, radius, 0, height, 10, 10);
    glPopMatrix();
}

It doesn't need to be a highly optimized solution, just a working one.


If I understand your question correctly, you basically want to rotate one vector onto another along the shortest spherical path- that is what aiming generally comes down to. This turns out to be a bit painful to do with Euler angles, in the general case. I have some code somewhere that does a general aim at entirely with Euler angles, but it is overly complicated- if for some reason you have to do this with Euler angles I could dig it up.

But what you really want to do here, IMHO, is use a quaternion representation. That makes things very simple. You just take the vector you want to rotate onto and the vector you want to rotate, and find their cross product. That is the axis you want to rotate around. Then find the angle between the two vectors- that is the amount you want to rotate by. Rotating by a certain amount around a certain axis is what quaternions do best- there is a singularity if you want to rotate onto an axis that points in exactly the opposite direction from your initial vector though- but that's unavoidable, and might not matter much for what you are doing.

EDIT: It's also not that hard to calculate the rotation matrix directly given an angle and an axis. But in every case I've had to do this in I had to be able to interpolate between the rotational frames. That's why I suggest using quaternions. If you don't have to interpolate smoothly any method of deriving a rotation from an axis and angle will be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜