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

Author Topic: Rotating a sprite is causing some really weird bugs  (Read 6445 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Rotating a sprite is causing some really weird bugs
« on: October 02, 2014, 05:56:14 pm »
I'm trying to implement rotation into my tetris clone and it is going terribly. Basically, when I try and set the tetrimino's sprite's origin to its center so I can rotate it without changing the position any, the sprite moves a lot farther on the screen that it should. Like it moves by tens of pixels each time. Here is the rotate function:
void GamePiece::RotateLeft(){
        if(originCount != 10){
                bool rotate = true;
                int newx, newy;
                sf::Vector2f origin(pieceRectangles_[0].getPosition().x + originCount, pieceRectangles_[0].getPosition().y + originCount);
                for(int i = 0; i < 4; i++){
                        newx = (origin.x + origin.y - pieceRectangles_[i].getPosition().y - 10);
                        newy = (pieceRectangles_[i].getPosition().x + origin.y - origin.x);

                        sf::RectangleShape temp = pieceRectangles_[i];
                        temp.setPosition(newx, newy);
                        sf::FloatRect fr = temp.getGlobalBounds();

                        for(int j = 0; j < levelCollisions.size(); j++){
                                if(fr.intersects(levelCollisions[j])){
                                        rotate = false;
                                }
                        }
                }
                if(rotate == true){
                        for(int i = 0; i < 4; i++){
                                newx = (origin.x + origin.y - pieceRectangles_[i].getPosition().y - 10);
                                newy = (pieceRectangles_[i].getPosition().x + origin.y - origin.x);
                                pieceRectangles_[i].setPosition(newx, newy);
                        }
                        pieceShape.setOrigin(pieceShape.getPosition().x + pieceShape.getGlobalBounds().width / 2, pieceShape.getPosition().y + pieceShape.getGlobalBounds                                                       ().height / 2);
                        pieceShape.rotate(90);
                        pieceShape.setOrigin(pieceShape.getPosition());
                }
        }
}

The first half with the for loops and such is the part that rotates all the individual squares which I use for collision detection. This part works perfectly (I have their color set to blue so I can see where they are moving versus the new position of the actual sprite). The problem starts at pieceShape.setOrigin(). I'm trying to set the sprite's origin to its center so I can rotate it smoothly, but the sprite spirals out from where it should have been, with increasing distance everytime.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Rotating a sprite is causing some really weird bugs
« Reply #1 on: October 02, 2014, 06:06:06 pm »
Position/Rotation/Scale all happen around the origin when the sprite is drawn, not when rotate(...) is called.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Rotating a sprite is causing some really weird bugs
« Reply #2 on: October 02, 2014, 06:08:24 pm »
Right I read that in the documentation... So, do I have to go through and change all of my other transforming functions to accomodate a new origin?

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Rotating a sprite is causing some really weird bugs
« Reply #3 on: October 02, 2014, 06:11:32 pm »
 pieceShape.setOrigin(pieceShape.getPosition().x + pieceShape.getGlobalBounds().width / 2, pieceShape.getPosition().y + pieceShape.getGlobalBounds().height / 2);
            pieceShape.rotate(90);
            pieceShape.setOrigin(pieceShape.getPosition());
Update, if I comment out the last two lines and only set the origin, the sprite is placed back at (0,0). Why?

Update update:: That was a stupid question. But, if I do set the origin to the sprite's center in the constructor, it's still not rotating around its center. This function does set the origin to the center, right:
pieceShape.setOrigin(pieceShape.getPosition().x + pieceShape.getGlobalBounds().width / 2, pieceShape.getPosition().y + pieceShape.getGlobalBounds().height / 2);
?
« Last Edit: October 02, 2014, 06:23:14 pm by wh1t3crayon »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Rotating a sprite is causing some really weird bugs
« Reply #4 on: October 02, 2014, 06:45:35 pm »
That is another thing I didn't see in your original code, but origin is relative to the sprite/transformable - not to the world (position is relative to the world).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating a sprite is causing some really weird bugs
« Reply #5 on: October 02, 2014, 06:48:31 pm »
This function does set the origin to the center, right:
pieceShape.setOrigin(pieceShape.getPosition().x + pieceShape.getGlobalBounds().width / 2, pieceShape.getPosition().y + pieceShape.getGlobalBounds().height / 2);
?

Does this work instead:
pieceShape.setOrigin(pieceShape.getLocalBounds().x / 2, pieceShape.getLocalBounds().y / 2);

p.s. Why do I keep just giving the answer to people...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Rotating a sprite is causing some really weird bugs
« Reply #6 on: October 02, 2014, 07:06:22 pm »
p.s. Why do I keep just giving the answer to people...

Because you believe people can't figure out things on their own and they must C&P code to get anywhere.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating a sprite is causing some really weird bugs
« Reply #7 on: October 02, 2014, 07:13:59 pm »
you believe people can't figure out things on their own and they must C&P code to get anywhere.
Not fully true; I guess I'm just 'saving them the trouble'. Unfortunately, this can often have the adverse effect of getting into the habit of having answers spoon-fed. Sometimes, I'm just too nice  :(
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Rotating a sprite is causing some really weird bugs
« Reply #8 on: October 03, 2014, 02:29:23 pm »
Quote
Does this work instead:
pieceShape.setOrigin(pieceShape.getLocalBounds().x / 2, pieceShape.getLocalBounds().y / 2);

p.s. Why do I keep just giving the answer to people...

It does not work, it actually throws an error because getLocalBounds() does not have a member 'x' or 'y'. I believe you meant
pieceShape.setOrigin(pieceShape.getLocalBounds().width / 2, pieceShape.getLocalBounds().height / 2);
However, this doesn't work. The piece is still not rotating around its center, but it's not rotating around 0,0 either.

The image below shows the four orientations of my sprite. The blue line is where the piece is supposed to be rotating, and the red line is where is actually ends up. So it's rotating close to its center, but not exactly.
« Last Edit: October 03, 2014, 05:13:41 pm by wh1t3crayon »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Rotating a sprite is causing some really weird bugs
« Reply #9 on: October 03, 2014, 06:25:08 pm »
Try using floating point division rather than integer division.
ie:
pieceShape.setOrigin(pieceShape.getLocalBounds().width / 2.f, pieceShape.getLocalBounds().height / 2.f);

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating a sprite is causing some really weird bugs
« Reply #10 on: October 03, 2014, 06:54:19 pm »
It does not work...I believe you mean.
I see that I'm subconciously still getting people to help themselves  ;)

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Rotating a sprite is causing some really weird bugs
« Reply #11 on: October 03, 2014, 07:18:44 pm »
Try using floating point division rather than integer division.
ie:
pieceShape.setOrigin(pieceShape.getLocalBounds().width / 2.f, pieceShape.getLocalBounds().height / 2.f);
That produced no change at all. I discovered that if I just comment out the line where I set the origin, then there is also no change in the rotation. In other words, no matter what I set the origin to the piece rotates around the same point.
//pieceShape.setOrigin(pieceShape.getLocalBounds().width / 2.f, pieceShape.getLocalBounds().height / 2.f);
So, this function essentially does nothing, commented out or not

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating a sprite is causing some really weird bugs
« Reply #12 on: October 03, 2014, 07:43:05 pm »
Does this work?
pieceShape.setOrigin(static_cast<float>(pieceShape.getLocalBounds().width) / 2.f, static_cast<float>(pieceShape.getLocalBounds().height) / 2.f);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Rotating a sprite is causing some really weird bugs
« Reply #13 on: October 05, 2014, 12:27:36 pm »
Are you sure you read the tutorial about transformables? I think you are forgetting that changing the origin also relatively changes the position of the sprite.

Just draw a sprite at (0,0) with it's origin at (0,0) and then with it's origin changed. See what changes. Hapax last suggestion should not change anything because of how casts work is C++, but it's safer anyway how he writes it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotating a sprite is causing some really weird bugs
« Reply #14 on: October 05, 2014, 02:19:50 pm »
Both operands are floats, the static_cast is totally useless.
Laurent Gomila - SFML developer

 

anything