Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Player pointing at mouse?  (Read 4080 times)

0 Members and 1 Guest are viewing this topic.

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Player pointing at mouse?
« on: March 22, 2011, 05:26:02 pm »
How do I make my player sprite point at the mouse?  Originally I used the equation atan((mousey-playery)/(mousex-playerx)).  I looked on the internet, and this equation is supposed to return the angle of the player, in degrees.  I was then planning on putting the result in for the player.SetRotation.  However, this doesn't return the angle, just a weird decimal negative number.  What should I do?
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Player pointing at mouse?
« Reply #1 on: March 22, 2011, 08:16:55 pm »
I think the return is in radian, try to multiplicate by 180 and divide by PI

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Player pointing at mouse?
« Reply #2 on: March 23, 2011, 03:56:59 am »
Because the Y-axis goes upwards while the object goes downwards in relation to the screen, and the X-axis is at the top, the window is basically in the fourth quadrant, which has a negative Y and a positive X.  Do I need to convert the Y-coordinates to negative numbers to get the correct angle?
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

kalgon

  • Newbie
  • *
  • Posts: 37
    • View Profile
Player pointing at mouse?
« Reply #3 on: March 23, 2011, 02:48:23 pm »
try atan2 ?

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Player pointing at mouse?
« Reply #4 on: March 23, 2011, 04:23:06 pm »
cmath functions use radians, you will need to invert/negate/thingy (y = 0 - y) the y coordinate and atan2 is more suited for this. Everyone is right! Gold stars!

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Player pointing at mouse?
« Reply #5 on: March 23, 2011, 04:32:47 pm »
I got it working.  Thanks, guys!
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Player pointing at mouse?
« Reply #6 on: March 24, 2011, 07:21:58 am »
Hey Makuto.

Would you mind sharing the solution to this? It might be useful for something I would like to do.

(I'm lousy at maths  :? )
{much better code}

mimipim

  • Newbie
  • *
  • Posts: 49
    • View Profile
Player pointing at mouse?
« Reply #7 on: March 24, 2011, 08:56:21 am »
To get the angle you must know your player position and mouse position:

float angle = atan2(mouseY-plrY,mouseX-plrX)*(180/3.1415f);
spr_myplr.SetRotation(angle);

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Player pointing at mouse?
« Reply #8 on: March 24, 2011, 09:25:01 am »
Many thanks.
{much better code}

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Player pointing at mouse?
« Reply #9 on: March 24, 2011, 01:29:07 pm »
Great code!
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

 

anything