1
votes

I have an opengl c++ project I'm working on, where I draw a player and a ball in his right hand. At first I want to make the ball follow the player's hand movement by setting ball position. After some time I need to make the ball travel using kinematics eqations. I get the player right hand transform matrix using (I do this the moment I draw the player):

glGetFloatv(GL_MODELVIEW_MATRIX, ball->transformMatrix);

and then the moment I draw the ball I try to do:

glLoadMatrixf(ball->transformMatrix);
glTranslated(0, 0, 0);
glCallList(lists[BALL]);

this works perfect and ball drawn at place.

The problem is when I need to move the ball

x += velocityX*dt;
y += velocityY*dt + gravity*dt*dt;
z += velocityZ*dt;

velocityY += gravity*dt;

the movement is relative not to player transform but the global. I tried many ways to solve this but none works.

So how is it possible for me to calculate properly the x, y, z of the ball so I can translate the ball after it left the player hand?

Edit:

I've managed to recieve the ball coordinates by multiplying ball->transformMatrix with the vector {0, 0, 0, 1} the result storing in world vector, after that calling (the moment I draw the ball):

glGetFloatv(GL_MODELVIEW_MATRIX, currentTransformMatrix);

then inversing currentTransformMatrix and multiplying inverse with world vector. The result is the ball proper coordinates {x, y, z}.

1
Please don't abuse OpenGL as a matrix math library. It's awkward and inefficient. Better use a real matrix math library and use glLoadMatrix and glMultMatrix to apply the matrices to OpenGL. BTW: OpenGL-3.1 completely removed all matrix functions!datenwolf

1 Answers

2
votes

You need to transform back to the player's hand position; invert the transformation from hand to world. Since OpenGL is not a math library, you need to either implement the matrix math yourself of use some 3rd party linear math library.

EDIT due to comment:

What you transform is the velocity and acceleration into the ball's local coordinate system. Both velocity and gravity are just "directive" vectors, so the transformation is independent of the position of the ball in the world (well the gravity may change, but that's no issue here). So you need the inverse transform from world to ball space, that is independent from position. This is the very same problem like transforming normals (jsut the other way round) => so you need to take the transposed inverse of the inverted matrix (Explained in detail here http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/ ). However you already have the inverse of that inverse; it's the ball->world matrix itself. So you just have to transpose the modelview matrix, multiply velocity and gravity vectors with that and apply them in the ball's local space.