1

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)));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ceptno
  • 687
  • 2
  • 6
  • 28
  • 1
    You cod have a look at [this example](http://stackoverflow.com/questions/12964983/rotate-image-around-character-java/12971987#12971987), which demonstrates a means by which you can calculate the point a distance from the centre given a particular angle – MadProgrammer Dec 21 '12 at 08:47

2 Answers2

2

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

rotate.transform(tipOfTheBarrel, transformedTipOfTheBarrel);
dacwe
  • 43,066
  • 12
  • 116
  • 140
0

When your barrel is a rotated rectangle, then you can't use it because your bullet should probably come out of the middle of the side of the rectangle and not one of the corners.

But if you create your own shape and you make sure the point where the bullets appear is the first in the shape, then you can use getPathIterator() to get the transformed point.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820