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

Author Topic: Weird sprite rotation  (Read 1894 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Weird sprite rotation
« on: November 03, 2012, 03:59:52 pm »
So I have a simple program where there is an arrow and it moves towards the mouse. I calculate the angle between the current 'player' position and where it should go.

Code: [Select]
heading = std::atan2(yTarget - yPos, xTarget - xPos) * RAD2DEGREE;

The sprite's rotation is then set to be equal to the heading later in the same function:

Code: [Select]
playerSprite.SetRotation(heading);

However, it faces opposite up and down movement of the mouse. If I move the mouse up, it rotates to face down. If I move the mouse down, it rotates to face up. It works correctly left to right; if you are directly left or right of the arrow, then the arrow will face the mouse as it should.

I logged the numbers and it appears that when directly above the player position, atan2 is return -90 degrees, which then in the rotation function of the sprite gets converted to 270.

However, also odd is that if I multiply heading by -1 in the SetRotation function argument, it works completely fine even though the log shows completely different numbers for heading and orientation.  For example:

Heading = 44.113
Sprite Rotation = 315.887

But the player sprite, an arrow, is pointing directly at the mouse button the entire time.

I simply don't understand what is going on and would love someone to explain what I'm missing.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Weird sprite rotation
« Reply #1 on: November 03, 2012, 04:35:04 pm »
OK so the different numbers are really the same -45 degrees = 315 degrees. I don't understand why atan2 is returning -90 degrees when it should be 90 though. It doesn't make any sense. I've used it before without problem.


victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Weird sprite rotation
« Reply #2 on: November 03, 2012, 05:26:35 pm »
Hello,

I'm not sure but the trigonometric functions of the stdlib use a y-axis which grows when it goes up and SFML a y-axis which grows when it goes down.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Weird sprite rotation
« Reply #3 on: November 03, 2012, 05:32:38 pm »
I don't think that is the issue. It moves just fine, it just faces the wrong way up or down.

Here is the entire function that does the math:

Code: [Select]
void Player::Update(float xTarget, float yTarget)
{
heading = std::atan2(yTarget - yPos, xTarget - xPos) * RAD2DEGREE;

tofile << "Heading = " << heading << std::endl;

float xDirect = std::cos(heading * DEGREE2RAD);
float yDirect = std::sin(heading * DEGREE2RAD);

tofile << "Cos(heading) = " << xDirect << std::endl;
tofile << "Sin(heading) = " << yDirect << std::endl;

        xPos += xVel * xDirect;
yPos += yVel * yDirect;

if(xPos < 0 + width)
{
xPos = 0 + width/2;
}

if(xPos + width > screenWidth)
{
xPos = screenWidth - width;
}

if(yPos < 0 + height/2)
{
yPos = 0 + height/2;
}

if(yPos + height > screenHeight)
{
yPos = screenHeight - height;
}

playerSprite.SetRotation(heading);

tofile << "Sprite Rotation = " << playerSprite.GetRotation() << std::endl;

playerSprite.SetPosition(xPos, yPos);

tofile << "X = " << xPos << std::endl;
tofile << "Y = " << yPos << std::endl << std::endl;
}