Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Rotating mesh till it look at another mesh


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 10 May 2012 - 02:08 PM

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

Sponsor:

#2 Tispe   Members   -  Reputation: 1176

Like
0Likes
Like

Posted 10 May 2012 - 02:33 PM

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, 10 May 2012 - 02:39 PM.


#3 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 10 May 2012 - 03:53 PM

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?

#4 Tispe   Members   -  Reputation: 1176

Like
0Likes
Like

Posted 11 May 2012 - 12:48 AM

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.

#5 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 11 May 2012 - 05:29 AM

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

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.


#6 Tispe   Members   -  Reputation: 1176

Like
0Likes
Like

Posted 11 May 2012 - 06:35 AM

D3DXMatrixRotationYawPitchRoll(&matRotation, atan(distance.y/distance.x), Pitch, Roll);

make sure distance.x is greater then zero unless you want to divide by zero Posted Image

Edited by Tispe, 11 May 2012 - 06:40 AM.


#7 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

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 Tispe   Members   -  Reputation: 1176

Like
0Likes
Like

Posted 11 May 2012 - 11:54 AM

Do you device->SetTransform(D3DTS_WORLD, &matRotation); ?

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 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 12 May 2012 - 01:47 AM

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, 12 May 2012 - 12:08 PM.


#10 ankhd   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 12 May 2012 - 09:26 PM

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.

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.

#11 ankhd   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 12 May 2012 - 09:34 PM

damn it cat edit my post there is a error in this line
FLOAT dot = D3DXVec3Dot(&heading, &target);
change it to
FLOAT dot = D3DXVec3Dot(&heading, &reffdir);

#12 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 13 May 2012 - 03:04 AM

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.

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

#13 ankhd   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 13 May 2012 - 07:08 AM

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

#14 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 13 May 2012 - 07:30 AM

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.

#15 hupsilardee   Members   -  Reputation: 491

Like
0Likes
Like

Posted 14 May 2012 - 04:10 AM

Make sure rotations are working by setting the rotation to a value that increases every frame

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 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 14 May 2012 - 09:18 AM

@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.

#17 ankhd   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 14 May 2012 - 09:19 PM

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.

#18 Medo3337   Members   -  Reputation: 683

Like
-1Likes
Like

Posted 15 May 2012 - 02:40 AM

@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.

#19 ankhd   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 15 May 2012 - 02:50 AM

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.

#20 Medo3337   Members   -  Reputation: 683

Like
0Likes
Like

Posted 15 May 2012 - 07:58 AM

@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, 17 May 2012 - 11:03 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS