[Help] matrix problem

Started by
2 comments, last by tomneo2004 14 years, 8 months ago
I got a problem with matrix, I have a object class which defined myself, object class has a variable translate(D3DXMATRIX), Mrotation_x(D3DXMATRIX),Mrotation_y(D3DXMATRIX),Mrotation_z(D3DXMATRIX),matrix(D3DXMATRIX).and there are some functions to deal with translation and rotation. what i would like to do is when user call any of function and give the value then do the rotation or translation. For example, if i invoke void Object::ObjectRotationX(float x_rotation) and give 0.5f then it will do the rotation depend on last position. But my code doesn't work. object just don't rotate or move. object class a matrix so every time whether rotation or translation it will be set to a matrix might be translate or Mrotation_x and then multiply rotation and translation matrix then set to the matrix so i can retrieve the matrix for drawing on screen. If i want to move a object to a new position, should i take it's current matrix then multiply rotation and translation eg. matrix=matrix*(rotation*translation) is there right?


Object::Object()
{
	D3DXMatrixIdentity(&matrix);
	D3DXMatrixIdentity(&translate);
	D3DXMatrixIdentity(&Mrotation_x);
	D3DXMatrixIdentity(&Mrotation_y);
	D3DXMatrixIdentity(&Mrotation_z);
}

Object::~Object()
{
}

void Object::ObjectSetPosition(float x, float y, float z)
{
	D3DXMatrixIdentity(&matrix);
	D3DXMatrixTranslation(&translate, x, y, z);
	matrix=translate;
}

void Object::ObjectRotationX(float x_rotation)
{
	x_degree+=x_rotation;
	D3DXMatrixRotationY(&Mrotation_x, x_degree);
	ObjectUpdateMatrix();
}

void Object::ObjectRotationY(float y_rotation)
{
	y_degree+=y_rotation;
	D3DXMatrixRotationY(&Mrotation_y, y_degree);
	ObjectUpdateMatrix();
}

void Object::ObjectRotationZ(float z_rotation)
{
	z_degree+=z_rotation;
	D3DXMatrixRotationZ(&Mrotation_z, z_degree);
	ObjectUpdateMatrix();
}

void Object::ObjectTranslationX(float _x)
{
	x+=_x;
	D3DXMatrixTranslation(&translate, x, 0.0f, 0.0f);
	ObjectUpdateMatrix();
}

void Object::ObjectTranslationY(float _y)
{
	y+=_y;
	D3DXMatrixTranslation(&translate, 0.0f, y, 0.0f);
	ObjectUpdateMatrix();
}

void Object::ObjectTranslationZ(float _z)
{
	z+=_z;
	D3DXMatrixTranslation(&translate, 0.0f, 0.0f, z);
	ObjectUpdateMatrix();
}

void Object::ObjectUpdateMatrix()
{

	matrix=Mrotation_x*Mrotation_y*Mrotation_z*translate;

	D3DXMatrixIdentity(&Mrotation_x);
	D3DXMatrixIdentity(&Mrotation_y);
	D3DXMatrixIdentity(&Mrotation_z);
	D3DXMatrixIdentity(&translate);
}


Advertisement
One thing that looks wrong is that when you set a translation component, you create a translation matrix with only that component, instead of x,y,z.

Yes, you need to multiply the all rotation matrices and translation. See for example this tutorial. And what matrix does the object already have?
Quote:Original post by ET3D
One thing that looks wrong is that when you set a translation component, you create a translation matrix with only that component, instead of x,y,z.

Yes, you need to multiply the all rotation matrices and translation. See for example this tutorial. And what matrix does the object already have?


I define a matrix for object so if i declare 3 objects then they have their own matrix. So i can't do the translation with one particular axis, i have to do it with three axis is that you mean? I also update matrix every rotation and translation is that ok? About the matrix multiplication, i mean if a object move to a position then this object's matrix is changed and then user want it to move 2 quantities on axis so use call translation then will have a matrix which represent move 2 quantities, so do i need to take old matrix to multiply new rotation and translation matrix ?

here is Object.h

class Object{public:	Object();	~Object();	/*	@get matrix	*/	D3DXMATRIX* GetMatrix();protected:	D3DXMATRIX matrix;	D3DXMATRIX translate;	D3DXMATRIX Mrotation_x;	D3DXMATRIX Mrotation_y;	D3DXMATRIX Mrotation_z;	float x;	float y;	float z;	float x_degree;	float y_degree;	float z_degree;		/*	@to set specific position but not the rotation	@if x y z =0 then object will be set to position (0,0,0) instantly	*/	void ObjectSetPosition(float x, float y, float z);	/*	@rotate object X according to last rotation	*/	void ObjectRotationX(float x_rotation);	/*	@rotate object Y according to last rotation	*/	void ObjectRotationY(float y_rotation);	/*	@rotate object Z according to last rotation	*/	void ObjectRotationZ(float z_rotation);	/*	@translate object X according to last translation	*/	void ObjectTranslationX(float x);	/*	@translate object Y according to last translation	*/	void ObjectTranslationY(float y);	/*	@translate object Z according to last translation	*/	void ObjectTranslationZ(float z);private:	/*	@update the matrix 	@this will multiply object's matrix with rotation and tanslation then set to new matrix 	*/	void ObjectUpdateMatrix();	};
i figure it out

This topic is closed to new replies.

Advertisement