**0**

# player look at the mouse, not as easy as it sounds

###
#2
Moderators - Reputation: **8472**

Posted 28 July 2011 - 06:38 AM

###
#3
Members - Reputation: **105**

Posted 28 July 2011 - 07:00 AM

Reverse project the on-screen mouse coordinates into world coordinates to calculate the location on the ground plane of the mouse, subtract the player's location from this mouse position to calculate a vector, normalize that vector to unit length and calculate a rotation matrix around the Up axis that will orient the player to face along that vector. This works if your characters are 3D models. If they are 2D pre-rendered, then instead of calculating a rotation matrix, you would select the appropriate facing-direction animation from the set of facing directions.

thank you

###
#4
Members - Reputation: **207**

Posted 01 August 2011 - 02:20 PM

you get the x and y coordinate from the mouse..

than u divide the screen into 4 areas and let the player be 0/0 as the midpoint

| | | | --------------P-------------- | | | |

you look now in which area the mouse is and than u create a right-angled triangle.

the vector from the player to the mouse is the hypotenuse. from now you can calculate the alpha or beta radiant and use this to rotate your caracter.

I used this technic 2 weeks ago in a game i wrote which will soon be available on my blog (with source code).. http://howtomakeitin....wordpress.com/

heres the source code.. ( i know, most of the variables are useless and it could be written shorter, .. i dont know what i did, it was 5am s.th. :-)

void CObject::rotate(unsigned int pointx, unsigned int pointy, int w, int h) { unsigned int midx = this->x + w/2; unsigned int midy = this->y + h/2; double squarea; double squareb; double root; double angleplus; // create triangle double b; // ankathete double a; // gegenkathete double c; // hypothenuse c = sqrt(a² + b²) // in which area is pointx, pointy; if (pointx > midx && pointy < midy) // viereck oben rechts { b = pointx - midx; a = midy - pointy; this->angle = 270; // stellt object auf 0 grad squarea = a*a; squareb = b*b; root = squarea+squareb; c = sqrt(root); // hypothenuse berechnet angleplus = asin(b/c)* 180.0 / M_PI; this->angle=this->angle+angleplus; // addiert den winkel } else if (pointx > midx && pointy > midy) // viereck unten rechts // gut { b = pointx - midx; a = pointy - midy; this->angle = 0; squarea = a*a; squareb = b*b; root = squarea+squareb; c = sqrt(root); // hypothenuse berechnet angleplus = acos(b/c)* 180.0 / M_PI; this->angle=this->angle+angleplus; // addiert den winkel } else if (pointx < midx && pointy < midy) // viereck oben links // gut { b = midx - pointx; a = midy - pointy; this->angle = 180; squarea = a*a; squareb = b*b; root = squarea+squareb; c = sqrt(root); // hypothenuse berechnet angleplus = acos(b/c)* 180.0 / M_PI; this->angle=this->angle+angleplus; // addiert den winkel } else if (pointx < midx && pointy > midy) // viereck unten links { b = midx - pointx; a = pointy - midy; this->angle = 90; squarea = a*a; squareb = b*b; root = squarea+squareb; c = sqrt(root); // hypothenuse berechnet angleplus = asin(b/c)* 180.0 / M_PI; this->angle=this->angle+angleplus; // addiert den winkel } }

I open sourced my C++/iOS OpenGL 2D RPG engine :-)

See my blog: (Tutorials and GameDev)