Help rotating spheres around a models axis
I need help rotating a sphere around a models axis I'm totally lost on how to accomplish this.
Let me explain what I am trying to do:
I have a model with its own m_xpos, m_ypos, m_zpos to keep track of where it is and to move the model I use glTranslatef( m_xpos, m_ypos, m_zpos ); and to rotate it I send the angle and what to rotate by to glRotatef(m_xrot, 1.0f. 0.0f, 0.0f); glRotatef(m_yrot, 0.0f. 1.0f, 0.0f); glRotatef(m_zrot, 0.0f. 0.0f, 1.0f);
I have created spheres for collision detection and this is my problem.
If I want to move the sphere with the model all I have to do is use the models m_xpos, m_ypos, m_zpos + sheres m_Xoffset, m_Yoffset, m_Zoffset this moves the sphere everywhere the model goes and keeps it in intended location. ex: a sphere on the models hand will stays on the models hand everywhere I move it. Now here is where I am lost when I rotate the model using glRotatef() how do I get the sphere to rotate according to the models rotations and keep their offsets so the spheres will stay where I placed them on the model no matter how I rotate the model??? The spheres are not models themselves just mathematical representations in 3d space but I donít know how to accomplish this and this is the last bit of info I need so I can have collision detection in my game.
Can anyone please help me on this I would greatly appreciate it.
The simplest solution is to stop using the OpenGL matrix functions (glTranslate, glRotate, etc) and construct the matrices yourself, then upload them with glLoadMatrix(). Then you can just use the same matrix to transform the sphere's centre position.
This is part of the reason why the matrix functions where removed from the OpenGL 3 core profile: for anything beyond "example" programs, they're of no use to you, as you end up needing to construct the matrices yourself for collision, picking, etc.
You can use a library such as GLM, or you can write your own (if writing your own takes longer than learning GLM, you need to improve your programming and/or matrix math skills before going any further). The matrices which glRotate etc generate can be found in the online manual pages. Although, if you're using Euler angles, separate functions for X/Y/Z rotations may be more useful than the arbitrary-axis case.