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

matrix math

Started by tendifo Jul 31, 2006 at 8:40 PM 0 replies 400+ views
Original Post
tendifo
tendifo
Ok, so I've got a nasty run-time bug in my program that I can't seem to track down. Everything looks okie dokie in my matrix math, the calculations and what not, but when I run it everything seems whacko. Maybe you guys could help me out. Can someone check the math here? If these all look good then my bug is somewhere else. I have checked multiple sources to double check, but maybe I overlooked something? Thanks.
Matrix Matrix::operator*(Matrix &rhs)
{
	Matrix mat;
	for (int i = 0; i < 3; i += 1)
	{
		for (int j = 0; j < 3; j += 1)
		{
			float sum = 0;
			for (int k = 0; k < 3; k += 1)
			{
				sum += data[k] * rhs[k][j];
			}
			mat[j] = sum;
		}
	}
	return mat;
}

float Matrix::determinant()
{
	float det;
	float a = data[0][0]; float b = data[0][1]; float c = data[0][2];
	float d = data[1][0]; float e = data[1][1]; float f = data[1][2];
	float g = data[2][0]; float h = data[2][1]; float i = data[2][2];
	det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
	return det;
}

Matrix Matrix::inverse()
{
	Matrix mat;
	float a = data[0][0]; float b = data[0][1]; float c = data[0][2];
	float d = data[1][0]; float e = data[1][1]; float f = data[1][2];
	float g = data[2][0]; float h = data[2][1]; float i = data[2][2];
	mat[0][0] = e * i - f * h;
	mat[0][1] = c * h - b * i;
	mat[0][2] = b * f - c * e;
	mat[1][0] = f * g - d * i;
	mat[1][1] = a * i - c * g;
	mat[1][2] = c * d - a * f;
	mat[2][0] = d * h - e * g;
	mat[2][1] = b * g - a * h;
	mat[2][2] = a * e - b * d;
	mat = mat * (1 / determinant());
	return mat;
}
Dmytry
Dmytry
try multiplying or inverting some matrices and comparing to matrix math lib written by somebody else (which you probably should be using if you have problems doing it yourself).

Topic Locked

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

Sign in to reply to this topic.