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

Author Topic: Rotation of sprite with small steps  (Read 1969 times)

0 Members and 1 Guest are viewing this topic.

UtkuDemir

  • Guest
Rotation of sprite with small steps
« on: March 30, 2020, 10:59:26 pm »
Hello everyone. I want to rotate a sprite with small step sizes.

I do not want to write directly as:

sprite.setRotation(90);

I want to do it with 1 degree increment in every step. Therefore, the sprite should not be seen as just 90 degree rotation. It should be seen as continuous change in rotation.

How do I implement that?

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotation of sprite with small steps
« Reply #1 on: March 31, 2020, 06:49:03 pm »
You can either:

(setRotation())
Step the amount yourself and set the rotation directly.
For example, loop from zero to 90 and set rotation to that amount.

or:

(setRotation() & rotate())
set the starting rotation and just repeatedly rotate by a small amount each time.
For example, set starting rotation to zero and rotate by 0.5 each time.
Note that if you want this method to stop at 90, you will need to stop rotating once it passes 90 and also set the rotation to 90 (to make sure it hasn't passed it).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

UtkuDemir

  • Guest
Re: Rotation of sprite with small steps
« Reply #2 on: April 02, 2020, 01:00:28 am »
@Hapax,

I tried two of the methods you wrote; however, its rotation is same before. Maybe, this happens because I write it in a function. I implement it with parameter like c = 0 and then write,

for(c = 0; c <= 90; c++)
    sprite.setRotation(c);
 


does the same thing, direct change in rotation.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Rotation of sprite with small steps
« Reply #3 on: April 02, 2020, 04:01:30 pm »
You need to be updating the rotation once per game loop, not creating a new loop for this.

What you've written will rotate the sprite in steps, but you aren't going to see that rotation because it isn't being drawn to the screen between those steps. In your game loop you should be checking the rotation of your sprite and, if it's less than 90, incrementing the angle by a small amount. Then the rest of your game loop should be clearing and drawing everything.

UtkuDemir

  • Guest
Re: Rotation of sprite with small steps
« Reply #4 on: April 03, 2020, 11:38:00 am »
@Arcade,

I tried updating the rotation by small amount. It works correctly until it comes to 180 degree rotation.

Here is my function:

void Vehicle::move(float &rotation, int &c, sf::Vector2f &direction, sf::Vector2f &currentVelocity, float &maxVelocity, float &acceleration, float &tempSpeed, int &increment, int &increment2, float &x, float &y, sf::Sprite &mySprite)
{
    x += increment; //increment = 1
    y += increment2; //increment2 = 0

    mySprite.setOrigin(sf::Vector2f(mySprite.getGlobalBounds().width / 2, mySprite.getGlobalBounds().height / 2));

    direction = sf::Vector2f(0.f, 0.f);

    if((x >= 130 && x < 239*2 + 100) && (y >= 130 && y < 239*2 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation > 0)
        {
            rotation -= 1;
            cout << "rotation in first 0: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(10.f * tempSpeed / 1000, 0);
            increment = 1;
            increment2 = 0;

            //Acceleration
            direction.x = 1.f;

            if(currentVelocity.x < maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 239*2 + 100 && x < 239*4 + 100) && (y >= 130 && y < 239*2 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation >= 0 && rotation < 90)
        {
            rotation += 1;
            cout << "rotation in first 90: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * tempSpeed / 1000);
            increment = 0;
            increment2 = 1;

            //Acceleration
            direction.y = 1.f;

            if(currentVelocity.y < maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }

    else if((x >= 239*2 + 100 && x < 239*4 + 100) && (y >= 239*2 + 100 && y < 239*4 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation > 0)
        {
            rotation -= 1;
            cout << "rotation in second 0: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);

            mySprite.move(10.f * tempSpeed / 1000, 0);
            increment = 1;
            increment2 = 0;

            //Acceleration
            direction.x = 1.f;

            if(currentVelocity.x < maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 239*4 + 100 && x < 239*5 + 100) && (y >= 239*2 + 100 && y < 239*4 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation < 90)
        {
            rotation += 1;
            cout << "rotation in second 90: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * tempSpeed / 1000);
            increment = 0;
            increment2 = 1;

            //Acceleration
            direction.y = 1.f;

            if(currentVelocity.y < maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }

    else if((x >= 239*4 + 100 && x < 239*5 + 100) && (y >= 239*4 + 100 && y < 239*5 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation >= 90 && rotation < 180)
        {
            rotation += 1;
            cout << "rotation in 180: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(10.f * -tempSpeed / 1000, 0);
            increment = -1;
            increment2 = 0;

            //Acceleration
            direction.x = -1.f;

            if(currentVelocity.x > -maxVelocity)
                currentVelocity.x += acceleration * direction.x * tempSpeed;
        }
    }

    else if((x >= 130 && x < 239*2 + 100) && (y >= 239*4 + 100 && y < 239*5 + 100))
    {
        rotation = mySprite.getRotation();

        if(rotation >= 180 && rotation < 270)
        {
            rotation += 1;
            cout << "rotation in 270: " << rotation;
            cout << endl;

            mySprite.setRotation(rotation);
            mySprite.move(0, 10.f * -tempSpeed / 1000);
            increment = 0;
            increment2 = -1;

            //Acceleration
            direction.y = -1.f;

            if(currentVelocity.y > -maxVelocity)
                currentVelocity.y += acceleration * direction.y * tempSpeed;
        }
    }
}
 


And, the output is:

https://imgur.com/CI1QrmV

It stops at 91 degree whilst it should increase up to 180 degree.

How can I increase the rotation up to 180 degree?
« Last Edit: April 03, 2020, 11:41:42 am by UtkuDemir »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotation of sprite with small steps
« Reply #5 on: April 03, 2020, 05:56:03 pm »
It looks like it's this line:
else if((x >= 239*4 + 100 && x < 239*5 + 100) && (y >= 239*4 + 100 && y < 239*5 + 100))
It's firing once but not more than once.
Unfortunately, no-one else will know what these magic numbers are so only you can figure this one out. :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*