# Rotating mesh till it look at another mesh

This topic is 2040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can I make a mesh rotate till it LOOK at another mesh?

For example: If I have a tank mesh and I want it till it look at a human or a building, how can I do that?

I am using DirectX 9 (C++)

##### Share on other sites
edit: missed the question completly lol

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

##### Share on other sites
I have to only rotate the tank on "Yaw", here is what I got:

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?

##### Share on other sites
Assume the X and Y component of the vector calculated is the horizontal plane simply use the tangent on the two components and you should get the Yaw angle of the vector.

##### Share on other sites
@Tispe: I am not sure about what you mean, can you please complete the following code to make the rotation:

[left][background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, Yaw, Pitch, Roll); [/background][/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]// Change this line for rotation[/background][/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]Thanks,[/background][/size][/font][/color][/left] Edited by Medo3337

##### Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, [b]atan(distance.y/distance.x)[/b], Pitch, Roll);[/background][/size][/font][/color]

[color=#282828][font=helvetica,arial,verdana,tahoma,sans-serif][size=3][background=rgb(250, 251, 252)]make sure distance.x is greater then zero unless you want to divide by zero [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img][/background][/size][/font][/color] Edited by Tispe

##### Share on other sites
[left][background=rgb(250, 251, 252)]I tried:[/background][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/size][/font][/color][/left]
[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x)[/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3], 0.0f, [/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]0.0f[/size][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]);[/size][/font][/color][/left]

[left][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)][size=3]It doesn't rotate at all.[/size][/background][/font][/color][/left] Edited by Medo3337

##### Share on other sites
Do you device->SetTransform(D3DTS_WORLD, &[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3]matRotation[/size][/font][/color]); ?

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 [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][background=rgb(250, 251, 252)]distance vector is in and apply rules if your using this method.[/background][/size][/font][/color]

##### Share on other sites
Of course I am using SetTransform(), and both meshes could be moving or standing.

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);

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

##### Share on other sites
Hi.

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.

[size="2"][size="2"][color="#008000"][size="2"][color="#008000"]//first determine the angle between the heading vector and the target[/color][/size][/color][/size][/size]
[size="2"][size="2"]dot = Clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1[/size][/size]

[size="2"][size="2"][color="#0000ff"][size="2"][color="#0000ff"]double[/color][/size][/color][/size][size="2"] Angle = acos(dot);[/size][/size]

[size="2"] //im rotating on the Y[/size]
[size="2"]D3DXMatrixRotationY(&Rotation, Angle * s);[/size]
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]int [/color][/size][/color][/size][size="2"]Sign([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] D3DXVECTOR3& v1, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] D3DXVECTOR3& v2)[/size]
[size="2"]{[/size]
//only good in 2d
[size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"] (v1.z*v2.x > v1.x*v2.z)[/size]
[size="2"]{ [/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] -1;[/size]
[size="2"]}[/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]else[/color][/size][/color][/size]
[size="2"]{[/size]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] 1;[/size]
[size="2"]}[/size]

}
[size="2"]this is one way, may not be the best but it should get you going in the right directions.[/size]

##### Share on other sites
damn it cat edit my post there is a error in this line
[size="2"]change it to[/size]

##### Share on other sites
Unfortuantely, I didn't get it to work well, I am using the following code:

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.

//first determine the angle between the heading vector and the target
dot = clamp( dot, - 1.0f, 1.0f ); //just clamps it with in 2 ranges -1 and 1

double Angle = acos(dot);

//im rotating on the Y
D3DXMATRIX matCRot;
float rot = Angle * s;
D3DXMatrixRotationY(&matCRot, rot);

##### Share on other sites
that should face the tank to the target whats it doing.
you will need to clap the angle so you can increament it, if you want to rotate slowll towards the target

##### Share on other sites
The problem is it doesn't rotate the tank towards the box at all, I tried to move the box but the tank doesn't look at it.

##### Share on other sites
Make sure rotations are working by setting the rotation to a value that increases every frame

[code]
float f = 0;

void EveryFrame()
{
f += 0.05f;
D3DXMATRIX matrix;
D3DXMatrixRotationY(&matrix, f);
pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
pd3dDevice->Present();
}
[/code]

Then you know whether the problem is to do with updating the wrong matrix or something

##### Share on other sites
@hupsilardee: I am pretty sure that I don't have that problem

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.

##### Share on other sites
If my code rotates the tank and your using the refangle I set then may be the tank is not exported facing the x + direction first find what way the tank is exported first. That should be where the error is.

##### Share on other sites
@ankhd: The tank is exported as .x file and it's looking at the "front" direction, I am trying to make the tank look at the box DIRECTLY no matter where is the box located, maybe you haven't tested the code.

##### Share on other sites
I dont think there is a front I only know X Y Z find what wat the mesh is and then get back dont just say fron , back left or right.

##### Share on other sites
@ankhd: In 3Ds Max were the mesh was created, there are 4 viewports, when I look at the "Front" viewport, I see the tank looking at me, when I look at left or right viewport I see the left and the right sides of the tank and when I look at the "Back" viewport I see the back side of the tank, thats what I am trying to explain. Edited by Medo3337

##### Share on other sites

first take all rotations from the tank, place some markers in the +x and + z and +y within your own 3d world, load your tank see what direction its pointing.
And yes its working in my code, my unit faces the target.(my world plane is x z, y up) if your worlds coord not match change the x,z, y params around(and thats enough guessing for me).

##### Share on other sites
I am not sure what you mean, but I think my 3D world is working right, here is what happens:

Camera position:
m_vEyePt = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_vLookAtPt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_vRight = D3DXVECTOR3(0.0f, 1.0f, 0.0f);

X, Y and Z:

Y+ move the mesh up towards the sky
Y- move the mesh down towards the ground

X+ move the mesh to the left
X- move the mesh to the right

Z+ move the mesh to back
Z- move the mesh to front

Of course if I moved the camera position the above will not be the same.

##### Share on other sites

This topic is 2040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.