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

Author Topic: Smooth rotation of a sprite  (Read 1666 times)

0 Members and 1 Guest are viewing this topic.

SamuelGHOST

  • Newbie
  • *
  • Posts: 5
    • View Profile
Smooth rotation of a sprite
« on: November 30, 2018, 11:51:06 am »
Let's say that I have a sprite and with code shown below I obtained a rotation angle:

rotation = (atan2f(dy, dx)) * 180 / 3.14159265;

How do I rotate a sprite smoothly with angle I have in "rotation"? I tried something like this but it didn't work:

for (int i = 0; i < rotation + 180; i++)
   sprite.setRotation(i);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Smooth rotation of a sprite
« Reply #1 on: November 30, 2018, 12:22:28 pm »
You must do this over time, ie. add a small amount of your total rotation every frame, until you reach the final value.
Laurent Gomila - SFML developer

SamuelGHOST

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Smooth rotation of a sprite
« Reply #2 on: November 30, 2018, 04:21:34 pm »
Could you give me an example please?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Smooth rotation of a sprite
« Reply #3 on: November 30, 2018, 06:43:09 pm »
Your code is changing the angle of the sprite in small steps between 0 and rotation+180 and then afterwards (presumably) drawing the sprite. You weren't very specific about what didn't work with your attempt, but my guess is that you aren't actually seeing the sprite move in small steps, but just seeing the final result. This is because you aren't drawing/displaying between steps. You are probably only drawing/displaying after the for loop when it has already completed the full rotation.

An example of what you probably want to do is:
1. Enter your game loop
2. Get the current angle of the sprite
3. Calculate the final angle you want the sprite to have
3. rotate the sprite a small amount towards the final angle (no for loop)
4. Draw the sprite
5. end your game loop

Each loop of your game loop will rotate your sprite a little bit and draw it