I have an Arraylist with objects that contains the position and rotation of an object which i want to draw on screen using Graphics2D.
public void render(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.white);
for(PhysicObject object : entities) {
if (object.getBody().getType() == BodyType.DYNAMIC) {
Vec2 position = object.getBody().getPosition().mul(30);
g.translate(position.x, position.y);
g.rotate(object.getBody().getAngle());
g.fillRect((int)-(object.width), (int)-(object.height), (int)(object.width*2), (int)(object.height*2));
}
}
}
The first object always rotates correct, but the following rotate around the first one and not around themselves.
Hope someone can help me, thanks.
To rotate each object independently you have to undo the translation and rotation of the first object. So the simplest way is to use an AffineTransform to “combine” the two transformations and undo them easier. e.g.
The createInverse() creates the “opposite” transformation and returns the Graphics space to its original state. Then the next transformation should work fine.