Rotating mesh till it look at another mesh

Started by
20 comments, last by Medo Mex 11 years, 11 months ago
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++)
Advertisement
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.
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?
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.
@Tispe: I am not sure about what you mean, can you please complete the following code to make the rotation:

[background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, Yaw, Pitch, Roll); [/background][/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]// Change this line for rotation[/background][/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Thanks,[/background][/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x), Pitch, Roll);[/background][/font]

[color=#282828][font=helvetica,arial,verdana,tahoma,sans-serif]

[background=rgb(250, 251, 252)]make sure distance.x is greater then zero unless you want to divide by zero smile.png[/background][/font]

[background=rgb(250, 251, 252)]I tried:[/background]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXVECTOR3 dest(mesh1->posX, mesh1->posY, mesh1->posZ);[/background][/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXVECTOR3 src(mesh2->posX, mesh2->posY, mesh2->posZ);[/background][/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]D3DXVECTOR3 distance = dest - src;[/background][/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x)[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

, 0.0f, [/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

0.0f[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

);[/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][background=rgb(250, 251, 252)]

It doesn't rotate at all.[/background][/font]

Do you device->SetTransform(D3DTS_WORLD, &[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

matRotation[/font]); ?

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]

[background=rgb(250, 251, 252)]distance vector is in and apply rules if your using this method.[/background][/font]

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);
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.
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"]D3DXVec3Normalize(&heading, &heading);

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

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

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]int [size="2"]s = Sign(heading, target);

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

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

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

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

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

This topic is closed to new replies.

Advertisement