Vector to radians conundrum

Started by
3 comments, last by adriano_usp 18 years, 10 months ago
Hi, I am trying to develop a class to control movement of ingame objects. It is based around a camera class, in that objects are given a position vector3, lookat vector3 and up vector3. This works well on the whole, with objects respond correctly to pitching,yawing and rolling controls. However, my problem is getting the object to point in the direction of the look vector. I think I need a way to convert the Lookat vector into the set of 3 angles in radians, to use in D3DXMatrixRotationYawPitchRoll. Anyone else puzzled over this? Many thanks in advance Simon
Advertisement
If you do some dot product math with your look at vector normal (normalize your lookat vector, if its not already) and some axix vectors (<0,0,1>, <0,1,0>, 1,0,0>) which are normals, you will get the cosine of the angle between the two axes. You can convert this to radians/degrees with some simple math from there, then just assemble your X, Y and Z angles into your struct or whatever, and viola.

I would give more formulas, but its really late and I would probably get them all wrong. ;)
For a pure maths answer, you could try asking the folks in Maths and Physics...

As a long shot, you might be able to use elements of the polar coordinates system...

I forget exact formulae for a 3D sphere, but the circle is:

X = r*Cos(Theta)
Y = r*Sin(Theta)

For a unit sphere, you can eliminate r and calculate Theta from the appropriate inverses.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Still strugglin with this one.....

I've tried various attempts at the dot product version, slightly better but, stil confused motion and pointing in entirely the wrong direction! plus it flips over occasionally ( a problem with using euler angel approach i understand).

So I have now tried this - building a matrix from my vectors:

m_matObjWorld[0,0] = mRight.x;
m_matObjWorld[0,1] = mRight.y;
m_matObjWorld[0,2] = mRight.z;
m_matObjWorld[0,3] = 0.0f;
m_matObjWorld[1,0] = mUp.x;
m_matObjWorld[1,1] = mUp.y;
m_matObjWorld[1,2] = mUp.z;
m_matObjWorld[1,3] = 0.0f;
m_matObjWorld[2,0] = mLook.x;
m_matObjWorld[2,1] = mLook.y;
m_matObjWorld[2,2] = mLook.z;
m_matObjWorld[2,3] = 0.0f;
m_matObjWorld[3,0] = mPos.x;
m_matObjWorld[3,1] = mPos.y;
m_matObjWorld[3,2] = mPos.z;
m_matObjWorld[3,3] = 1.0f;

This produces terrible results, poly's flash in and out of existence over the whole screen!

Maybe Its time i learnt quaternions!

Simon
Hi Simon,

If you want to get the angles that a vector makes with the world axes, you can proceed exactly like Bonehed316 said. For example:
// World axesD3DXVECTOR3 Xaxis (1.0f, 0.0f, 0.0f);D3DXVECTOR3 Yaxis (0.0f, 1.0f, 0.0f);D3DXVECTOR3 Zaxis (0.0f, 0.0f, 1.0f);// For a known V vector, you can extract the "directional angles" doing:D3DXVec3Normalize(&V,&V);angle_with_Xaxis = acosf( D3DXVec3Dot(&V,&Xaxis) );angle_with_Yaxis = acosf( D3DXVec3Dot(&V,&Yaxis) );angle_with_Zaxis = acosf( D3DXVec3Dot(&V,&Zaxis) );


Actually I don't know if I understood exactly your problem, but perhaps you would like to make a vector points in the direction of another. Suppose that you want a V1 vector points in the direction of a V2 vector, so the steps are:

1) Extract the angle (angV1V2) between V1 and V2:
D3DXVECTOR3 V1n, V2n;D3DXVec3Normalize(&V1n,&V1);D3DXVec3Normalize(&V2n,&V2);angV1V2 = acosf( D3DXVec3Dot(&V1n,&V2n) );


2) Generate a normal vector (N) to them:
D3DXVECTOR3 N;D3DXVec3Cross(&N,&V1,&V2);


3) Rotate V1 around N by angV1V2:
D3DXMATRIX matRot;D3DXMatrixRotationAxis(&matRot,&N,angV1V2);D3DXVec3TransformCoord(&V1,&V1,&matRot);


An important note about camera:
It is a very common error to think that the direction where the camera points and the lookAt vector direction are the same (I'm not saying that this is your case). Actually the lookAt vector is defined from the world origin to the camera's target. The direction where the camera points is defined by LookAt - Eye.

[Edited by - adriano_usp on June 2, 2005 4:06:12 PM]

This topic is closed to new replies.

Advertisement