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

Author Topic: sf::View breaks Object Rotation  (Read 2560 times)

0 Members and 1 Guest are viewing this topic.

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
sf::View breaks Object Rotation
« on: September 01, 2010, 09:19:41 pm »
I have an Entity that points towards my mouse using this function:
Code: [Select]

Object.SetRotation( Math::Convert::RadToDeg( std::atan2( Pos.x - x, Pos.y - y ) ) )


It works fine when I don't have any views applied. However, when I apply a view through my camera class the rotation breaks and is limited to a ninety agree angle.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::View breaks Object Rotation
« Reply #1 on: September 01, 2010, 10:49:06 pm »
Have you converted the mouse position to the view's coordinates using RenderWindow::ConvertCoords?
Laurent Gomila - SFML developer

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
sf::View breaks Object Rotation
« Reply #2 on: September 01, 2010, 11:55:08 pm »
I just added that and it's still got the same issue, maybe it has something to do with the order of my functions?

Loop:

1. Update To the New View
2. Assign Mouse X and Y to new variables.
3. Convert Mouse X and Y to the New View via sf::RenderWindow
4. Event Managing
5. Rotate Entity To Mouse X and Y
6. Entity Think Code ( Just modifies Position by Velocity )
7. Clear Window, Render Entity, Display Rendered Items
8. Goto 1

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::View breaks Object Rotation
« Reply #3 on: September 02, 2010, 08:40:35 am »
It looks OK, I think we'll need to see the actual code. Maybe you can show us a complete and minimal example that reproduces the problem?
Laurent Gomila - SFML developer

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
sf::View breaks Object Rotation
« Reply #4 on: September 02, 2010, 06:19:10 pm »
Quote from: "Laurent"
It looks OK, I think we'll need to see the actual code. Maybe you can show us a complete and minimal example that reproduces the problem?


Code: [Select]

int main()
{
sf::RenderWindow Game( sf::VideoMode( 800, 600, 32U ), "Game", sf::Style::Close );
Game.SetFramerateLimit( 60 );
sf::Event Events;

sf::View View( sf::FloatRect( 0, 0, 800, 600 ) );

sf::Image Img;
Img.LoadFromFile( "textures/picture.png" );

sf::Sprite Entity;
Entity.SetImage( Img );
Entity.SetPosition( 100, 100 );
Entity.SetOrigin( Entity.GetSize().x/2, Entity.GetSize().y/2 );

while( Game.IsOpened() )
{
Game.SetView( View );
View.SetCenter( Entity.GetPosition() );

while( Game.GetEvent( Events ) )
{
if( Events.Type == Events.Closed )
Game.Close();
}

Entity.SetRotation( ( 180/3.14159f ) * (std::atan2( Entity.GetPosition().x - float( Game.GetInput().GetMouseX() ), Entity.GetPosition().y - float( Game.GetInput().GetMouseY() ) ) ) );

Game.Clear();
Game.Draw( Entity );
Game.Display();
}
}


After running that I also realize it has something to do with sf::View::SetCenter(), as it didn't start breaking until I put that in.

(( I'm also aware it's standard for std::atan to hold pos.y - mouse.y, pos.x - mouse.x instead of how I'm doing it, but I've tested it with the proper atan2 parameters and it didn't make any difference other than which direction the sprite faces by default ))

NorthernGate

  • Newbie
  • *
  • Posts: 10
    • View Profile
sf::View breaks Object Rotation
« Reply #5 on: September 02, 2010, 07:05:48 pm »
Figured it out, I didn't realize sf::RenderWindow::ConvertCoords() returned a value, I thought it modified the values you gave it. Now it works, cool.

 

anything