Results 1 to 19 of 19
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By JosAH

Thread: Object collision

  1. #1
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Object collision

    hi, im creating a game where you need to eat fishes to grow.
    I just have one problem. How can I make it that when my main fish touches another fish, the other fish dissapears?
    heres my code:
    Java Code:
    package visje;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JApplet;
    
    /**
     *
     * @author 111946
     */
    
    public class Visje extends JApplet implements MouseMotionListener {
        
        private Image main_fish, greenfish_r, blueredfish_r, bluefish_l, butterflyfish_l;
        private Image rockfish_r, rockfish_l;
        private Image background1, scoreboard;
        int mouseX, mouseY, fishX, fishY;
        Graphics bufferGraphics;
        Image offscreen;
        Dimension dim;
        Font scorefont = new Font( "Arial", Font.BOLD, 17 );
        int delay = 0;
        int mainsize = 2;
        int blueredfishsize = 1;
        int greenfishsize = 3;
        int bluefishsize = 3;
        int score = 0;
        int xp = 0;
        
        @Override
        public void init() {
            background1 = getImage( getDocumentBase(), "background 1.png");
            main_fish = getImage( getDocumentBase(), "main_right1.png" );
            greenfish_r = getImage( getDocumentBase(), "greenfish_r.png" );
            bluefish_l = getImage( getDocumentBase(), "bluefish_l.png" );
            blueredfish_r = getImage( getDocumentBase(), "blueredfish_r.png" );
            butterflyfish_l = getImage( getDocumentBase(), "butterflyfish_l.png" );
            rockfish_r = getImage( getDocumentBase(), "rockfish_r.png" );
            rockfish_l = getImage( getDocumentBase(), "rockfish_l.png" );
            scoreboard = getImage( getDocumentBase(), "scoreboard.png" );
           
            addMouseMotionListener(this);
            setSize( 1000, 600 );
            dim = getSize();
            offscreen = createImage( dim.width, dim.height );
            bufferGraphics = offscreen.getGraphics();
        }
        
        int gf1X = -100,  gf1Y = 200;
        int bf1X = 1200,  bf1Y = 250;
        int bf2X = 1300,  bf2Y = 275;
        int brf1X = -80,  brf1Y = 140;
        int brf2X = -200, brf2Y = 100;
        int bff1X = 1300, bff1Y = 170;
        int bff2X = 1400, bff2Y = 200;
        int bff3X = 1050, bff3Y = 290;
        int rf1X = -300,  rf1Y = 400;
        int rf2X = 1600,  rf2Y = 450;
        
        @Override
        public void paint( Graphics g ) {
            dim = getSize();
            bufferGraphics.clearRect( 0, 0, dim.width, dim.height );
            bufferGraphics.drawImage( background1, 0, 0, this);
            bufferGraphics.drawImage( main_fish, fishX, fishY, this );     
            bufferGraphics.drawImage( greenfish_r, gf1X, gf1Y, this );
            bufferGraphics.drawImage( bluefish_l, bf1X, bf1Y, this );
            bufferGraphics.drawImage( bluefish_l, bf2X, bf2Y, this );
            bufferGraphics.drawImage( blueredfish_r, brf1X, brf1Y, this );
            bufferGraphics.drawImage( blueredfish_r, brf2X, brf2Y, this );
            bufferGraphics.drawImage( butterflyfish_l, bff1X, bff1Y, this);
            bufferGraphics.drawImage( butterflyfish_l, bff2X, bff2Y, this);
            bufferGraphics.drawImage( butterflyfish_l, bff3X, bff3Y, this);
            bufferGraphics.drawImage( rockfish_r, rf1X, rf1Y, this );
            bufferGraphics.drawImage( rockfish_l, rf2X, rf2Y, this );
            
            bufferGraphics.drawImage( scoreboard, 1, 0, this );
            bufferGraphics.setFont( scorefont );
            bufferGraphics.setColor( Color.WHITE );
            bufferGraphics.drawString( "Score: " + score, 10, 20 );
            bufferGraphics.drawString( "XP points: " + xp, 10, 40 );
            g.drawImage( offscreen, 0, 0, this );
            
        }
        
        @Override
        public void update( Graphics g ) {
            paint(g);
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();
            t.start();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
        javax.swing.Timer t = new javax.swing.Timer( 0, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if( fishX < mouseX ) {
                    fishX +=2;
                    main_fish = getImage( getDocumentBase(), "main_right1.png" );
                }
                if( fishX > mouseX ) {
                    fishX -=2;
                    main_fish = getImage( getDocumentBase(), "main_left1.png" );
                }
                if( fishY < mouseY ) { 
                    fishY +=2;
                }
                if( fishY > mouseY ) {
                        fishY -=2;
                }
                repaint();
    
                if( gf1X > 1500 ) {
                    gf1X = -100;
                } else {
                    gf1X += 2;
                }
                
                if( bf1X < -100 ) {
                    bf1X = 1200;
                } else {
                    bf1X -= 2;
                }
                
                if( bf2X < -150 ) {
                    bf2X = 1100;
                } else {
                    bf2X -= 1;
                }
                
                if( brf1X > 1600 ) {
                    brf1X = -80;
                } else {
                    brf1X += 1;
                }
                
                if( brf2X > 1700 ) {
                    brf2X = -200;
                } else {
                    brf2X += 2;
                }
                
                if( bff1X < -200 ) {
                    bff1X = 1200;
                } else {
                    bff1X -= 1;
                }
                
                if( bff2X < -200 ) {
                    bff2X = 1500;
                } else {
                    bff2X -= 1;
                }
                
                if( bff3X < -200 ) {
                    bff3X = 1350;
                } else {
                    bff3X -= 1;
                }
                
                if( rf1X > 1600 ) {
                    rf1X = -300;
                } else {
                    rf1X +=1;
                }
                
                if( rf2X < -300 ) {
                    rf2X = -1600;
                } else {
                    rf2X -=1;
                }
            }
        });
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Object collision

    The simplest approach is as follows: construct an (invisible) bounding rectange around each fish; if the bounding boxes intersect, consider the fish to bounce. If your fish are quite 'fat' (i.e. they barely fit in their bounding box) the inaccuracy isn't noticable. Read the API documentation of the Rectange class for details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    thanks, and is it also possible that only something happens when it touches one side of the rect?, cuz it would be weird if the tail of a fish would eat another fish.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Object collision

    Quote Originally Posted by Joey0412 View Post
    thanks, and is it also possible that only something happens when it touches one side of the rect?, cuz it would be weird if the tail of a fish would eat another fish.
    Sure, first do the 'rude' test (a Rectangle can do it itself); only if you're sure the Rectangles intersect, check which sides overlap.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Object collision

    If you make a bounding box as Jos described, but have it only cover the mouth area, I think you can get the effect you're looking for. In shooting games they call this the hitbox and the size of it varies depending on difficulty, controller type, distance, etc...

    In this way, when something enters the small hitbox covering the mouth, you can trigger the eating event.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Object collision

    Quote Originally Posted by quad64bit View Post
    when something enters the small hitbox covering the mouth, you can trigger the eating event.
    And now I'm hungry...
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    this may sound stupid, but how do I create an invisible rectangle? :P

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Object collision

    There is a Rectangle class in the API. You can have rectangles that exist in some array, but aren't drawn to the screen. The rectangle can move with your sprites, but not actually be displayed. The Rectangle class has methods that can facilitate your intersection checks - like contains().

  9. #9
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    thanks for the help, but I still have one problem :P
    i used your idea about only making a rectangle for the mouth, but the problem is that my fish can face two sides. Now I have an invisible rectangle for my fish when it faces left, but when it faces right it can only eat with its tail.
    I used this code to change which side my fish is facing to:
    Java Code:
    main_fish = getImage( getDocumentBase(), "main_right1.png" );
    main_fish = getImage( getDocumentBase(), "main_rleft1.png" );
    Is there any code where I can check which one of those above is currently active?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Object collision

    You should solve that programatically, e.g. keep track of the orientation of the fish (an excellent candidate for a property of a Fish object) and position the invisible rectangle accordingly. But if those fish move fast enough a user won't even notice that a fish got eaten by the tail of another fish ...

    kind regards,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Object collision

    I concur with Jos.

  12. #12
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    Ok. now I have a problem. My Fish can't die. I have a code that when my fish touches a bigger fish that something happens. But now i can only make it change image.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Object collision

    Make it sink to the bottom, upside down with its eyes closed; when it hits the bottom make it explode; that's dead enough for me.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Object collision

    Zombie fish!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    Quote Originally Posted by JosAH View Post
    Make it sink to the bottom, upside down with its eyes closed; when it hits the bottom make it explode; that's dead enough for me.

    kind regards,

    Jos
    the problem is that it still moves to my mouse when its dead :P.

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Object collision

    Quote Originally Posted by Joey0412 View Post
    the problem is that it still moves to my mouse when its dead :P.
    Another fine (boolean) attribute for a Fish object.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Object collision

    Yes:
    Java Code:
    boolean zombie;
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    i used a
    Java Code:
    package visje;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JApplet;
    
    /**
     *
     * @author 111946
     */
    
    public class Visje extends JApplet implements MouseMotionListener {
        
        private Image main_fish, greenfish_r, blueredfish_r, blueredfish_l, bluefish_l, greenfish_l;
                Image butterflyfish_l, butterflyfish_r;
        private Image rockfish_r, rockfish_l;
        private Image background1, scoreboard;
        int mouseX, mouseY, fishX, fishY;
        Graphics bufferGraphics;
        Image offscreen;
        Dimension dim;
        Font scorefont = new Font( "Arial", Font.BOLD, 17 );
        int delay = 0;
        int mainsize = 2;
        int score = 0;
        int xp = 0;
        
        @Override
        public void init() {
            background1 = getImage( getDocumentBase(), "background 1.png");
            main_fish = getImage( getDocumentBase(), "main_right1.png" );
            greenfish_r = getImage( getDocumentBase(), "greenfish_r.png" );
            greenfish_l = getImage( getDocumentBase(), "greenfish_l.png" );
            bluefish_l = getImage( getDocumentBase(), "bluefish_l.png" );
            blueredfish_r = getImage( getDocumentBase(), "blueredfish_r.png" );
            blueredfish_l = getImage( getDocumentBase(), "blueredfish_l.png" );
            butterflyfish_l = getImage( getDocumentBase(), "butterflyfish_l.png" );
            butterflyfish_r = getImage( getDocumentBase(), "butterflyfish_r.png" );
            rockfish_r = getImage( getDocumentBase(), "rockfish_r.png" );
            rockfish_l = getImage( getDocumentBase(), "rockfish_l.png" );
            scoreboard = getImage( getDocumentBase(), "scoreboard.png" );
           
            addMouseMotionListener(this);
            setSize( 1000, 600 );
            dim = getSize();
            offscreen = createImage( dim.width, dim.height );
            bufferGraphics = offscreen.getGraphics();
        }   
        
        int max = 500;
        int min = 50;
    
        int random1 = min + (int) ( Math.random()*(max - min) );
        int random2 = min + (int) ( Math.random()*(max - min) );
        int random3 = min + (int) ( Math.random()*(max - min) );
        int random4 = min + (int) ( Math.random()*(max - min) );
        int random5 = min + (int) ( Math.random()*(max - min) );
        int random6 = min + (int) ( Math.random()*(max - min) );
        int random7 = min + (int) ( Math.random()*(max - min) );
        int random8 = min + (int) ( Math.random()*(max - min) );
        int random9 = min + (int) ( Math.random()*(max - min) );
        int random10 = min + (int) ( Math.random()*(max - min) );
        int random11 = min + (int) ( Math.random()*(max - min) );
        int random12 = min + (int) ( Math.random()*(max - min) );
        int random13 = min + (int) ( Math.random()*(max - min) );
        int random14 = min + (int) ( Math.random()*(max - min) );
        int random15 = min + (int) ( Math.random()*(max - min) );
        int random16 = min + (int) ( Math.random()*(max - min) );
        int random17 = min + (int) ( Math.random()*(max - min) );
        int random18 = min + (int) ( Math.random()*(max - min) );
        int random19 = min + (int) ( Math.random()*(max - min) );
        int random20 = min + (int) ( Math.random()*(max - min) );
        int random21 = min + (int) ( Math.random()*(max - min) );
        int random22 = min + (int) ( Math.random()*(max - min) );
        
        
        int gf1X = 10,    gf1Y = random1;
        int gf2X = -250,  gf2Y = random2;
        int gf3X = -300,  gf3Y = random3;
        int gf4X = 1100,  gf4Y = random4;
        int gf5X = 1150,  gf5Y = random5;
        int bf1X = 1200,  bf1Y = random6;
        int bf2X = 1300,  bf2Y = random7;
        int brf1X = -80,  brf1Y = random8;
        int brf2X = -200, brf2Y = random9;
        int brf3X = 1200, brf3Y = random10;
        int brf4X = 1100, brf4Y = random11;
        int bff1X = 1300, bff1Y = random12;
        int bff2X = 1400, bff2Y = random13;
        int bff3X = 1050, bff3Y = random14;
        int bff4X = -200, bff4Y = random15;
        int bff5X = -145, bff5Y = random16;
        int rf1X = -300,  rf1Y = random17;
        int rf2X = 1200,  rf2Y = random18;
        int rf3X = 1300,  rf3Y = random19;
        int rf4X = -250,  rf4Y = random20;
        int rf5X = -140,  rf5Y = random21;
        int rf6X = 1200,  rf6Y = random22;
        
        @Override
        public void paint( Graphics g ) {
            dim = getSize();
            bufferGraphics.clearRect( 0, 0, dim.width, dim.height );
            bufferGraphics.drawImage( background1, 0, 0, this);
            bufferGraphics.drawImage( main_fish, fishX, fishY, this );     
            bufferGraphics.drawImage( greenfish_r, gf1X, gf1Y, this );
            bufferGraphics.drawImage( greenfish_r, gf2X, gf2Y, this );
            bufferGraphics.drawImage( greenfish_r, gf3X, gf3Y, this );
            bufferGraphics.drawImage( greenfish_l, gf4X, gf4Y, this );
            bufferGraphics.drawImage( greenfish_l, gf5X, gf5Y, this );
            bufferGraphics.drawImage( bluefish_l, bf1X, bf1Y, this );
            bufferGraphics.drawImage( bluefish_l, bf2X, bf2Y, this );
            bufferGraphics.drawImage( blueredfish_r, brf1X, brf1Y, this );
            bufferGraphics.drawImage( blueredfish_r, brf2X, brf2Y, this );
            bufferGraphics.drawImage( blueredfish_l, brf3X, brf3Y, this );
            bufferGraphics.drawImage( blueredfish_l, brf4X, brf4Y, this );
            bufferGraphics.drawImage( butterflyfish_l, bff1X, bff1Y, this);
            bufferGraphics.drawImage( butterflyfish_l, bff2X, bff2Y, this);
            bufferGraphics.drawImage( butterflyfish_l, bff3X, bff3Y, this);
            bufferGraphics.drawImage( butterflyfish_r, bff4X, bff4Y, this);
            bufferGraphics.drawImage( butterflyfish_r, bff5X, bff5Y, this);
            bufferGraphics.drawImage( rockfish_r, rf1X, rf1Y, this );
            bufferGraphics.drawImage( rockfish_l, rf2X, rf2Y, this );
            bufferGraphics.drawImage( rockfish_l, rf3X, rf3Y, this );
            bufferGraphics.drawImage( rockfish_r, rf4X, rf4Y, this );
            bufferGraphics.drawImage( rockfish_r, rf5X, rf5Y, this );
            bufferGraphics.drawImage( rockfish_l, rf6X, rf6Y, this );
            
            bufferGraphics.drawImage( scoreboard, 1, 0, this );
            bufferGraphics.setFont( scorefont );
            bufferGraphics.setColor( Color.WHITE );
            bufferGraphics.drawString( "Score: " + score, 10, 20 );
            bufferGraphics.drawString( "XP points: " + xp, 10, 40 );
            g.drawImage( offscreen, 0, 0, this );
        }
        
        @Override
        public void update( Graphics g ) {
            paint(g);
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();
            t.start();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
        javax.swing.Timer t = new javax.swing.Timer( 15, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean dead = false;
                if( dead == false) {
                if( fishX < mouseX ) {
                    fishX +=3;
                    if( mainsize == 2 ) { 
                        main_fish = getImage( getDocumentBase(), "main_right1.png" );
                    } else { 
                        if ( mainsize == 4 ) {
                            main_fish = getImage( getDocumentBase(), "main_right2.png" );
                        } else { 
                            //mainsize == 6
                            main_fish = getImage( getDocumentBase(), "main_right3.png" );
                        }
                    }
                }
                if( fishX > mouseX ) {
                    fishX -= 4;
                    if( mainsize == 2 ) { 
                        main_fish = getImage( getDocumentBase(), "main_left1.png" );
                    } else { 
                        if ( mainsize == 4 ) {
                            main_fish = getImage( getDocumentBase(), "main_left2.png" );
                        } else { 
                            //mainsize == 6
                            main_fish = getImage( getDocumentBase(), "main_left3.png" );
                        }
                    }
                }
                if( fishY < mouseY ) { 
                    fishY += 4;
                }
                if( fishY > mouseY ) {
                    fishY -= 4;
                }
                repaint();
                
                if( xp >= 250 ) { 
                    mainsize = 6;
                } else {
                    if( xp >= 100 ) {
                        mainsize = 4;
                    } else {
                        mainsize = 2;
                    }
                }
                
                if( gf1X > 1500 ) {
                    gf1X = -100;
                } else {
                    gf1X += 2;
                }
                
                if( gf2X > 1500 ) {
                    gf2X = -300;
                } else {
                    gf2X += 2;
                }
                
                if( gf3X > 1500 ) {
                    gf3X = -250;
                } else {
                    gf3X += 2;
                }
                
                if( gf4X < -100 ) {
                    gf4X = 1250;
                } else {
                    gf4X -= 2;
                }
    
                if( gf5X < -150 ) {
                    gf5X = 1200;
                } else {
                    gf5X -= 2;
                }
                
                if( bf1X < -100 ) {
                    bf1X = 1200;
                } else {
                    bf1X -= 2;
                }
                
                if( bf2X < -150 ) {
                    bf2X = 1100;
                } else {
                    bf2X -= 1;
                }
                
                if( brf1X > 1600 ) {
                    brf1X = -80;
                } else {
                    brf1X += 1;
                }
                
                if( brf2X > 1700 ) {
                    brf2X = -200;
                } else {
                    brf2X += 2;
                }
                
                if( brf3X < -170 ) {
                    brf3X = 1200;
                } else {
                    brf3X -= 2;
                }
                
                if( brf4X < -100 ) {
                    brf4X = 1100;
                } else {
                    brf4X -= 2;
                }
                
                if( bff1X < -200 ) {
                    bff1X = 1200;
                } else {
                    bff1X -= 1;
                }
                
                if( bff2X < -200 ) {
                    bff2X = 1500;
                } else {
                    bff2X -= 1;
                }
                
                if( bff3X < -200 ) {
                    bff3X = 1350;
                } else {
                    bff3X -= 1;
                }
                
                if( bff4X > 1200 ) {
                    bff4X = -350;
                } else {
                    bff4X += 1;
                }
                
                if( bff5X > 1200 ) {
                    bff5X = -250;
                } else {
                    bff5X += 1;
                }
                
                if( rf1X > 1300 ) {
                    rf1X = -300;
                } else {
                    rf1X +=1;
                }
                
                if( rf2X < -300 ) {
                    rf2X = 1300;
                } else {
                    rf2X -=1;
                }
                
                if( rf3X < -300 ) {
                    rf3X = 1200;
                } else {
                    rf3X -=2;
                }
                
                if( rf4X > 1300 ) {
             
                    rf4X = -230;
                } else {
                    rf4X +=1;
                }
                
                if( rf5X > 1300 ) {
                    rf5X = -260;
                } else {
                    rf5X +=2;
                }
                
                if( rf6X < -300 ) {
                    rf6X = 1150;
                } else {
                    rf6X -=1;
                }
                
                int main_width = 29;
                int main_length = 17;
                if( mainsize == 2 ) {
                    main_width = 29;
                    main_length = 17;
                } else {
                    if( mainsize == 4 ) {
                        main_width = 46;
                        main_length = 27;
                    } else {
                        if( mainsize == 6 ) {
                            main_width = 138;
                            main_length = 81;
                        }
                    }
                } 
                
                Rectangle main_fish_rect = new Rectangle( fishX, fishY, main_width, main_length );
                
                Rectangle greenfish1_rect = new Rectangle( gf1X, gf1Y, 29, 14 );
                if ( main_fish_rect.intersects( greenfish1_rect ) ) {
                    gf1X = -300;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle greenfish2_rect = new Rectangle( gf2X, gf2Y, 29, 14 );
                if ( main_fish_rect.intersects( greenfish2_rect ) ) {
                    gf2X = -300;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle greenfish3_rect = new Rectangle( gf3X, gf3Y, 29, 14 );
                if ( main_fish_rect.intersects( greenfish3_rect ) ) {
                    gf3X = -350;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle greenfish4_rect = new Rectangle( gf4X, gf4Y, 29, 14 );
                if ( main_fish_rect.intersects( greenfish4_rect ) ) {
                    gf4X = 1200;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle greenfish5_rect = new Rectangle( gf5X, gf5Y, 29, 14 );
                if ( main_fish_rect.intersects( greenfish5_rect ) ) {
                    gf5X = 1100;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle bluefish1_rect = new Rectangle( bf1X, bf1Y, 29, 14 ); 
                if ( main_fish_rect.intersects( bluefish1_rect ) ) {
                    bf1X = 2000;
                    score += 10;
                    xp += 2;
                }      
                
                Rectangle bluefish2_rect = new Rectangle( bf2X, bf2Y, 29, 14 ); 
                if ( main_fish_rect.intersects( bluefish2_rect ) ) {
                    bf2X = 2000;
                    score += 10;
                    xp += 2;
                }
                
                Rectangle blueredfish1_rect = new Rectangle( brf1X, brf1Y, 20, 9 );
                if ( main_fish_rect.intersects( blueredfish1_rect ) ) {
                    brf1X = -150;
                    score += 5;
                    xp += 1;
                }      
    
                Rectangle blueredfish2_rect = new Rectangle( brf2X, brf2Y, 20, 9 );
                if ( main_fish_rect.intersects( blueredfish2_rect ) ) {
                    brf2X = -180;
                    score += 5;
                    xp += 1;
                }     
                
                Rectangle blueredfish3_rect = new Rectangle( brf3X, brf3Y, 20, 9 );
                if ( main_fish_rect.intersects( blueredfish3_rect ) ) {
                    brf3X = 1100;
                    score += 5;
                    xp += 1;
                } 
                
                Rectangle blueredfish4_rect = new Rectangle( brf4X, brf4Y, 20, 9 );
                if ( main_fish_rect.intersects( blueredfish4_rect ) ) {
                    brf4X = 1200;
                    score += 5;
                    xp += 1;
                } 
                
                Rectangle butterflyfish1_rect = new Rectangle( bff1X, bff1Y, 39, 36 );
                if ( mainsize > 3 ) {
                    if ( main_fish_rect.intersects( butterflyfish1_rect ) ) {
                        bff1X = 2200;
                        score += 50;
                        xp += 5;
                    }
                } else {
                    if ( main_fish_rect.intersects( butterflyfish1_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle butterflyfish2_rect = new Rectangle( bff2X, bff2Y, 39, 36 );
                if ( mainsize > 3 ) {
                    if ( main_fish_rect.intersects( butterflyfish2_rect ) ) {
                        bff2X = 1500;
                        score += 50;
                        xp += 5;
                    }
                } else {
                    if ( main_fish_rect.intersects( butterflyfish2_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle butterflyfish3_rect = new Rectangle( bff3X, bff3Y, 39, 36 );
                if ( mainsize > 3 ) {
                    if ( main_fish_rect.intersects( butterflyfish3_rect ) ) {
                        bff3X = 1100;
                        score += 50;
                        xp += 5;
                    }
                } else {
                    if ( main_fish_rect.intersects( butterflyfish3_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle butterflyfish4_rect = new Rectangle( bff4X, bff4Y, 39, 36 );
                if ( mainsize > 3 ) {
                    if ( main_fish_rect.intersects( butterflyfish4_rect ) ) {
                        bff4X = -220;
                        score += 50;
                        xp += 5;
                    }
                } else {
                    if ( main_fish_rect.intersects( butterflyfish4_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle butterflyfish5_rect = new Rectangle( bff5X, bff5Y, 39, 36 );
                if ( mainsize > 3 ) {
                    if ( main_fish_rect.intersects( butterflyfish5_rect ) ) {
                        bff5X = -190;
                        score += 50;
                        xp += 5;
                    }
                } else {
                    if ( main_fish_rect.intersects( butterflyfish5_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle rockfish1_rect = new Rectangle( rf1X, rf1Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish1_rect ) ) {
                        rf1X = -300;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish1_rect ) ) {
                        dead = true;
                    }
                }
                    
                Rectangle rockfish2_rect = new Rectangle( rf2X, rf2Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish2_rect ) ) {
                        rf2X = 1200;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish2_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle rockfish3_rect = new Rectangle( rf3X, rf3Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish3_rect ) ) {
                        rf3X = 1200;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish3_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle rockfish4_rect = new Rectangle( rf4X, rf4Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish4_rect ) ) {
                        rf4X = -300;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish4_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle rockfish5_rect = new Rectangle( rf5X, rf5Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish5_rect ) ) {
                        rf5X = -300;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish5_rect ) ) {
                        dead = true;
                    }
                }
                
                Rectangle rockfish6_rect = new Rectangle( rf6X, rf6Y, 116, 55 );
                if ( mainsize > 5 ) {
                    if ( main_fish_rect.intersects( rockfish6_rect ) ) {
                        rf6X = 1200;
                        score += 200;
                        xp += 25;
                    }
                } else {
                    if ( main_fish_rect.intersects( rockfish6_rect ) ) {
                        dead = true;
                    }
                }
                } 
                if (dead == true) {
                    main_fish = getImage( getDocumentBase(), "main_left1_dead.png" );
                    t.stop();
                    bufferGraphics.setColor( Color.CYAN );
                    bufferGraphics.drawRect( 400, 200, 100, 50);
                    repaint();
                }
            }
        });
    
    }
    but it doesnt seem to work..
    Last edited by Joey0412; 06-13-2012 at 09:18 PM.

  19. #19
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object collision

    nvm, i got it solved.
    but it won't draw the rectangle at row 581.

Similar Threads

  1. ImageView Collision
    By TP-Oreilly in forum Android
    Replies: 1
    Last Post: 01-30-2012, 03:17 AM
  2. Collision
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 12-12-2011, 03:20 AM
  3. Object and Collision [Medium Code Problem]
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-14-2011, 05:36 PM
  4. Collision
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 10:58 PM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •