Followers 0

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

## 4 posts in this topic

hay guys

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++
0

##### Share on other sites
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.
0

##### Share on other sites
[quote name='JTippetts' timestamp='1311856691' post='4841549']
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.
[/quote]

thank you
0

##### Share on other sites
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

[code]
|
|
|
|
--------------P--------------
|
|
|
|

[/code]

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).. [url="http://howtomakeitinamsterdam.wordpress.com/"]http://howtomakeitin....wordpress.com/[/url]

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. :-)

[code]

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;

}
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;

}
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;

}
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;

}
}
[/code]
1

##### Share on other sites
[quote name='FlyingDutchman' timestamp='1312230052' post='4843251']
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[/quote]

Alternatively, you just use atan2.

1

## Create an account

Register a new account