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}.