Hi I’m not really new to opengl but I’m having a serious issue orienting an object using the built in opengl matrix. It’s a gun that needs to be locked to the hand of an enemy. The enemy keeps track of a grip position which is where his hand is at that particular frame. The gun also has a position set in it for grip position in the handle and that tells how the gun orients relative to the enemy hand grip position. Also there’s rotation involved. The enemy has a set rotation based on current direction he’s facing obviously. And the gun must also be rotated to face this same direction. So I’m using multiple calls to glRotate & glTranslate to rotate the gun around its grip point and then position it relative to the enemy’s current position. Everything pans out fine until I add the last glTranslate that accounts for the enemy’s world position. My algorithm is as follows
Vectors3f PlayerPos; Enemy’s world position
Vectors3f PlayerDir; Enemy’s facing direction(Only Y is used)
Vectors3f
Vectors3f PlayerGrip; Position of grip point in enemy hand when is at PlayerDir=0
Vectors3f GunGrip; Position in gun that should lock to PlayerGrip(in the handle)
-
Translate gun so it is centered around origin relative to it’s grip point and rotate to same direction as enemy
glRotatef(PlayerDir.Y,0.0,1.0,0.0);
glTranslatef(-GunGrip.X,-GunGrip.Y,-GunGrip.Z); -
Translate out to the position player’s hand would be at PlayerDir.Y=0 and rotate around the origin at 0.0,PlayerDir.Y,0.0
glRotatef(PlayerDir.Y,0.0,1.0,0.0);
glTranslatef(PlayerGrip.X,PlayerGrip.Y,PlayerGrip.Z); -
Translate out to final player world position
glTranslatef(PlayerPos.X,PlayerPos.Y,PlayerPos.Z);
Executing only step 1 works fine. The gun is moved to the origin and it rotates perfect and spins on its grip point. Things start messing up around step 2. I have tried rotating first then translating, and vice versa, but it doesn’t work. Only doing steps 1+2 should have the gun suspended in the position of the player’s hand at PlayerDir.Y=0 and spinning around in place around it’s handle but it spins relative to the origin instead. Any input or tips for handling multiple glRotate/glTranslate calls on the same object are much appreciated