我正在使用多边形(旋转它们)进行一些测试,并且在旋转它们时遇到了一个烦人的问题。问题是,多边形在旋转时似乎在移动,最终它们飞出屏幕。我一直在使用 AffineTransform 来旋转它们,但我没有翻译它们,所以它们不应该移动,对吧?这是代码:
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
public class PhysicsProp {
Point2D[] OriginalPoints;
Point2D[] NewPoints = OriginalPoints;
int X;
int Y;
double rotation = 0.0;
public PhysicsProp(Point2D[] Points){
OriginalPoints = Points;
NewPoints = Points;
}
public Polygon PointsToPoly(Point2D[] Points){
Polygon p = new Polygon();
for (int i = 0; i < Points.length; i++){
p.addPoint((int) Points[i].getX(), (int) Points[i].getY());
}
return p;
}
public void rotateModel(double theta){
AffineTransform.getRotateInstance(Math.toDegrees(theta), PointsToPoly(NewPoints).getBounds().getCenterX(), PointsToPoly(NewPoints).getBounds().getCenterY()).transform(OriginalPoints, 0, NewPoints, 0, OriginalPoints.length);
}
public Polygon getModel(){
return PointsToPoly(NewPoints);
}
public void setModel(Point[] points){
OriginalPoints = points;
}
public Rectangle getBounds(){
return PointsToPoly(NewPoints).getBounds();
}
}
我想知道这段代码到底出了什么问题,以及是否有更好的方法来解决我想要实现的目标(旋转多边形)。这是我渲染和旋转多边形的地方:
g2.setColor(Color.pink);
for (PhysicsProp p : CurrentLevel.PhysicsObjects){
g2.drawPolygon(p.PointsToPoly(p.NewPoints));
p.rotateModel(0.001);
}
谢谢你的帮助!