java - Polygon moves while being rotated -
i'm doing couple of tests polygons (rotating them) , i've run annoying issue crops while i'm rotating them. issue is, polygons seem moving when rotate, , fly out of screen. i've been using affinetransform rotate them, , haven't been translating them shouldn't moving, right? here's code:
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(); (int = 0; < 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(); } }
i'd know what's going wrong code, , if there's better way around i'm trying achieve (rotating polygons). here's render , rotate polygons:
g2.setcolor(color.pink); (physicsprop p : currentlevel.physicsobjects){ g2.drawpolygon(p.pointstopoly(p.newpoints)); p.rotatemodel(0.001); }
thanks help!
try caching centerpoint of polygon, , using cached point point around rotate. nothing guarantees you'll calculate same exact x/y center point arbitrary polygon in arbitrary rotation, remove risk (and cpu cycles) , cache once , reuse there.
Comments
Post a Comment