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

Author Topic: [SOLVED] Sprite rotates towards mouse but can only move on X axis  (Read 1890 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Hey all thanks for taking the time to look at my problem, so I have a sprite that rotates perfectly towards the direction of the mouse but when i attempt to move it in that direction it only moves in the correct direction on the X axis. On the y axis if the mouse is above the sprite, it will move down or below it will make it move up...

so basically my question is - Why is my Y axis reversed!?

float Player::getRotation(sf::RenderWindow &win)
{
   sf::Vector2f curPos;  //current position(not cursor position, sorry for ambiguous var names!)
   curPos.x = arrowSprite.getGlobalBounds().left;
   curPos.y = arrowSprite.getGlobalBounds().top;
   sf::Vector2i position = sf::Mouse::getPosition(win);

    const float PI = 3.14159;

    float dx = position.x - curPos.x;
    float dy = position.y - curPos.y;

    float rotation = (atan2(dy, dx)) * 180 / PI;

   return rotation;
}
void Player::LookMouse(sf::RenderWindow &win)
{
    float rotation = getRotation(win);
    arrowSprite.setRotation(rotation);
}
void Player::Shoot(float deltaT,sf::RenderWindow &win)
{
   arrowSprite.move( cos(getRotation(win)*3.14159265/180)*4, sin(getRotation(win)*3.14159265/180)*-4);
}

//lookMouse is called whenever the mouse moves, Shoot is called whenever spacebar is hit, getRotation is called as needed
« Last Edit: August 01, 2013, 08:54:52 am by HailBee »

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Always the way....spend HOURS troubleshooting, post for help then instantly work out the solution...

For those playing the home game:

arrowSprite.move( cos(getRotation(win))*(300.0f * deltaT), sin(getRotation(win))*(300.0f * deltaT));

Math help thanks to: http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction
« Last Edit: August 01, 2013, 08:57:28 am by HailBee »

 

anything