Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error (invert a 4*4 matrix)  (Read 1272 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Error (invert a 4*4 matrix)
« on: April 05, 2014, 09:38:31 pm »
Hi, I've a problem with this source code and I don't see where :
Matrix4f Matrix4f::inverse () throw (std::exception&) {
    Matrix4f store;
    float fa1 = m11 * m22 - m12 * m21;
    float fa2 = m11 * m23 - m13 * m21;
    float fa3 = m11 * m24 - m14 * m21;
    float fa4 = m12 * m23 - m13 * m22;
    float fa5 = m12 * m24 - m14 * m22;
    float fa6 = m13 * m24 - m14 * m23;
    float fb1 = m31 * m42 - m32 * m41;
    float fb2 = m31 * m43 - m33 * m41;
    float fb3 = m31 * m44 - m34 * m41;
    float fb4 = m32 * m43 - m33 * m42;
    float fb5 = m32 * m44 - m34 * m42;
    float fb6 = m33 * m44 - m34 * m43;
    float det = fa1 * fb6 - fa2 * fb5 + fa3 * fb4 + fa4 * fb3 - fa5 * fb2 + fa6 * fb1;
    if (Math::abs(det) <= 0.f) {
        return store;
    } else {
       store.m11 = m22 * fb6  - m23 * fb5 + m24 * fb4;
       store.m21 = -m21 * fb6  + m23 * fb3 - m24 * fb2;
       store.m31 = m21 * fb5  - m22 * fb3 + m24 * fb1;
       store.m41 = -m21 * fb4  + m22 * fb2 - m23 * fb1;

       store.m12 = -m12 * fb6  + m13 * fb5 - m14 * fb4;
       store.m22 = m11 * fb6  - m13 * fb3 + m14 * fb2;
       store.m32 = -m11 * fb5  + m12 * fb3 - m14 * fb1;
       store.m42 = m11 * fb4  - m12 * fb2 + m13 * fb1;

       store.m13 = m42 * fa6  - m43 * fa5 + m44 * fa4;
       store.m23 = -m41 * fa6  + m43 * fa3 - m44 * fa2;
       store.m33 = m41 * fa5  - m42 * fa3 + m44 * fa1;
       store.m43 = -m41 * fa4  + m42 * fa2 - m43 * fa1;

       store.m14 = -m32 * fa6  + m33 * fa5 - m34 * fa4;
       store.m24 = m31 * fa6  - m33 * fa3 + m34 * fa2;
       store.m34 = -m31 * fa5  + m32 * fa3 - m34 * fa1;
       store.m44 = m31 * fa4  - m32 * fa2 + m33 * fa1;

       float invDet = 1.f / det;
       return store * invDet;
    }
}
 

This source code is the same than the source code of jme except that I start at 1 and not 0 for the row and column numbers.

Mmm I don't think to use a framework is a good idea, often there are untested, and, there are errors in the source code.

But it's good to have a good code design.

This source code don't provide me the same result, it print me 1, 1, 11 as result and it should be 1, 1, 1 if I cancel the transform.
TransformMatrix tm;
    Vec3f p(1, 1, 1);
    //tm.setScale(Vec3f(2.f, 2.f, 2.f));
    tm.setTranslation(Vec3f(20, 10, 10));
    //tm.setRotation(Vec3f(0, 0, 1), 10);
    Vec3f pt = tm.transform(p);

    std::cout<<tm.get3DMatrix() * tm.get3DMatrix().inverse()<<std::endl;
    std::cout<<tm.inverseTransform(pt)<<std::endl;
 

Does everyone have a function with work ?
« Last Edit: April 05, 2014, 09:41:35 pm by Lolilolight »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error (invert a 4*4 matrix)
« Reply #1 on: April 05, 2014, 10:33:25 pm »
Didn't we agree that questions about general C++ or other frameworks should not be posted here?

I'm sorry but I don't see any SFML in this post.
Laurent Gomila - SFML developer

 

anything