directX vector normals

lets say you have the Unit moving and camera follows it.

But if u rotate the Unit (around Y axis) and then start to move, you need different coordinate change in X and Z axis, aka new vector which is pointing to straight direction. I calculated it like this and it works correctly:
1
2
3
4
5
6
7
8
9
10
11
12
13
        D3DXVECTOR3 lookAt (0.f, 0.f, 1.f); /*watch in front of Unit (Unit is at starting position 0.f, 0.f, 0.f)*/
        D3DXMatrixIdentity (&world); /*rotation and translation for Unit and camera is the same*/
	D3DXMatrixRotationY (&buffer, rotateY);
	D3DXMatrixMultiply (&world, &world, &buffer);
	D3DXMatrixTranslation (&buffer, tranX, tranY, tranZ);
	D3DXMatrixMultiply (&world, &world, &buffer);
	D3DXVec3TransformCoord (&lookAt, &lookAt, &world);
	
	D3DXMatrixLookAtLH (&m_view, &eye, &lookAt, &up);

	//Update vectors
	straightVector = lookAt; 
/*because we move the Unit and follow it with camera then straightComponent of the velocity vector is same as where camera is looking at*/




So if we want to move sideways we need vector for that direction. This is how I got it:
1
2
3
4
5
6
7
8
9
10
11

//this code works
        strafeVector = D3DXVECTOR3(1.f, 0.f, 0.f);
	D3DXMatrixIdentity (&world);
	D3DXMatrixRotationY (&buffer, rotateY);
	D3DXMatrixMultiply (&world, &world, &buffer);
	D3DXMatrixTranslation (&buffer, tranX, tranY, tranZ);
	D3DXMatrixMultiply (&world, &world, &buffer);
	D3DXVec3TransformCoord (&strafeVector, &strafeVector, &world);

//after that on next call I scale vectors to the proper size(staright and strafe vectors are static, just like tranX, tranY and tranZ) 


translation points are updated like this:
1
2
3
        tranX += directionStraight.x + directionStrafe.x; /*directionStraight and Strafe are scaled vectorStraight and vectorStrafe to corect size*/
 	tranZ += directionStraight.z + directionStrafe.z;
	tranY = 0.f; //we're not jumping (for now) 


So I thought I can do updating the strafe vector with less code. Since I have vector normal for the plane where I am moving (up = D3DXVECTOR3 (0.f, 1.f, 0.f)), and I have vector for striaght moving, then my strafeVector for moving sideways should be cross product between them.
This is what and I can say that behavior of code is not quite normal
1
2
//this doesn't work
D3DXVec3Cross (&strafeVector, &straightVector, &up); /*vector would face to the right*/


So I tried to move the "up" vector to the translation point and then do cross product..
1
2
3
4
//nor this
        up.x = tranX;
	up.z = tranZ;
	D3DXVec3Cross (&strafeNormal, &straightNormal, &up);


.. and the result was even worse, Unit moves in one direction with exponential speed relative to world's starting point.


Can some1 explain why is the second method wrong? Or I should just shut up cuz i found working way~~
Last edited on
Topic archived. No new replies allowed.