Search code examples
c++openglgame-physicsphysics-engine

How to update transform matrix (NewtonGameDyanmics/OpenGL)?


I'm trying to simulate rotating box using Newton Physics and OpenGL. This is what i have implemented.

float mat44[16] = {
    1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,0,1
};

box.mat = &mat44[0];
box.x_size = 0.50;
box.y_size = 0.50;
box.z_size = 0.50;

nWorld = NewtonCreate(NULL, NULL);

NewtonCollision * collision = NULL;
collision = NewtonCreateBox(nWorld, box.x_size, box.y_size, box.z_size,NULL);
body = NewtonCreateBody(nWorld, collision);
NewtonReleaseCollision (nWorld, collision);
NewtonBodySetMassMatrix(body, 10.0, 2.0, 2.0, 2.0);
NewtonBodySetMatrix (body, box.mat);
float omega[3] = {0.0f, 10.0f, 0.0f};
NewtonBodySetOmega (body, &omega[0]);

inside the rendering loop i'm doing these things.

NewtonUpdate(nWorld, time);
float m[16];
NewtonBodyGetMatrix(body, &m[0]);
box.mat = m;

my problem is how to draw a cube (actually 8 points) using the matrix(box.mat) ? how can i calculate updated vertex points using the matrix ?


Solution

  • You consider the vertex as a column vector and multiply your matrix by it.

    r.x = v.x * m[0] + v.y * m[1] + v.z * m[2]  + m[3]
    r.y = v.x * m[4] + v.y * m[5] + v.z * m[6]  + m[7]
    r.z = v.x * m[8] + v.y * m[9] + v.z * m[10] + m[11]
    

    Where [ v.x, v.y, v.z, 1 ] is the vertex position and r is the output vertex.

    Incidentally, if you're using OpenGL, the "correct" thing to do is feed OpenGL the unchanged vertices and simply use box.mat as the "model matrix" for OpenGL (part of the model-view matrix).