Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 28 July 2011 - 05:16 AM
Posted 28 July 2011 - 06:38 AM
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.
Posted 01 August 2011 - 02:20 PM
| | | | --------------P-------------- | | | |
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)
Posted 11 August 2011 - 02:38 PM
you can also do the following.
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
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.