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

Author Topic: Rotating sprite towards mouse.  (Read 2616 times)

0 Members and 1 Guest are viewing this topic.

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Rotating sprite towards mouse.
« on: September 07, 2009, 06:38:00 pm »
I was using the following code to do it (found the code here, I take no credit for it), but now, after I successfully got the camera to follow the sprite, the code only works when I start the program, if I move the sprite, it's like the center is out of position, re-setting the center didn't work.

Code: [Select]

#include <SFML/Graphics.hpp>
#define PI 3.1415926535

int WINAPI WinMain(      
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{

sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");

sf::Image PlayerImage;
sf::Image Background;
Background.LoadFromFile("background.png");
PlayerImage.LoadFromFile("player.png");
sf::Sprite Player(PlayerImage);
sf::Sprite BackgroundS(Background);
sf::Sprite* follow = &Player;
Player.SetPosition(400,300);

sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));  
sf::Vector2f PPos = Player.GetPosition();


while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();
}

float ElapsedTime = Window.GetFrameTime();


if (Window.GetInput().IsKeyDown(sf::Key::W))  Player.Move(0, -425 * ElapsedTime);  
        if (Window.GetInput().IsKeyDown(sf::Key::S))  Player.Move(0,  425 * ElapsedTime);  
if (Window.GetInput().IsKeyDown(sf::Key::A))  Player.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D))  Player.Move(425 * ElapsedTime, 0);  

Player.SetCenter(Player.GetSize() / 2.f);


if (Window.GetInput().GetMouseX() <= Player.GetPosition().x) {
Player.SetRotation(((-1* 360 / PI * (atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
} else {
Player.SetRotation(((-1* 360 / PI *(atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
}


Window.Clear();
Camera.SetCenter(follow->GetPosition());
Window.SetView(Camera);
Window.Draw(BackgroundS);
Window.Draw(Player);

Window.Display();
}
}

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Rotating sprite towards mouse.
« Reply #1 on: September 08, 2009, 03:12:03 pm »
Anyone?

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Rotating sprite towards mouse.
« Reply #2 on: September 08, 2009, 04:35:34 pm »
The mouse position is relative to the window and not the 2D plane (which is the space that the sprites are in).

For instance, if you positioned the mouse in the center of the screen and move the player around, the mouse position will remain the same but the sprite position will be different.

This makes your calculation incorrect.

What you need to do is convert from the window space to the global space that the sprites use. You need to calculate the mouse position in relation to the position of the view.

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Rotating sprite towards mouse.
« Reply #3 on: September 08, 2009, 07:07:42 pm »
Thanks a lot, it's working now.

 

anything