Hi everybody
In my game, the user can look around using the mouse. The mouse position is translated into viewangle_vertical and viewangle_horizontal. I use those variables to compute the correct values for GLULookAt. That works great so far.
(Just to clarify: viewangle_vertical is always between -PI/2 and PI/2)
Now I’d like to have a model at the player location that looks in the same direction as the player does. Similar to a Gun in a Shooter (the gun moves with the player, and always points in the player’s direction).
I already have my Object Loader, so rendering the thing isn’t a problem. Here’s my rendering code: (simplieifed)
glPushMatrix();
glLoadIdentity();
gluLookAt(pos.x, pos.y, pos.z, lookat.x, lookat.y, lookat.z, 0, 1, 0);
glTranslated(x, y, z);
glRotated(rot_x, 1, 0, 0);
glRotated(rot_y, 0, 1, 0);
glRotated(rot_z, 0, 0, 1);
//Actual call to glDrawArrays or similar
glPopMatrix();
I already figured out that
x = X part of Player’s ViewVector
y = Y part of Player’s ViewVector
z = Z part of Player’s ViewVector
rot_y = viewangle_horizontal*RAD2ANG (RAD2ANG = 180.0 / Math.PI)
So as long as viewangle_vertical equals 0, this works perfectly. (My gun stays clearly visible in front of the player, and points away from the player)
If I look up or down, however, the Gun is at the correct position, but isn’t rotated correctly (well, rot_x and rot_z = 0, so this isn’t surprising)
I tried setting
rot_x = Math.abs(Math.cos(viewangle_horizontal))*180;
rot_z = Math.abs(Math.sin(viewangle_horizontal))*180;
But this turns out VERY bad, the gun just flings aroundwith no clear pattern (I have no idea ;( )
Can you guys give me some pointers to a tutorial that deals with this kind of math?
Thanks