can someone tell me how can i make the player look at the mouse like in Alien shooter ? here take a look at this video.
i'm Using SFML and c++
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.
|
|
|
|
--------------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
}
}
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