Question

I'm trying to invert 4x4 matrices, I've seen some examples of calculating the determinant with a few dozens of calculations, this did not seem to work for me, I might've just messed up somewhere, but I've been trying to find another way to solve this, my answer was this:

public Matrix inverse()
{
    double[] array = new double[]{
        1/m[0], m[4],   m[8],       0,
        m[1],   1/m[5], m[9],       0,
        m[2],   m[6],   1/m[10],    0,
        -m[12], -m[13], -m[14],     1/m[15]
    };
    return new Matrix(array);
}

Basicly what I do is, calculating the invert of all it's components, this has been working quite well, expect for number 15, the last number, it's been spewing out infinity for some reason, does anyone have a clue as to why? My only guess would be division by zero, but then the question, what is the inverse of a 0? Is the answer 0?

(Not sure if this is relevant, but my matrices are row-major)

Was it helpful?

Solution

Your attempt to write down the inverse of a 4x4 matrix is utterly wrong. There's absolutely no point trying to fix it since it can never work.

You ask what the result of 1/0 is. Well, that is division by zero and the result is not defined. There is no real number x that satisfies 1/0 == x. If there was then 1 == x*0 == 0, a contradiction. On a computer, attempting to perform division by zero sometimes leads to an error, or sometimes results in a special floating point value Inf being returned. The latter appears to be what happens in your environment.

I don't know why you rejected the determinant based code. Perhaps you found it tricky to implement. But that's just how it is. You aren't going to shortcut that complexity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top