This topic is now archived and is closed to further replies.


mouse and looking at things

Recommended Posts

Is there a simple (or not so) way to make an object rotate to where the mouse is pointing? An example would be you have a head in the middle and it looks at the mouse pointer as you move it around.

Share this post

Link to post
Share on other sites
I think I have an idea for you. Set up a message check in your WindowProc function for WM_MOUSEMOVE like this:

/*xhead, yhead, zhead are the variables for the head''s base coordinates, which should pre-exist. Ditto for x_ang and y_ang, which are the head rotation angle variables.*/

#include <math.h> //You will probably have the other needed header files already included.

POINT mseMove;
double modMtx[16], projMtx[16];
int vwarr[4];
glLoadIdentity(); //To give the code a cleaner look.
glGetDoublev(GL_MODELVIEW_MATRIX, &modMtx);
glGetDoublev(GL_PROJECTION_MATRIX, &projMtx);
glGetIntegerv(GL_VIEWPORT, &vwarr);
double x,y,z;
gluUnProject(mseMove.x, mseMove.y, 0, modMtx, projMtx, vwarr, &x, &y, &z); /*I chose 0 since it''s probably the right depth value for the job. I hope it is!*/
//Now for some trigonometry work to find the y axis rotation variable.
double tany, tanx;
tany = (x-xhead)/(-zhead);
y_ang = atan(tany); //This will give the y-angle rotation you need.
tanx = (y-yhead)/(-zhead);
x_ang = atan(tanx); //This will give the x-angle rotation you need.
return 0;

This code may need tweaking to make it work just right, but I think it should get you close to where you want.

Share this post

Link to post
Share on other sites