Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

custom matrix

Started by csisy Nov 21, 2009 at 3:52 AM 5 replies 1k views
Original Post
csisy
csisy
hi. I tried to make a custom matrix. I need help to some function: RotationYawPitchRoll(...) CreateLookAt(...) CreatePerspectiveFOView(...) here is my matrix:

struct Matrix
{		
public:
	float m[4][4];
	
	inline static Matrix Identity() throw()
	{
		Matrix mat;

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
				mat.m[j] = 0;
			mat.m = 1;
		}

		return mat;
	}

	inline static Matrix Null() throw()
	{
		Matrix mat;

		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				mat.m[j] = 0;

		return mat;
	}

	inline Matrix operator* (const Matrix& mat) throw()
	{
		Matrix result = Matrix::Null();

		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				for (int k = 0; k < 4; k++)
					result.m[j] += m[k] * mat.m[k][j];

		return result;
	}

	inline static Matrix Scale(const float& x, const float& y, const float& z) throw()
	{
		Matrix result = Matrix::Null();

		result.m[0][0] = x;
		result.m[1][1] = y;
		result.m[2][2] = z;
		result.m[3][3] = 1.0f;
			
		return result;
	}

	inline static Matrix Translate(const Vector3& v) throw()
	{
		Matrix result = Matrix::Identity();

		result.m[3][0] = v.X;
		result.m[3][1] = v.Y;
		result.m[3][2] = v.Z;
		result.m[3][3] = 1.0f;

		return result;
	}

	inline static Matrix RotationX(const float& radians) throw()
	{
		Matrix result = Matrix::Identity();

		float s = sin(radians);
		float c = cos(radians);

		result.m[1][1] = c;
		result.m[1][2] = s;
		result.m[2][1] = -s;
		result.m[2][2] = c;

		return result;
	}

	inline static Matrix RotationY(const float& radians) throw()
	{
		Matrix result = Matrix::Identity();

		float s = sin(radians);
		float c = cos(radians);

		result.m[0][0] = c;
		result.m[0][2] = -s;
		result.m[2][0] = s;
		result.m[2][2] = c;

		return result;
	}

	inline static Matrix RotationZ(const float& radians) throw()
	{
		Matrix result = Matrix::Identity();

		float s = sin(radians);
		float c = cos(radians);

		result.m[0][0] = c;
		result.m[0][1] = s;
		result.m[1][0] = -s;
		result.m[1][1] = c;

		return result;
	}
};



[Edited by - csisy on November 21, 2009 4:17:31 AM]
sorry for my bad english
Stani R
Stani R
Show us what you tried and which parts do not work. The first can be done with axis-angles and/or quaternions (or even Euler angles). The second I'm not sure of but generally I think you'd take difference in position as position and rotate to align with this vector. The third one is a well-documented function which you can just copy from here.
csisy
csisy
Quote:
Original post by lightbringer
Show us what you tried and which parts do not work. The first can be done with axis-angles and/or quaternions (or even Euler angles). The second I'm not sure of but generally I think you'd take difference in position as position and rotate to align with this vector. The third one is a well-documented function which you can just copy from here.

To RotationYawPitchRoll(...) I think, simply multiple RotationY(...) RotationX(...) and RotationZ(...), but it didn't work :)
I haven't idea to LookAt(...), but I'll try create it (maybe I'll debug the basic D3DXMatrixLookAtLH(...) values...)
Thanks for the link, and the reply. :)

EDIT:
Maybe this? :
http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml
sorry for my bad english
Stani R
Stani R
Yeah, that should work fine.

For rotations I still don't know how you're doing it (ie how does it "not work") but also keep in mind that if you rotate an already rotated object then the rotations will be in object space. Is that how it looks like when you try it? Also I think the correct order should be X,Y,Z (or roll, pitch, yaw) but don't quote me on that :)
csisy
csisy
Quote:
Original post by lightbringer
Yeah, that should work fine.

For rotations I still don't know how you're doing it (ie how does it "not work") but also keep in mind that if you rotate an already rotated object then the rotations will be in object space. Is that how it looks like when you try it? Also I think the correct order should be X,Y,Z (or roll, pitch, yaw) but don't quote me on that :)



Well... I'm using directx:

D3DXMATRIX dm;D3DXMatrixRotationYawPitchRoll(&dm, 1, 2, 3);D3DXMATRIX m1, m2, m3, m4;D3DXMatrixRotationX(&m1, 1);D3DXMatrixRotationY(&m2, 2);D3DXMatrixRotationZ(&m3, 3);m4 = m1 * m2 * m3;

and m4 != dm;

And the correct projection matrix (Left-handed) :

inline static Matrix PerspectiveFOView(const float& fov, const float& aspect, const float& nPlane, const float& fPlane) throw(){	Matrix mat = Matrix::Null();	float f = 1 / tan(fov/2);	mat.m[0][0] = f / aspect;	mat.m[1][1] = f;	mat.m[2][2] = -(fPlane * nPlane) / (nPlane - fPlane);	mat.m[2][3] = 1.0f;	mat.m[3][2] = -1.0f;	return mat;}
sorry for my bad english
Stani R
Stani R
Try Z * X * Y, that's what it does internally, or so the documentation says. Which also means I messed up on the XYZ order (but got it right in terms of actual function names... seems pretty stupid to me now that I read it again. Sorry, it's been a long day/night).
csisy
csisy
Quote:
Original post by lightbringer
Try Z * X * Y, that's what it does internally, or so the documentation says. Which also means I messed up on the XYZ order (but got it right in terms of actual function names... seems pretty stupid to me now that I read it again. Sorry, it's been a long day/night).

Yeah :)
Z * X * Y = yawpitchroll rotate.
Thanks for the help. :)
sorry for my bad english

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.