Maintain alignment of two objects with modelview

Hi all,

I’m rather new to the world of OpenGL and I’ve been banging my head against the desk for a while now trying to figure this out so hopefully you guys can help me with that. I have two objects, both of which are modelled statically and maintain their own transformation matrices for any transformations done to them.

One object - the enemy in my game - I want the user to always be staring at, so I maintain a point that represents it’s center, and use gluLookAt to accomplish that. Now I want this enemy to be able to rotate up and down around the x axis and left and right around the y axis with respect to the eye point at (0, 0, 0), and when it does the other object - my weapon - will follow it.

Here’s a screenshot so you can get a better idea of what I’m working with

All works fine and dandy for the enemy if I am only rotating in one direction, but as soon as I start to mix, things start to break. For example:

Enemy starts at origin, rotate around y-axis 90 degrees so that enemy is now to our right, try to rotate around x-axis relative to view, and enemy/weapon rotate almost in place, this is because the modelview’s x-axis is now parallel to the enemy. I would have to rotate around the z-axis to get the effect of rotating up or down.

The other challenge, is that the weapon object is always following the cursor (ie x, y of cursor -> x, y, z on world coordiantes, with z held at -10.0). But if I rotate the enemy+weapon roughly 90 degrees around the y-axis, and then try to move my weapon, it won’t move in the x direction anymore (since we’re parallel to the modelview’s x-axis).

Any ideas on how to accomplish all this? Any help would be greatly appreciated!!