Search code examples
javaawtjava-2daffinetransform

Getting the x and y point of a transformed object


I am transforming the barrel of a turret with AffineTransform, and I want a bullet to shoot right out of the tip of the barrel. Is there a method in shape to get these coordinates or do I have to calculate it manually?

code for transform

AffineTransform rotate = AffineTransform.getRotateInstance(rotation, getX() + getWidth()/2, getY() + getHeight()/2);
barrel = rotate.createTransformedShape(new Rectangle(getX() + getWidth()/2, getY() - getHeight()/2, 2, getHeight()/2 + 1));

code for bulllet

int dx = getX() - o.getX();
int dy = o.getY() - getY();
bullets.add(new Bullet((int)barrel.getBounds2D().getX(), (int)barrel.getBounds2D().getY(), SPEED, new NVector(dx, dy)));

Solution

  • Use the same transform (rotate) to transform the coordinate of the tip of the barrel:

    rotate.transform(tipOfTheBarrel, transformedTipOfTheBarrel);