#2 Members - Reputation: 368
Posted 10 May 2012 - 02:33 PM
Get the position of the human and and the position of the tank, subtract the two to get a vector pointing from the human to the tank, use that vector to rotate the human.
Edited by Tispe, 10 May 2012 - 02:39 PM.
#3 Members - Reputation: 357
Posted 10 May 2012 - 03:53 PM
D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);
D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);
D3DXVECTOR3 distance = dest - src;
Now, how can I use D3DXMatrixRotationYawPitchRoll() to do the actual rotation towards the other mesh?
#5 Members - Reputation: 357
Posted 11 May 2012 - 05:29 AM
D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);
D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);
D3DXVECTOR3 distance = dest - src;
D3DXMatrixRotationYawPitchRoll(&matRotation, Yaw, Pitch, Roll); // Change this line for rotation
Thanks,
Edited by Medo3337, 11 May 2012 - 05:29 AM.
#7 Members - Reputation: 357
Posted 11 May 2012 - 07:22 AM
I tried:
D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);
D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);
D3DXVECTOR3 distance = dest - src;
D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x), 0.0f, 0.0f);
It doesn't rotate at all.
Edited by Medo3337, 11 May 2012 - 07:23 AM.
#8 Members - Reputation: 368
Posted 11 May 2012 - 11:54 AM
Are the meshes moving? Might be that it rotates but you dont see it as the model might be 90 degrees off or something.
btw, you gotta check which quadrant the distance vector is in and apply rules if your using this method.
#9 Members - Reputation: 357
Posted 12 May 2012 - 01:47 AM
The code is similar to the following:
D3DXVECTOR3 dest(m_entity[3]->posX, m_entity[3]->posY, m_entity[3]->posZ); // Box
D3DXVECTOR3 src(m_entity[5]->posX, m_entity[5]->posY, m_entity[5]->posZ); // Tank
D3DXVECTOR3 distance = dest - src;
rotX = atan(distance.y / distance.x);
D3DXMatrixRotationYawPitchRoll(&matRotation, D3DXToRadian(rotX), D3DXToRadian(rotY), D3DXToRadian(rotZ));
The tank doesn't even rotate no matter where I move the box.
The tank is not moving, I am only moving the box and trying to make the tank rotate till it look at the box.
Edited by Medo3337, 12 May 2012 - 12:08 PM.
#10 Members - Reputation: 384
Posted 12 May 2012 - 09:26 PM
What I found I needed to do in this case was I first needed a direction vector for how the mesh was facing when exported this is your refference direction.
d3vector3 refdir(1.0f, 0.0f, 0.0f);//the direction the mesh is exported in(in this case facing the +x direction);
What you do next is using the objects current position in the world and the target(what you want to look at).
d3vector3 heading = obj->pos - target;//play with this it may need to be the other way around.
D3DXVec3Normalize(&heading, &heading);
//first determine the angle between the heading vector and the target
FLOAT dot = D3DXVec3Dot(&heading, &target);
dot = Clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1
double Angle = acos(dot);
int s = Sign(heading, target);
//im rotating on the Y
D3DXMatrixRotationY(&Rotation, Angle * s);
int Sign(const D3DXVECTOR3& v1, const D3DXVECTOR3& v2)
{
//only good in 2d
if (v1.z*v2.x > v1.x*v2.z)
{
return -1;
}
else
{
return 1;
}
}
this is one way, may not be the best but it should get you going in the right directions.
#12 Members - Reputation: 357
Posted 13 May 2012 - 03:04 AM
D3DXVECTOR3 dest(m_entity[3]->posX, m_entity[3]->posY, m_entity[3]->posZ); // Box
D3DXVECTOR3 src(m_entity[5]->posX, m_entity[5]->posY, m_entity[5]->posZ); // Tank
D3DXVECTOR3 refdir(1.0f, 0.0f, 0.0f); //the direction the mesh is exported in(in this case facing the +x direction);
D3DXVECTOR3 heading = src - dest; //play with this it may need to be the other way around.
D3DXVec3Normalize(&heading, &heading);
//first determine the angle between the heading vector and the target
FLOAT dot = D3DXVec3Dot(&heading, &refdir);
dot = clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1
double Angle = acos(dot);
int s = Sign(heading, dest);
//im rotating on the Y
D3DXMATRIX matCRot;
float rot = Angle * s;
D3DXMatrixRotationY(&matCRot, rot);
#15 Members - Reputation: 442
Posted 14 May 2012 - 04:10 AM
float f = 0;
void EveryFrame()
{
f += 0.05f;
D3DXMATRIX matrix;
D3DXMatrixRotationY(&matrix, f);
pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
pd3dDevice->Present();
}
Then you know whether the problem is to do with updating the wrong matrix or something
#16 Members - Reputation: 357
Posted 14 May 2012 - 09:18 AM
The problem is with the rotation code not with giving the device the matrix, If I put the line 3DXMatrixRotationY(&matrix, 100.0f); it WILL rotate, and with the code that ankhd provided it DOES rotate but not looking directly at the other mesh.
#20 Members - Reputation: 357
Posted 15 May 2012 - 07:58 AM
Edited by Medo3337, 17 May 2012 - 11:03 AM.






