Yet Another Rotation Question :)

Well,

i can compute the direction vector from a position to anoher.
How can i compute the rotation values from this direction vector.

example :
for direction vector = (0.0f, 1.0f, 0.0f) going up, i should get (90.0f, 0.0f, 0.0f) for rotation values…

Any idea ??

I don’t know if I correctly understood your question…
You have a direction vector and you want to get the rotation angles: first of all, in this way you haven’t any information about the ‘roll’ (rotation around the axis that is parallel to your vector). For this, you need an ‘up’ vector, ortho to the direction vector.
For the other 2 angles, you can think at spherical coordinates, assuming that the direction vector is normailzed:

  • rotation around the vertical direction: arcsin of the projection on the horizontal direction of your vector
  • rotation around the horizontal direction: arcsin of the projection on the vertical direction of your vector
    Then you have to work with the signs of the projections to use 360° for 1st and 180° for 2nd.
    If you need it for a camera, forget all and use gluLookAt.
    Hope that could be useful…
    Bye

Yep Teofuzz,

I know that gluLookAt is fine but the problem is that it doesn’t provide feedback…
Oh yeah, I still can retreive the current modelview matrix but i think this could be a performance hit in a real time app (a game for example). May be i’m wrong, may be not :slight_smile:

Maybe my first tutorial on 3D-rotations will help. It can be found at my site:
home.conceptsfa.nl/~fredo
in the tutorial section. It explains 3D-rotations in detail. Let me know what you think about it.

Hope this helps,

Edo

Yep Edo,

i had a look to your tut : cool, but not usefull for as the rotation values are already known and my problem is to determine them :wink:

Let’s say i’m located at (Xp, Yp, Zp) and i’m looking at (Xt, Yt, Zt). What are the Xr, Yr, Zr rotation values a need to give openGL for this piece of code :
glRotatef(Xr, 1.0f, 0.0f, 0.0f);
glRotatef(Yr, 0.0f, 1.0f, 0.0f);
glRotatef(Zr, 0.0f, 0.0f, 1.0f);
glTranslatef(Xp, Yp, Zp);
Zr must be 0.0f

(I regret thoses times when i had a chance to really understand math…)

I don’t understand, if Zr must be 0 then why do that rotation with it? So you really only want (Xr,Yr) correct?

Ah… I understand.

Well this problem is a bit more complicated. But since its math, all functions work both ways ! Somehow you must try to reverse the function I’ve given in my tutorial to get the values you want. But personally I wouldn’t want to go there, because there are 3 equations with 3 unknown variables with lots of sine and cosine in it. So IF you can do it some other way, do it because what you want is very difficult. Not impossible though.

Why do you want this anyway ?

Edo

Actually this problem can be reduced to just two equations in two unknowns. He has already said that Zr=0. But still there are an infinite number of solutions. Of course using the standard definitions of arcsine and arccosine will reduce it to just a few solutions. A further restriction on one of the remaining variables’ domain will get it down to a one to one solution. Look for cartesian to spherical conversions to gain some insight into the solution. Hehe. Give a man a fish and he eats for a day. Teach a man to fish and he eats for a lifetime I always say (ok so I really don’t).

Here is a function I dug out of some of my old code that does something like this.

void SnapViewToEntity(Player *viewer, EntityObj *ent)
{
float tmp[3];
VectorSubtract(ent->position,viewer->position,tmp);
MakeNormalized(tmp);

VectorCopy(viewer->eye,tmp);

viewer->roll=0.0; // no roll please
viewer->pitch=(float)asin(-(double)tmp[2]);

double cp=cos((double)viewer->pitch);
if(cp==0.0f)
viewer->yaw=0;
else
viewer->yaw=(float)asin((double)tmp[1]/cp);
if(tmp[0]<0.0)
{
if(tmp[1]<0.0)
viewer->yaw=-PI-viewer->yaw;
else
viewer->yaw=PI-viewer->yaw;
}
}

Of course you’ll likely have to make changes to it as your app may not be using the same coordinate system my app did. Ignore the code concerning the eye, I used that for something else.

[This message has been edited by DFrey (edited 11-02-2000).]

And the winnerz are… DFrey and Haust !!

Yep DFrey you’re code is the right one, but while i was waiting for some solution i did some tests (switch from a cos to a sin, add this, sub that…) and it finally worked !!

However i’m sure that this thread will help many coders, and your code is very clean (mine is quite a mess)…

Oh bye the way : all this is for a little strategy/RPG game i’m working on. It needs free camera and target camera. Now i’ve got both i can go on…

Thanks coders !!