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.