SFML community forums
Help => General => Topic started by: SamuelGHOST 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);
-
You must do this over time, ie. add a small amount of your total rotation every frame, until you reach the final value.
-
Could you give me an example please?
-
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