0

I want to make a simple shooting game. I have a Plane Class that contains IMG and Shape ( This is a border for that IMG, so I can use isIntersect method) attribute.

Then I rotate and translate the object ( Using AffineTransform ), after that I want to get the coordinate of it. Can someone help me?

Ps: I tried to use Rectangle than a Shape, but it seems that I can't make a Rectangle Object with CreateTransformedShape.

Here is my code :

public class EPlane {
Shape l = new Rectangle(0,0,45,55);
BufferedImage ep;
private String imgFileName = "Eplane.PNG";
ArrayList<Rectangle> EShoot = new ArrayList();
int live;
AffineTransform at;
AffineTransform at2;
AffineTransform at3;
boolean Alrdy = false;

EPlane(){
    this.live = 5;
    at = new AffineTransform();
    at2 = new AffineTransform();
    at3 = new AffineTransform();
    URL imgUrl = getClass().getClassLoader().getResource(imgFileName);

        if (imgUrl == null) {
            System.err.println("couldn't find file: " + imgFileName);
        } else {
            try {
                ep = ImageIO.read(imgUrl);

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
}

And the class who want to find the x Coordinate

public class EPlaneCommander {

ArrayList<ArrayList<EPlane>> EPSList = new ArrayList();
int count = 0;
int count1 = 0;
int count2 = 0;

public EPlaneCommander() {
    ArrayList<EPlane> EPS = new ArrayList();
    ArrayList<EPlane> EPS1 = new ArrayList();
    ArrayList<EPlane> EPS2 = new ArrayList();
    EPSList.add(EPS);
    EPSList.add(EPS1);
    EPSList.add(EPS2);
}

public void move1() {
    if (count % 50 == 1 && EPSList.get(0).size() < 10) {

        EPSList.get(0).add(new EPlane());
    }

    for (int i = 0; i < EPSList.get(1).size(); i++) {
        if (!EPSList.get(0).get(i).Alrdy) {
            EPSList.get(0).get(i).at.translate(400, 0);
            EPSList.get(0).get(i).Alrdy = true;               
            EPSList.get(0).get(i).l = EPSList.get(0).get(i).at.createTransformedShape(EPSList.get(0).get(i).l);
            EPSList.get(0).get(i).at2.rotate(0.004, 0, 0);
        }

        //EPS.get(i).at2.rotate(0.0001, 0, 0);
        EPSList.get(0).get(i).l = EPSList.get(0).get(i).at2.createTransformedShape(EPSList.get(0).get(i).l);
        EPSList.get(0).get(i).at.rotate(0.004, -400, 0);
        Point s = (Point) EPSList.get(2).get(i).at.createTransformedShape(EPSList.get(0).get(i).l);
    }

    count++;
}

2 Answers2

1

Java's Shape class has a getBounds() method that can be used to obtain a Rectangle object that includes an X and Y coordinate. Bear in mind that with Java graphics, those coordinates represent the upper left hand corner of the Rectangle, as opposed to the lower left hand that you're used to for typical Cartesian systems.

That Rectangle can also be used as a bounding box for purposes of collision detection in your implementation of player missile graphics, but Shape already has an intersects method that can be used as a bounding box.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
0

Bounding box around the transformed shape will be bigger and can be inaccurate in certain points. You can convert the Shape into Area and check for intersection.

Here is what I found in another answer. Java collision detection between two Shape objects?