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

Author Topic: Rotation  (Read 4568 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« on: October 11, 2008, 04:04:40 pm »
hello, i want my sprite to rotate when the mouse is moved, so that my hero always looks in direction of MouseCursor

i tried following:
Code: [Select]

if( Event.Type == sf::Event::MouseMoved)
            {
                Vector2f Sprite_Mouse = GetMousePos() - v[0]->GetPosition();
                float angle = atan(Sprite_Mouse.x / Sprite_Mouse.y);
                v[0]->Rotate(angle);
            }


v[0] is a pointer to a sprite;
somehow it doesnt work exactly like i want it to , in the beginning it looks almost fine but when there is an angle of more tha 90° it goes crazy

how can i do what im trying to?

edit: in the beginning sometimes my sprite disappears

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotation
« Reply #1 on: October 11, 2008, 07:07:08 pm »
atan function has 2 possible angles for the same value, use atan2 instead.

Code: [Select]
float angle = atan2(Sprite_Mouse.y, Sprite_Mouse.x)
(swap x and y if it's wrong)
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« Reply #2 on: October 11, 2008, 07:12:18 pm »
same problem with both variants,

uis there any mathematical mistake? maybe GetPosition returns different points when its rotated?

is there any possibility to attach a video?

or can it be because my y-axe goes down in positive sense?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotation
« Reply #3 on: October 11, 2008, 08:24:44 pm »
You should use SetRotation rather than Rotate. Rotate adds to the previous angle, whereas SetRotation completely overwrite the current angle.
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« Reply #4 on: October 11, 2008, 11:15:53 pm »
hmm i tried something i let my program write sprites x-pos, y-pos
then x and y-value of the vector mouse-sprite and then the calculated angle , an excerpt:

Sprite x:    0      Sprite y:          0    SM x:    -173.75    SM y:   -36.6667   angle:   -2.93361
Sprite x:          0Sprite y:          0    SM x:     -137.5    SM y:         -5   angle:   -3.10525
Sprite x:          0Sprite y:          0    SM x:     -96.25    SM y:    31.6667   angle:    2.82374
Sprite x:          0Sprite y:          0    SM x:     -48.75    SM y:    73.3333   angle:    2.1574

when i let this do my calculator, for example the first one

173.75 / 36.66 = 4,73 ->  atan = 78° even in rad its 1.36

so i cannot explain whats happening :-(

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« Reply #5 on: October 11, 2008, 11:49:18 pm »
o i managed it, atan was really in rad and i checked for division / 0 now , it works fine now

o no : now when mouse is in 4th quadrant the sprite points to 2nd
and same with 3rd when its there then the oicture shows 1st

but when mouse is in 1st or 2nd everythings ok

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« Reply #6 on: October 11, 2008, 11:59:18 pm »
here my .txt:

Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    1.66667   angle:    86.8227
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:          5   angle:    80.5401
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:         10   angle:    71.5672
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    11.6667   angle:    68.7515
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    13.3333   angle:    66.0395
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:         15   angle:    63.4368
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    16.6667   angle:    60.9472
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    18.3333   angle:    58.5722
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:         20   angle:    56.3116
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    21.6667   angle:    54.1639
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    23.3333   angle:    52.1266
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:         25   angle:    50.1959
Sprite x:    112.487Sprite y:    171.853    SM x:         30    SM y:    26.6667   angle:    48.3679
Sprite x:    112.487Sprite y:    171.853    SM x:      28.75    SM y:    26.6667   angle:    47.1544
Sprite x:    112.487Sprite y:    171.853    SM x:      28.75    SM y:    28.3333   angle:    45.4196
Sprite x:    112.487Sprite y:    171.853    SM x:      26.25    SM y:         30   angle:    41.1871
Sprite x:    112.487Sprite y:    171.853    SM x:      26.25    SM y:    31.6667   angle:    39.6581
Sprite x:    112.487Sprite y:    171.853    SM x:         25    SM y:    33.3333   angle:     36.871
Sprite x:    112.487Sprite y:    171.853    SM x:       22.5    SM y:    36.6667   angle:    31.5357
Sprite x:    112.487Sprite y:    171.853    SM x:      18.75    SM y:         40   angle:    25.1156
Sprite x:    112.487Sprite y:    171.853    SM x:         15    SM y:    41.6667   angle:    19.7995
Sprite x:    112.487Sprite y:    171.853    SM x:       12.5    SM y:    43.3333   angle:    16.0913
Sprite x:    112.487Sprite y:    171.853    SM x:    6.25001    SM y:    46.6667   angle:    7.62838
Sprite x:    112.487Sprite y:    171.853    SM x:    1.25001    SM y:    46.6667   angle:     1.5344
Sprite x:    112.487Sprite y:    171.853    SM x:   -7.49999    SM y:    46.6667   angle:   -9.13044
Sprite x:    112.487Sprite y:    171.853    SM x:      -12.5    SM y:    46.6667   angle:   -14.9955
Sprite x:    112.487Sprite y:    171.853    SM x:     -16.25    SM y:    46.6667   angle:   -19.1994
Sprite x:    112.487Sprite y:    171.853    SM x:     -21.25    SM y:    46.6667   angle:   -24.4832
Sprite x:    112.487Sprite y:    171.853    SM x:     -23.75    SM y:    46.6667   angle:   -26.9736
Sprite x:    112.487Sprite y:    171.853    SM x:     -26.25    SM y:    46.6667   angle:   -29.3586
Sprite x:    112.487Sprite y:    171.853    SM x:      -27.5    SM y:    46.6667   angle:   -30.5111
Sprite x:    112.487Sprite y:    171.853    SM x:        -30    SM y:    43.3333   angle:   -34.6962
Sprite x:    112.487Sprite y:    171.853    SM x:      -32.5    SM y:    43.3333   angle:    -36.871
Sprite x:    112.487Sprite y:    171.853    SM x:     -36.25    SM y:    38.3333   angle:   -43.4013
Sprite x:    112.487Sprite y:    171.853    SM x:     -41.25    SM y:    36.6667   angle:   -48.3679
Sprite x:    112.487Sprite y:    171.853    SM x:     -43.75    SM y:    33.3333   angle:   -52.6976
Sprite x:    112.487Sprite y:    171.853    SM x:      -47.5    SM y:    28.3333   angle:    -59.186
Sprite x:    112.487Sprite y:    171.853    SM x:     -48.75    SM y:         25   angle:   -62.8522
Sprite x:    112.487Sprite y:    171.853    SM x:     -51.25    SM y:         20   angle:   -68.6841
Sprite x:    112.487Sprite y:    171.853    SM x:      -52.5    SM y:    18.3333   angle:   -70.7526
Sprite x:    112.487Sprite y:    171.853    SM x:     -53.75    SM y:         15   angle:   -74.4094
Sprite x:    112.487Sprite y:    171.853    SM x:     -56.25    SM y:    8.33333   angle:   -81.5754
Sprite x:    112.487Sprite y:    171.853    SM x:      -57.5    SM y:          5   angle:   -85.0328

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Rotation
« Reply #7 on: October 12, 2008, 12:34:12 am »
i did some more experiments and have finally foud something working :

Code: [Select]

Vector2f Sprite_Mouse =  GetMousePos()  - v[1]->GetPosition()  ;
                if(Sprite_Mouse.y != 0)
                    angle = atan2(Sprite_Mouse.x, Sprite_Mouse.y);
                angle =  angle * 180 / 3.1415;
                angle = angle + 180 %360;
                v[1]->SetRotation(angle);


but WHY does this work?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Rotation
« Reply #8 on: October 12, 2008, 09:05:34 am »
Here is why it works :
Code: [Select]
if (Sprite_Mouse.y != 0)
    angle = atan2(Sprite_Mouse.x, Sprite_Mouse.y); // atan2 is the right function (atan is bad in this context)
angle =  angle * 180 / 3.1415; // convert to degrees for SFML
angle = angle + 180 % 360; // add 180 to map to SFML's drawing coordinates
Laurent Gomila - SFML developer

 

anything