14 Replies - 1085 Views - Last Post: 04 October 2013 - 04:08 PM Rate Topic: -----

#1 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Getting bounds of object from an arraylist.

Posted 04 October 2013 - 10:46 AM

Hi,

I apologize if this is in the wrong board. The moderators can move it if it is.

I have two abstract classes. Rocket and Projectile, which look like this (I have only left the relevant parts)
    public abstract class Rocket extends Entity{  
          
        protected int dx, dy;  
        protected int y;  
        protected int width = 20, height = 20;  
        protected BufferedImage rocket = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
      
        public Rocket(int x, int y, int health, int dir, int speed) {  
            super(x, y, health, dir);  
            this.y = y;  
            this.dy = speed;  
            this.dx = speed;  
            visible = false;  
        }  
          
        public void tick(){  
            y = y + dy;  
              
            if(y > Game.WIDTH * Game.SCALE){  
                visible = true;  
            }  
              
        }  
          
        public void render(Graphics g){  
            g.drawImage(rocket, x, y, null);  
            g.fillRect(x, y, width, height); // temporary  
        }  
          
        public Rectangle bounds() {  
             return new Rectangle(x, y, width, height);  
         }  
    } 


    public abstract class Projectile extends Entity{  
          
        protected int xOrigin, yOrigin;  
        protected double angle;  
        protected BufferedImage projectile;  
        protected double nx, ny;  
        protected int x, y;  
        protected double speed, range, damage;  
        protected boolean removed = false;  
        public static Rectangle bounds;  
      
        public Projectile(int x, int y, int health, double dir) {  
            super(x, y, health, dir);  
            xOrigin = x;  
            yOrigin = y;  
            angle = dir;  
            this.x = x;  
            this.y = y;  
        }  
          
        protected void move(){  
              
        }  
          
        public boolean isRemoved(){  
            return removed;  
        }  
      
        public Rectangle bounds() {  
            return new Rectangle((int)x, (int)y, width, height);  
        }  
          
    }  




