Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dot product problems

Started by drb2k2 Mar 2, 2004 at 10:00 AM 6 replies 450+ views
Original Post
drb2k2
drb2k2
Hi all, I am trying to rotate a character on its axis so that it faces the way it is moving. I thought this would be easy but am having problems somewhere I can''t see. When the character is loaded I keep a vector(Look) that relates to the position it is orginally facing i.e. 0,0,-1 as it starts off facing the screen. Then I calculate a vector(move) that is in a direct line from the camera through the character, and onto its far side i.e. the direction the character will run if you press up on the keyboard. I was then hoping to get the dot product of the Look and Move vectors to calculate the angle that the character would need to be rotated by. But it doesn''t work when I rotate the character! The value returned from the dot product is usually 1.0 ( i guess meaning one radian) but when I perform the D3DXToDegree() calculation it returns 50 degrees instead of 180. I''m sure i''m missing something here. Also the character does not rotate along with the move vector like it should! Relevent code is below: Generating the move vector
//calculate a line from the camera to the mesh
		//D3DXVECTOR3 Line;
		D3DXVec3Subtract(&Line,&m_vPos,&Camera.m_CamPos);
		D3DXVec3Add(&Line, &Line, &m_vPos);
		Line.y = 0.0f;
		// Line now represents the point past the camera
		// so normalise it and the looking vector to calculate angle of rotation
		D3DXVECTOR3 temp = Line;
		D3DXVECTOR3 temp2 = m_LookingAt;

		D3DXVec3Normalize(&Line,&Line);
		D3DXVec3Normalize(&m_LookingAt, &m_LookingAt);

		dotproduct = D3DXVec3Dot(&Line, &m_LookingAt);
		m_vRot.y = D3DXToDegree(dotproduct);


		Line = temp;
		m_LookingAt = temp2;
	}
 
Then applying this transformation to the mesh
// create the matrices
D3DXMATRIX matX;
	D3DXMatrixIdentity(&matX);
	D3DXMATRIX matY;
	D3DXMatrixIdentity(&matY);
	D3DXMATRIX matZ;
	D3DXMatrixIdentity(&matZ);
	D3DXMatrixIdentity(&m_matTransformation);

//work out the rotation
	D3DXMatrixRotationY (&matY, m_vRot.y* 0.01745f);
//add it into the transformation matrix
	D3DXMatrixMultiply (&m_matTransformation, &m_matTransformation, &matY);

//work out the translation movements
	D3DXMATRIX matTrans;
	D3DXMatrixTranslation (&matTrans, m_vPos.x, m_vPos.y, m_vPos.z);
//finish it up
	D3DXMatrixMultiply (&m_matTransformation, &m_matTransformation, &matTrans);
   
BitMaster
BitMaster
Are you sure you calculate the angle correctly? From what I read out of your post, you seem to believe that dot(x, y) is the angle between the vectors x and y. However, the formula is cos(a) = dot(x, y), provided x and y are unit length.
drb2k2
drb2k2
Okay guys,
thanks for the help, its is soooooooo almost working.

The character now follows the point...but.......

It will only follow for half a rotation, then it just sits and waits for the camera to do another rotation.
Have no idea what is causing that... if you could spare just another 5 minutes I would appreciate it.
cheers
Drb2k2

New code

//calculate a line from the camera to the mesh
D3DXVec3Subtract(&Line,&m_vPos,&Camera.m_CamPos);
D3DXVec3Add(&Line, &Line, &m_vPos);
Line.y = 0.0f;
// Line now represents the point past the camera

// so normalise it and the looking vector to calculate angle of rotation
D3DXVECTOR3 temp = Line;
D3DXVECTOR3 temp2 = m_LookingAt;

D3DXVec3Normalize(&Line,&Line);
D3DXVec3Normalize(&m_LookingAt, &m_LookingAt);

dotproduct = D3DXVec3Dot(&Line, &m_LookingAt);

// get the angle by using acos
dotproduct = (float)acos(dotproduct);
//assign this to the rotation values of object
m_vRot.y = D3DXToDegree(dotproduct);



Line = temp;
m_LookingAt = temp2;
}
BitMaster
BitMaster
I''m not sure if that is your problem, but the method above only calculates angles between 0 and pi. Use the direction of cross(x, y) to determine the angle between pi and -pi.
drb2k2
drb2k2
hmmmmmmmmm, okay.

A cross product gives a perpendicular vector, but both of these vectors are on the same plane, the Y value is 0. This is the first time i''ve tried this type of calculation, so I''m probably missing something somewhere. Do you think you could jot down a code snippet or a breakdown of the process so that I can understand what you mean.
Thank you for your help so far
Drb2k2

CURRENT PROCESS:

Get Direction vector that the Camera is pointing in
Get the position vector that indicates the front of the mesh
Normalise both vectors
Calculate the dot product between these two vectors
Get the acos of this value
Convert this to degrees
Assign this to rotation value of mesh
BitMaster
BitMaster
If the vectors u and v (let them be unit length for simplicity) are in the y = 0 plane, then cross(u, v) will be either (0, 1, 0) or (0, -1, 0) (unless of course u = v or u = -v). In the first case, use angle, in the second case use -angle.
drb2k2
drb2k2
Thank you bitmaster you are a genius.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.