Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
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.

  • You cannot reply to this topic
4 replies to this topic

#1 FantasyVI   Members   -  Reputation: 105

Like
0Likes
Like

Posted 28 July 2011 - 05:16 AM

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

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8344

Like
0Likes
Like

Posted 28 July 2011 - 06:38 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.

#3 FantasyVI   Members   -  Reputation: 105

Like
0Likes
Like

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 FlyingDutchman   Members   -  Reputation: 207

Like
1Likes
Like

Posted 01 August 2011 - 02:20 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


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

http://howtomakeitin....wordpress.com/


#5 Topher Cox   Members   -  Reputation: 103

Like
1Likes
Like

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


Alternatively, you just use atan2.







Old topic!
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.



PARTNERS