Then I have subclasses extending those two classes (I will show NormalRocket and MachineGunProjectile) which call the super of bounds like so:
    public class NormalRocket extends Rocket{  
          
        public int x;  
        public int y;  
        int dx, dy;  
        public int width = 20, height = 20;  
        int burnDuration = 20;  
        int timer = 0;  
        Base base = new Base(0, 0, 0, 0, 0, 0);  
          
        public NormalRocket(int x, int y, int health, int dir, int speed) {  
            super(x, y, health, dir, speed);  
            this.x = x;  
            this.y = y;  
            visible = false;  
            try {  
                rocket = ImageIO.read(new File("res/rocket_normal.png"));  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
          
        public void tick(){  
            super.tick();  
        }  
          
        public void render(Graphics g){  
            super.render(g);  
        }  
          
        public Rectangle bounds(){  
            return super.bounds();  
        }  
          
        public boolean getVisible(){  
            return visible;  
        }  
          
    }  


    public class MachineGunProjectile extends Projectile{  
          
        private int width = 6, height = 6;  
        private int x, y;   
        public static final int FIRE_RATE = 15;  
          
        public MachineGunProjectile(int x, int y, int health, double dir){  
            super(x, y, health, dir);  
            this.x = x;  
            this.y = y;  
            range = 300;  
            speed = 4;  
            damage = 10;  
            nx = speed * Math.cos(angle);  
            ny = speed * Math.sin(angle);  
            bounds = new Rectangle(this.x, this.y, this.width, this.height);  
            try {  
                projectile = ImageIO.read(new File("res//MachineGunProjectile.png"));  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
          
        public void tick(){  
            move();  
              
            if(x > Game.WIDTH * Game.SCALE || x < 0 || y > Game.HEIGHT * Game.SCALE || y < 0){  
                removed = true;  
            }  
        }  
          
        protected void move(){  
            x += nx;  
            y += ny;  
        }  
          
        public boolean isRemoved(){  
            return super.isRemoved();  
        }  
          
        public Rectangle bounds(){  
            return super.bounds();  
        }  
          
        public void collision(Rocket rocket){  
            super.collision(rocket);  
        }  
          
        public void render(Graphics g){  
            g.drawImage(projectile, (int)x, (int)y, null);  
        }  
      
    }  




I have a level class which contains two lists, one for rockets and the other for projectiles, and rockets and projectiles are added to the respective lists:
    public static ArrayList<Projectile> projectiles = new ArrayList<Projectile>();  
        private static ArrayList<Rocket> rockets = new ArrayList<Rocket>();  
      
    private void spawnRocket(int rocketType, int x) {  
            switch (rocketType) {  
                case ROCKET_NORMAL:  
                    rockets.add(new NormalRocket(x, -10, 80, 0, 2));  
                    rocketsSpawned++;  
                    rocketsOnScreen++;  
                    break;  
                case ROCKET_FIRE:  
                    if(difficulty > 1 && random.nextInt(100) > fireRocketSpawn){  
                            rockets.add(new FireRocket(x, -10, 70, 0, 2));  
                            rocketsSpawned++;  
                            rocketsOnScreen++;  
                    }else{  
                        return;  
                    }  
                    break;  
                case ROCKET_ZIPPER:  
                    if(difficulty > 2 && random.nextInt(100) > zipperRocketSpawn){  
                        rockets.add(new ZipperRocket(x, -10, 40, 0, 4));  
                        rocketsSpawned++;  
                        rocketsOnScreen++;  
                    }else{  
                        return;  
                    }  
                    break;  
                case ROCKET_TANK:  
                    if(difficulty > 3 && random.nextInt(100) > tankRocketSpawn){  
                        rockets.add(new TankRocket(x, -10, 130, 0, 1));  
                        rocketsSpawned++;  
                        rocketsOnScreen++;  
                    }else{  
                        return;  
                    }  
                    break;  
            }  
        }   




I am using the following code to check for collision between the projectiles and rockets. The problem is, that "HIT" is not printed and I cannot figure out why:
    private void collision(){  
            for(int i = 0; i < projectiles.size(); i++){  
                for(int j = 0; j < rockets.size(); j++){  
                    if(projectiles.get(i).bounds().intersects(rockets.get(j).bounds())){  
                        System.out.println("HIT!");  
                    }  
                }  
            }  
        }  




I do not know why this does not work, I have tried 2 different techniques so far and neither work. Can anyone help me solve this problem? I can post more code or an image if it is necessary.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting bounds of object from an arraylist.

#2 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 10:51 AM

Abstract classes can't extend other classes. Abstract classes cannot be instantiated, but they can be subclassed. You can't have class -> abstract class -> class, only abstract class -> class

This post has been edited by Michael26: 04 October 2013 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 10:58 AM

Sorry, I forgot to mention. Entity is an abstract class as well.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 11:30 AM

View PostMichael26, on 04 October 2013 - 12:51 PM, said:

Abstract classes can't extend other classes. Abstract classes cannot be instantiated, but they can be subclassed. You can't have class -> abstract class -> class, only abstract class -> class


This is incorrect. Abstract classes can extend other classes. Abstract classes cannot be instantiated, and they can contain abstract methods, but otherwise they are just classes.
public class Foo {
	public void frobnicate() {
 		System.out.println("frobnicating...");
	}
}


public abstract class Bar extends Foo {
	public abstract void rotateWidgets();
}


public class Baz extends Bar {
	public static void main(String[] args){
		Baz baz = new Baz();
		baz.frobnicate();
		baz.rotateWidgets(); 
	}

	public void rotateWidgets(){
		System.out.println("Rotating Widgets");
	}
}




Output:

/Users/jon/java/junk:611 $ java Baz
frobnicating...
Rotating Widgets
[jon: Fri Oct 04 14:28]

H3XAGON, please post the error output. That'll make is a lot easier to track down your issue.
Was This Post Helpful? 0
  • +
  • -

#5 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 11:42 AM

Hi Jon,

I don't understand what you mean by error output. There is no error that appears in the console or anything like that. Simply, "HIT" is not printed out when it "should" be.
Was This Post Helpful? 0
  • +
  • -

#6 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 11:45 AM

So you have abstract class extending another abstract class? You need to have 1 abstract class, like projectile and your sub-classes Rocket, MachineGun ... those sub-classes have something in common with Projectile class. See the concept of abstraction page 22.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 11:50 AM

Hi Micheal,

Thanks for the response, but does this somehow relate to the problem at hand?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:06 PM

Oh, I misunderstood. I had it in my head that you were getting an out of bounds exception, which obviously is not what you said at all. My bad.

private void collision(){  
        for(int i = 0; i < projectiles.size(); i++){  
            for(int j = 0; j < rockets.size(); j++){  
                if(projectiles.get(i).bounds().intersects(rockets.get(j).bounds())){  
                    System.out.println("HIT!");  
                }  
            }  
        }  
    }  




Not to be too obvious about it, but the reason "HIT" is not printed is because that condition does not return a true. Why is that?
This is the art of debugging: work your way back from the place where things aren't going the way you want them to. So you need to figure out why the intersects() method is not returning true. Intersect is a method of Rectangle, so we'll assume that's working correctly. (I'm assuming this is the library class, and not your own Rectangle class)

So two possibilities would be that your bounds() method is not returning the right thing, or that the items in your data set don't actually intersect.

You can examine both of these if you replace the if statement in that loop with a line that prints out the bounds() of objects i and j. This is usually a good sanity check: examine the data at the point where the program is failing.

Let us know what you find.
Was This Post Helpful? 0
  • +
  • -

#9 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:07 PM

Sorry jon, i meant Abstract class can't extend other abstract classes
H3XAGON it is good class design practice.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:09 PM

View PostMichael26, on 04 October 2013 - 02:07 PM, said:

Sorry jon, i meant Abstract class can't extend other abstract classes


Also incorrect. Abstract classes are just like any classes. They can extend other classes, they can implement interfaces, they can do anything that other classes can do. The only thing you can't do with them is to instantiate them.
Was This Post Helpful? 0
  • +
  • -

#11 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:10 PM

Thanks Jon, I'll try what you said.
Was This Post Helpful? 0
  • +
  • -

#12 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:17 PM

Really sorry about the double post, but I couldn't find the edit button.

Anyway, I printed out the bounds of each object and I have discovered that the x and y of the projectile are never updated, and the width and height are always 0. I might have an idea of why this is, and I will work on figuring it out, but if anyone can guess at why this is happening, that will also be appreciated.
Was This Post Helpful? 0
  • +
  • -

#13 H3XAGON   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 04-October 13

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:29 PM

Hi, I know this is a triple post and I am really sorry, but I would like to say thank you to everyone that helped. I have managed to fix this issue, which has been holding back my project for days.

The problem was that the way I set up the inheritance on projectile and it's subclass didn't actually update the variables that it was meant to.

Thanks again!
Was This Post Helpful? 1
  • +
  • -

#14 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 12:40 PM

Glad you were able to sort it.
Was This Post Helpful? 0
  • +
  • -

#15 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Getting bounds of object from an arraylist.

Posted 04 October 2013 - 04:08 PM

View PostMichael26, on 04 October 2013 - 01:51 PM, said:

Abstract classes can't extend other classes. Abstract classes cannot be instantiated, but they can be subclassed. You can't have class -> abstract class -> class, only abstract class -> class

Completly false !
This is perfectly legal

abstract public class foo {

}

abstract public class foo1 extends foo {

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1