I want to rotate a group of sprites, polygons and rectangles together. Figure a) shows 2 sprites, boundary polygons and rectangles. This group is rotated 90 degrees around the center of the big rectangle (union of two rectangles).
Following code creates Figure b). It does not rotate sprite and polygon around the union rectangle's center.
public void rotate(int rangle, float rotateX, float rotateY){
this.sprite.rotate(rangle);
this.polygon.rotate(rangle);
Polygon tempPoly = new Polygon(new float[] {
this.rectangle.x, this.rectangle.y,
this.rectangle.x, this.rectangle.y + this.rectangle.height,
this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height,
this.rectangle.x + this.rectangle.width, this.rectangle.y
});
tempPoly.setOrigin(rotateX, rotateY);
tempPoly.rotate(rangle);
this.rectangle = null;
this.rectangle = tempPoly.getBoundingRectangle();
}
Following code creates Figure c). There is a miss alignment, when I try to move the sprite and polygon to rectangle position.
public void rotate(int rangle, float rotateX, float rotateY){
this.sprite.rotate(rangle);
this.polygon.rotate(rangle);
Polygon tempPoly = new Polygon(new float[] {
this.rectangle.x, this.rectangle.y,
this.rectangle.x, this.rectangle.y + this.rectangle.height,
this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height,
this.rectangle.x + this.rectangle.width, this.rectangle.y
});
tempPoly.setOrigin(rotateX, rotateY);
tempPoly.rotate(rangle);
this.rectangle = null;
this.rectangle = tempPoly.getBoundingRectangle();
// This tries to move them to rectangle
this.sprite.setPosition(this.rectangle.getX(), this.rectangle.getY());
this.polygon.setPosition(this.rectangle.getX(), this.rectangle.getY());
}
The question is how to align sprites with rectangle after rotation (i.e. similar to Figure a, in angle =0). Hint: The issue arises because sprites are rotated when they are drawn so there is a miss alignment between sprite and rectangle. (link)