Jump to content
• Advertisement

# Rotating mesh till it look at another mesh

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
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. Edited by Tispe

#### Share this post

##### 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 this post

##### 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 this post

##### Share on other sites
@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)]Thanks,[/background][/font] Edited May 11, 2012 by Medo3337

#### Share this post

##### Share on other sites
[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 [/background][/font] Edited May 11, 2012 by Tispe

#### Share this post

##### Share on other sites

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

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

### It doesn't rotate at all.[/background][/font] Edited May 11, 2012 by Medo3337

#### Share this post

##### Share on other sites
Do you device->SetTransform(D3DTS_WORLD, &[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]

#### Share this post

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

#### Share this post

##### 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"]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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632991
• Total Posts
3009753
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!