Problems rotating and moving mesh...

Started by
7 comments, last by StoneDevil 18 years ago
hi...ive been trying to create a mesh class that loads in my mesh and thenyou can translate it and yaw, pitch, and roll. im using three vector3 to control the orientation of the object, up, right, and look. the object moves fine and it will rotate fine except that when the mesh is drawn, it is rotated the opposite way of its orientation. for example i yaw him left but the object yaws right, the orientation however has rotated properly, so im pretty sure the problem is when im creating my world translation matrix before drawing my object...im going to post my whole code just incase its not that...

Mesh::Mesh(char* filename,IDirect3DDevice9* _device)
{
	//code that creates the mesh

	look = D3DXVECTOR3(0,0,1);
	up = D3DXVECTOR3(0,1,0);
	right = D3DXVECTOR3(1,0,0);
	pos = D3DXVECTOR3(0,0,0);
}

Mesh::~Mesh()
{
	d3d::Release<ID3DXMesh*>(mesh);

	for(int i = 0; i < texture.size(); i++)
		d3d::Release<IDirect3DTexture9*>( texture );
}

void Mesh::translate(float x, float y, float z)
{
	pos += right * x;
	pos += up * y;
	pos += look * z;
}

void Mesh::setPosition(float x, float y, float z)
{
	pos.x = x;
	pos.y = y;
	pos.z = z;
}
	
void Mesh::yaw(Radian angle)
{
	D3DXMATRIX T;

	D3DXMatrixRotationAxis(&T,&up,angle.radian);
	D3DXVec3TransformCoord(&right,&right,&T);
	D3DXVec3TransformCoord(&look,&look,&T);
}

void Mesh::pitch(Radian angle)
{
	D3DXMATRIX T;

	D3DXMatrixRotationAxis(&T,&right,angle.radian);
	D3DXVec3TransformCoord(&up,&up,&T);
	D3DXVec3TransformCoord(&look,&look,&T);
}

void Mesh::roll(Radian angle)
{
	D3DXMATRIX T;

	D3DXMatrixRotationAxis(&T,&look,angle.radian);
	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);
	}
}
[Edited by - StoneDevil on April 19, 2006 12:41:16 PM]
Advertisement
The cause is most likely your projection matrix. I say "cause" because it is not a problem. DirectX allows you to have a left-handed or right-handed coordinate system.

Right-handed: X-axis Cross Y-axis = Z-axis
Left-handed: Y-axis Cross X-axis = Z-axis

I prefer left-handed (as do most of the tutorials I've seen). The simple fix is to cross all your vectors in the reversed order. (Edit:) And negate all rotating angles.

Hope that helps.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
when i reversed the cross calculations it flipped my model upside down and also inversed the faces on the model...
Well.. I guess that wasn't an easy fix then. Can you tell me how you are generating your projection matrix: D3DXCreateProjectionSomething and the parameters?

-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
//set the projection matrix
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.3, 1.3333333,0.1,1000.0);
device->SetTransform(D3DTS_PROJECTION,&proj);

this is the projection matrix i set when i first create the camera
So you are using a left-handed coordinate system. So any time you do a cross product, you will have to use the left-hand rule to figure out which direction the output vector will point. I.e: vForward X vRight = vUp. I just noticed that you you are crossing your vectors correctly in your original post (correct for left-handed that is).

About you original question, since you are using a left-handed coordinate system, your rotations will be in the opposite direction (I'm pretty sure). Don't worry about it, just start thinking in left-handed terms since you are using a left-handed system.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
its a problem though that it roates the mesh to opposite way because when i try to translate the model forward based on its orientation, the orientation is different than the model.
The problem is that you are building your transformation matrix wrong in Mesh::getTransMatrix(). D3D uses row vectors, so the matrix should be built like this:
    right.x right.y right.z 0       up.x    up.y    up.z 0     look.x  look.y  look.z 0      pos.x   pos.y   pos.z 1 
Everything else looks right.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
thanks for the replys...i was just reading some posts and realized that thats what i was doing wrong...

thanks for the help

This topic is closed to new replies.

Advertisement