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

Author Topic: Problem with Sprite.SetRotation()  (Read 6723 times)

0 Members and 1 Guest are viewing this topic.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with Sprite.SetRotation()
« on: January 24, 2011, 03:08:17 am »
I'm a bit puzzled by the behavior of a piece of my code:

The following code creates two sprites. One should constantly turn to face the mouse cursor, the other one moves at the first sprite.

For this I call AngleBetweenVectors. The second sprite moves into the right direction, which shows that AngleBetweenVectors returns the correct result.
But the first sprite (which is supposed to face the cursor) turns into the wrong direction. (It doesn't face into the wrong direction, but turns wrongly.)

As far as I can see, however, my code should be correct this far.
But since I haven't found anyone else complaining about that there must be something wrong ~


You can copy pasta that directly into MSVC and it'll work, if anyone wants to try.
Code: [Select]

#include <SFML/Graphics.hpp>

//  (180 / PI = 57.3065)
float AngleBetweenVectors(sf::Vector2f a, sf::Vector2f b){
   return 57.3065f * atan2(b.y - a.y, b.x - a.x);
}

void main () {
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "");
App.SetFramerateLimit(50);

sf::Image img(50, 10, sf::Color(150,150,150,255));


sf::Sprite a(img);
sf::Sprite b(img);

a.SetPosition(200,200);
b.SetPosition(400,500);

a.SetCenter(25,5);
b.SetCenter(25,5);

float travel=AngleBetweenVectors(b.GetPosition(), a.GetPosition());

while(true){
sf::Event event;
App.GetEvent( event);
sf::Vector2f mousePos(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
float f=AngleBetweenVectors(a.GetPosition(), mousePos);


// x = speed * cosine( angle * PI/180 )
// y = speed * sine( angle * PI/180 )
float nx= .25 * cos(travel * 0.01745);
float ny= .25 * sin(travel * 0.01745);

b.Move(nx,ny); //Make b move towards a

a.SetRotation(f); //Make a face the cursor, doesn't work.
//a.SetRotation(360-f); //Make a face the cursor, does work.

App.Clear();
App.Draw(a);
App.Draw(b);
App.Display();
}
}

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Problem with Sprite.SetRotation()
« Reply #1 on: January 24, 2011, 01:55:57 pm »
To get all mouse movements you have to call "while (App.GetEvent(event));" instead of "App.GetEvent(event);".
To make the rotation work try "a.SetRotation(-f);".

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with Sprite.SetRotation()
« Reply #2 on: January 24, 2011, 03:28:18 pm »
Quote from: "Lupinius"
To get all mouse movements you have to call "while (App.GetEvent(event));" instead of "App.GetEvent(event);".
To make the rotation work try "a.SetRotation(-f);".


Yes, but this is only some test code. I don't really care about catching all events. Catching mouse is enough so I don't need the while(..).

About a.SetRotation(-f):
Yes, that works - but the question is why. I haven't seen seen any other complaints about this, so I don't think it's like that in general. Shouldn't atan() and SetRotation() be using the same degree measurement?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Problem with Sprite.SetRotation()
« Reply #3 on: January 24, 2011, 04:08:08 pm »
If you only get events once then the input object will only be updated with that event. so if the event you get is not a mouse event then that won't be updated. So it would hurt to while-loop the get event call even though you won't use it.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with Sprite.SetRotation()
« Reply #4 on: January 24, 2011, 04:54:31 pm »
As I said the purpose of this code is to test the rotation and nothing else.

But let's assume I did use a while-loop to catch all events so we get that out of the focus. :)

The question is still why SetRotation() behaves so strangely, and whether this is an intended behavior or whether I do something wrong.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with Sprite.SetRotation()
« Reply #5 on: January 24, 2011, 05:13:19 pm »
You have to invert the angle (-f) because the Y axis is inverted in a SFML window: 0 is at top -- and trigonometric functions assume 0 at bottom.
Laurent Gomila - SFML developer

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with Sprite.SetRotation()
« Reply #6 on: January 24, 2011, 11:40:20 pm »
That makes sense. Thanks!

The Floating Brain

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • http://www.gcggames.webs.com
Problem with Sprite.SetRotation()
« Reply #7 on: April 17, 2011, 02:01:50 am »
May I use this code? I have changed it around a bit and used APA formatting to give credit you(used your username as a auther)/cite this page as a source.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with Sprite.SetRotation()
« Reply #8 on: April 17, 2011, 10:37:46 pm »
Quote from: "The Floating Brain"
May I use this code? I have changed it around a bit and used APA formatting to give credit you(used your username as a auther)/cite this page as a source.


No problem. Use it as you like.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Problem with Sprite.SetRotation()
« Reply #9 on: April 17, 2011, 10:57:01 pm »
By the way, the sign of angles in SFML has recently been changed, see Issue #3.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Floating Brain

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • http://www.gcggames.webs.com
Problem with Sprite.SetRotation()
« Reply #10 on: April 22, 2011, 09:03:10 pm »
Quote from: "s3rius"
Quote from: "The Floating Brain"
May I use this code? I have changed it around a bit and used APA formatting to give credit you(used your username as a auther)/cite this page as a source.


No problem. Use it as you like.

Thank you :-)

 

anything