Results 1 to 2 of 2
  1. #1
    Pneumokok is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Collision detect - intersects() is not working

    Hey everyone!

    At first, I have to emphasize that I am not native English and apologize for possible language mistakes. I hope I won't make it too much.

    What is the problem? I try to detect collision with intersect() function and it didn't work:
    Java Code:
    public boolean playerIsDefeated(){
        /*   int xColCord;
           int yUpColCord;
           int yDownColCord;*/
           for (int i=0; i<actualEnemiesNumber; i++){
             /*  xColCord=(int) enemiesList.get(i).getMinX();
               yUpColCord=(int) enemiesList.get(i).getMaxY();
               yDownColCord=(int) enemiesList.get(i).getMinY();*/
               if ( littleRect.getBounds().intersects(enemiesList.get(i).getBounds())  ){
                   return true;
               }
           }
           return false;
       }
    I don't have any slightest idea why. I'll paste all codes from project here, maybe some good guy would compile it and helps me to find the solution

    Main class, Gierrka:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gierrka;
    
    import java.awt.EventQueue;
    
    /**
     *
     * @author Abc
     */
    public class Gierrka {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MyFrame frame;
                    frame = new MyFrame(800,720);
                }
            });
        }
    }
    Class MyFrame:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gierrka;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    
    /**
     *
     * @author Abc
     */
    class MyFrame extends JFrame implements KeyListener{
        private int width, height;
        private MyPanel panel;
        private final int interval;
        private Timer timer;
     
        public MyFrame(int widthT, int heightT) {
            super("Gierrka!");
            width=widthT;
            height=heightT;
            interval=50;
            timer =  new Timer(interval, event);
            timer.start();
            setLayout( new BorderLayout(5,1));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize( new Dimension(width, height));
            setVisible(true);
            setLocation(0, 0);
            setBackground(Color.white);
            addKeyListener(this);
            double widthD=0.75*width;
            double heightD=0.75*height;
            panel = new MyPanel((int)widthD,(int)heightD);
            add(panel, BorderLayout.CENTER);
        }
        
        ActionListener event = new ActionListener(){
    
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.moveEnemies();
                if (panel.playerIsDefeated()){
                    timer.stop();
                }
                repaint();
            }        
        };
        
       
       
    
      
    
        @Override
        public void keyTyped(KeyEvent e) {
            
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode=e.getKeyCode();
            switch (keyCode){ 
                    case KeyEvent.VK_UP:
                        panel.moveNorth();
                        break;
                 /*  case KeyEvent.VK_LEFT:
                        panel.moveWest();
                        break;
                    case KeyEvent.VK_RIGHT:
                        panel.moveEast();
                        break; */
                    case KeyEvent.VK_DOWN:
                        panel.moveSouth();
                        break;
            }
    
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            
        }
        
    }
    Class MyPanel (there is a problem with intersects() ):
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gierrka;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    /**
     *
     * @author Abc
     */
    class MyPanel extends JPanel{
    
        private final int step;
        private final int multiplier;
        private final int interval;
        private final int enemiesNumber;
        
        private int width, height, xCoord, yCoord;
        private int widthLit, heightLit, xCoordLit, yCoordLit;
        private int actualEnemiesNumber;
        private int xCoordEnemy, yCoordEnemy;
        private int defeatedEnemies;
        
        
        private List<Rectangle2D> enemiesList;
        private Rectangle2D rect2d;
        private Rectangle2D littleRect;
        private Random ranGenerator;
        private Timer timer;
    
        public MyPanel(int widthT, int heightT) {
            super();
            interval=2000;
            timer =  new Timer(interval, event);
            timer.start();
            multiplier=24;
            step=5;
            enemiesNumber=3;
            actualEnemiesNumber=1;
            defeatedEnemies=0;
            width=widthT;
            height=heightT;
            widthLit=widthT/32;
            heightLit=heightT/32;
            xCoord=(int)(width/16);
            yCoord=(int) (height/16);
            xCoordLit=(int)(width/16)+3;
            yCoordLit=(int)(height/16)+3;
            setSize(new Dimension(width, height));
            xCoordEnemy = xCoord+(widthLit*multiplier);
            setForeground(Color.red);
            rect2d = new Rectangle2D.Double(xCoord,yCoord,width*0.75,height*0.75);
            littleRect = new Rectangle2D.Double(xCoordLit,yCoordLit,widthLit,heightLit);
            enemiesList = new ArrayList(enemiesNumber);
            ranGenerator = new Random();
            yCoordEnemy = ((int)rect2d.getY()) + ranGenerator.nextInt((heightLit*multiplier)+step);
            Rectangle2D rect = new Rectangle2D.Double(xCoordEnemy,yCoordEnemy,widthLit, heightLit);
            enemiesList.add(rect);
    
        }
        
        public void moveNorth(){
            if (yCoordLit>yCoord){
                yCoordLit-=step;
            }
        }  /* 
       public void moveWest(){
            if (xCoordLit>xCoord){
                xCoordLit-=step;
            }
        }  
        public void moveEast(){
            if(xCoordLit<(xCoord+(widthLit*11))){
                xCoordLit+=step;
            }
        }*/
       public void moveSouth(){
            if (yCoordLit<(yCoord+(heightLit*multiplier)+step)){
                yCoordLit+=step;
            }
        } 
        
       public void moveEnemies(){
            int enemiesNumberT = enemiesList.size();
            //xCoordEnemy-=3;
            for (int i=0; i<enemiesNumberT; i++){
                if ((int)(enemiesList.get(i).getX()-(step/2))>xCoord){
                    enemiesList.get(i).setRect((int)(enemiesList.get(i).getX()-(step/2)), (int)enemiesList.get(i).getY(), widthLit, heightLit);
                }
    
            }
            destroyEnemyIfDefeated();
        }
       
       private void addEnemy(){
           if (actualEnemiesNumber<enemiesNumber){
               yCoordEnemy = ((int)rect2d.getY()) + ranGenerator.nextInt((heightLit*multiplier)+step);
               Rectangle2D rect = new Rectangle2D.Double(xCoordEnemy,yCoordEnemy,widthLit, heightLit);
               enemiesList.add(rect);
               actualEnemiesNumber++;
           }
       } 
       private void destroyEnemyIfDefeated(){
           for (int i=0; i<actualEnemiesNumber; i++){
               if ((enemiesList.get(i).getX()-step/2)<xCoord+3){
                   enemiesList.remove(i);
                   //System.out.println("rozmiar:" + enemiesList.size());
                   actualEnemiesNumber--;
                   defeatedEnemies++;
               }
           }
       }
       public boolean playerIsDefeated(){
        /*   int xColCord;
           int yUpColCord;
           int yDownColCord;*/
           for (int i=0; i<actualEnemiesNumber; i++){
             /*  xColCord=(int) enemiesList.get(i).getMinX();
               yUpColCord=(int) enemiesList.get(i).getMaxY();
               yDownColCord=(int) enemiesList.get(i).getMinY();*/
               if ( littleRect.getBounds().intersects(enemiesList.get(i).getBounds())  ){
                   return true;
               }
           }
           return false;
       }
        
        ActionListener event = new ActionListener(){
    
            @Override
            public void actionPerformed(ActionEvent e) {
                addEnemy();
                if (playerIsDefeated()){
                    timer.stop();
                }
            }        
        };
        
        
        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
            g2D.draw(rect2d);
            g2D.fillRect(xCoordLit,yCoordLit,(int)(widthLit)+1,(int)(heightLit)+1);
            for (int i=0; i<actualEnemiesNumber; i++){
                g2D.fillRect((int)enemiesList.get(i).getX(), (int)enemiesList.get(i).getY(), widthLit, heightLit);
            }
                
        }
    }
    I would be happy if anybody would like to help me.

    P.S.
    I don't know why some parts of code are blue, in NetBeans 7.2.1. it looks like normal code....
    Last edited by DarrylBurke; 02-28-2013 at 04:50 AM. Reason: Marked SOLVED

  2. #2
    Pneumokok is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Collision detect - intersects() is not working

    I forget about updating ,,littleRect" rectangle's coordinates (with setRect function). Problem solved :)

Similar Threads

  1. Collision Detection HOW!? Im using intersects(Rectangle)
    By Wesley.laferriere in forum New To Java
    Replies: 5
    Last Post: 11-22-2012, 05:57 AM
  2. Replies: 0
    Last Post: 10-14-2012, 05:01 PM
  3. Replies: 0
    Last Post: 10-15-2011, 11:56 PM
  4. help with intersects() and contains()
    By Grid_iso in forum New To Java
    Replies: 8
    Last Post: 05-28-2011, 01:54 PM
  5. Why is my collision not working?
    By CNew in forum New To Java
    Replies: 8
    Last Post: 12-06-2010, 10:17 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
  •