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

Author Topic: make player look at the mouse cursor  (Read 2545 times)

0 Members and 2 Guests are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
make player look at the mouse cursor
« on: July 11, 2011, 10:10:24 pm »
hay guys

i'm trying to make my player to Rotate and look at the mouse when i move the mouse.

here is the code

Code: [Select]


//Screen = 800X600

//while loop
{
//while event loop
//{
//}
int PlayerX=SpritePlayer.GetPosition().x;
int PlayerY=SpritePlayer.GetPosition().y;

int MouseX=Game.GetInput().GetMouseX();
int MouseY=Game.GetInput().GetMouseY();

if ((MouseX<=400)&&(MouseY<=300))
SpritePlayer.Rotate(0 * ElapsedTime);

if ((MouseX>400)&&(MouseY<300))
SpritePlayer.Rotate(90* ElapsedTime);

if ((MouseX>400)&&(MouseY>300))
SpritePlayer.Rotate(180 * ElapsedTime);

if ((MouseX<400)&&(MouseY>300))
SpritePlayer.Rotate(270 * ElapsedTime);
}


this is what i though i would do

x=mousex
y=mousey


Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
make player look at the mouse cursor
« Reply #1 on: July 11, 2011, 10:34:15 pm »
I guess something is not working. Could you elaborate ?  :wink:
SFML / OS X developer

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
make player look at the mouse cursor
« Reply #2 on: July 11, 2011, 10:38:41 pm »
Quote from: "Hiura"
I guess something is not working. Could you elaborate ?  :wink:


well what i'm trying to do is if i move the mouse cursor the player will keep rotating till he look at the mouse and then stop, you know like Alien Shooter

here is an example

look when the mouse moves the player look at it

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
make player look at the mouse cursor
« Reply #3 on: July 17, 2011, 03:46:37 pm »
guys any help please ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
make player look at the mouse cursor
« Reply #4 on: July 17, 2011, 04:22:44 pm »
Why don't you use std::atan2() to compute the correct angle?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
make player look at the mouse cursor
« Reply #5 on: July 17, 2011, 04:24:32 pm »
Quote from: "Nexus"
Why don't you use std::atan2() to compute the correct angle?


thank i'll try it and see if i can get it to work