# Mesh rotation and translation

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

## Recommended Posts

im writitng a small mesh class that stores my model, and i have methods that yaw roll and pitch, and also translate the object. there seems to be something wrong though when i try and move the object and rotate it... rotations dont seem to go the right way....this is my code
void Mesh::translate(float x, float y, float z)
{
pos += right * x;
pos += up * y;
pos += look * z;
}
void Mesh::translate(D3DXVECTOR3 trans)
{
pos += right * trans.x;
pos += up * trans.y;
pos += look * trans.z;
}

void Mesh::setPosition(float x, float y, float z)
{
pos.x = x;
pos.y = y;
pos.z = z;
}
void Mesh::setPosition(D3DXVECTOR3 pos)
{
pos.x = pos.x;
pos.y = pos.y;
pos.z = pos.z;
}

{
D3DXMATRIX T;

D3DXVec3TransformCoord(&right,&right,&T);
D3DXVec3TransformCoord(&look,&look,&T);
}
void Mesh::yaw(Degree angle)
{
D3DXMATRIX T;

D3DXVec3TransformCoord(&right,&right,&T);
D3DXVec3TransformCoord(&look,&look,&T);
}

{
D3DXMATRIX T;

D3DXVec3TransformCoord(&up,&up,&T);
D3DXVec3TransformCoord(&look,&look,&T);
}
void Mesh::pitch(Degree angle)
{
D3DXMATRIX T;

D3DXVec3TransformCoord(&up,&up,&T);
D3DXVec3TransformCoord(&look,&look,&T);
}

{
D3DXMATRIX T;

D3DXVec3TransformCoord(&right,&right,&T);
D3DXVec3TransformCoord(&up,&up,&T);
}
void Mesh::roll(Degree angle)
{
D3DXMATRIX T;

D3DXVec3TransformCoord(&right,&right,&T);
D3DXVec3TransformCoord(&up,&up,&T);
}

void Mesh::getTransMatrix(D3DXMATRIX* V)
{
//keep the camera axes orthogonal to each other
D3DXVec3Normalize(&look,&look);

D3DXVec3Cross(&up,&look,&right);
D3DXVec3Normalize(&up,&up);

D3DXVec3Cross(&right,&up,&look);
D3DXVec3Normalize(&right,&right);

(*V)(0,0) = right.x;
(*V)(0,1) = up.x;
(*V)(0,2) = look.x;
(*V)(0,3) = 0.0f;

(*V)(1,0) = right.y;
(*V)(1,1) = up.y;
(*V)(1,2) = look.y;
(*V)(1,3) = 0.0f;

(*V)(2,0) = right.z;
(*V)(2,1) = up.z;
(*V)(2,2) = look.z;
(*V)(2,3) = 0.0f;

(*V)(3,0) = pos.x;
(*V)(3,1) = pos.y;
(*V)(3,2) = pos.z;
(*V)(3,3) = 1.0f;

}

void Mesh::update()
{
D3DXMATRIX V;
getTransMatrix(&V);

device->SetTransform(D3DTS_WORLD,&V);
for(int i = 0; i < mtrl.size(); i++)
{
device->SetMaterial(&mtrl);
device->SetTexture(0,texture);
mesh->DrawSubset(i);
}
}

is anything wrong with this..thanks

##### Share on other sites
Try this:

(*V)(3,0) = - D3DXVec3Dot(&pos, &right);
(*V)(3,1) = - D3DXVec3Dot(&pos, &up);
(*V)(3,2) = - D3DXVec3Dot(&pos, &look);

##### Share on other sites
I don't see the point in Thergothon's suggestion.

I am wondering why don't you use a single 4x4 matrix to store the position and transformation ? Obviously your code is trying to do the same thing.

I'll save you from much of headache, such as this.

##### Share on other sites
Quote:
 Original post by Demus79I don't see the point in Thergothon's suggestion.I am wondering why don't you use a single 4x4 matrix to store the position and transformation ? Obviously your code is trying to do the same thing.I'll save you from much of headache, such as this.

Quote to emphasize

You could achive the same result with less then 10 lines of code:

D3DXMATRIX ModelMatrix, Translation, Rotation;
D3DXMatrixRotationYawPitchRoll(&Rotation, RotY ,RotX ,RotZ );
D3DXMatrixTranslation(&Translation ,TranslX,TranslY,TranslZ);
D3DXMatrixMultiply(&ModelMatrix,&Rotation,&Translation);

Device->SetTransform(D3DTS_WORLD, &ModelMatrix);

This might not work at first, I did no testing.

##### Share on other sites
i dont understand why the method im using....whiich worked fine for the camera...doesnt work with the mesh....it moves properly but it seems like when i am rotating the model, the model axis are rotating properly but the model its self is rotating the opposite way....so if i yaw my object 50 degrees, it yaws -5 degrees, and then when i try to translate the mesh, it moves in the direction it should be going, but the mesh is not facing that direction anymore

thanks for your suggestions....i tried both of them....

calins...when i used those lines of code you gave me the mesh would rotate to a certain degree than move back the other way....for RotY,RotX,Rotz i put in look.y,up.x,right.z i wasnt sure if that was right though

##### Share on other sites
Quote:
 calins...when i used those lines of code you gave me the mesh would rotate to a certain degree than move back the other way....for RotY,RotX,Rotz i put in look.y,up.x,right.z i wasnt sure if that was right though

I made a quick check and it worked as expected, this is MDX code but it does the same as what I posted above:

ModelMatrix = Matrix.Multiply(Matrix.RotationYawPitchRoll(RY,RX,RZ),Matrix.Translation(TX,TY,TZ));

device.Transform.World = ModelMatrix;

Note that if you rotate the object around all axis the Matrix gets messed up and will probably behave different than what you're expecting. Use only two axis for rotations (i.e. RY and RX).
Onother thing: the code I'm proposing will not suit all needs, It all depends how do you want to manipulate the object/camera.

##### Share on other sites
alright i have the model rotating properly now...but i need to get it translating based on the direction its facing....

i cant seem to get that working though

##### Share on other sites
Sorry for my last post. I was a little drunk and misread the question. I thought you were talking about translating with respect to a facing direction (because of the up, right and look vectors). The snippet I posted was from my camera class that does this, so sorry about that.

However, given your last post, you should try it and see if it works.

1. 1
Rutin
22
2. 2
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633308
• Total Posts
3011293
• ### Who's Online (See full list)

There are no registered users currently online

×