Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision Problem....need help please  (Read 566 times)
0 Members and 1 Guest are viewing this topic.
Offline alphasil

Junior Newbie





« Posted 2013-01-10 23:32:24 »

Hi

I have a simple mini game who has a cannon, that cannon shot bullets (Rectangle) to enemies stored in a arrayList. I must code into Bullet class the collision method but i have no idea.
I have search examples but all of them has two rectangles...
This is my Bullet class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;



public class Balas implements Runnable
{
    int x, y;
    int balaX, balaY;
    Rectangle balas, r1;
    Boolean colisao= false;
    private Jogo jogo;
    private ArrayList<Alvos> bugs;
   
    public Balas()
    {
       x= 305;
       y= 355;
    }
    public void desenho(Graphics g)
    {
       //Bullet color
      g.setColor(Color.pink);
       //x and y position and height and width of bullet
      g.fillRect(balas.x, balas.y, balas.width, balas.height);
    }
   
    public int getX()
    {
        return x;
    }
   
    public int getY()
    {
        return y;
    }
   
    public int getBalaX()
    {
        return balaX;
    }
   
    public int getBalaY()
    {
        return balaY;
    }
    @Override
    public void run()
    {
       try
       {
          while(true)
          {
                    //colisao();
                   Thread.sleep(5);
          }
       }
       catch(Exception e){System.err.println(e.getMessage());
    }
    }
}


This is my cannon class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

public class Canhao implements Runnable
{
   int x, y, DirecaoX;
   boolean disparar, tiro = false;
   static Balas bl = new Balas();
        ArrayList<Alvos> bugs;
        ArrayList<Rectangle> bal = new ArrayList<>();
        public Canhao()
   {
            x= 350;
            y= 355;
   }
   
          public void desenho(Graphics g)
   {
      g.setColor(Color.BLACK);
      g.fillRect(x, y, 40, 10);
      g.fillRect(x+18, y-7, 4, 7);
      if(tiro)
      {
         bl.desenho(g);
      }
   }
   
   public void movimento()
   {
            x += DirecaoX;
            if(x<=0)
            {
                x=0;
            }
            if(x>=555)
            {
                x=555;
            }
   }
   
   public void setDirecaoX(int xdir)
   {
      DirecaoX= xdir;
   }
   
    public void keyPressed(KeyEvent e){
           int keyCode = e.getKeyCode();
           if(keyCode == KeyEvent.VK_LEFT){
              setDirecaoX(-1);
           }
           if(keyCode == KeyEvent.VK_RIGHT)
                {
               setDirecaoX(1);
           }
           if(keyCode == KeyEvent.VK_SPACE)
           {
              if(bl.balas==null)
                        {
                        disparar=true;
                        }
              if(disparar)
              {
                 bl.balaY= y-7;
                 bl.balaX= x+18;
                 bl.balas= new Rectangle(bl.balaX, bl.balaY, 15, 10);
                 tiro = true;
              }
           }
       }
             
       public void keyReleased(KeyEvent e){
           int keyCode = e.getKeyCode();
           if(keyCode == KeyEvent.VK_LEFT)
                {
               setDirecaoX(0);
           }
           if(keyCode == KeyEvent.VK_RIGHT)
                {
               setDirecaoX(0);
           }
           if(keyCode == KeyEvent.VK_SPACE)
                {
              disparar=false;
              if(bl.balas.y <=-5)
                        {
                 bl.balas=new Rectangle(0,0,0,0);
                 tiro = false;
                 disparar=true;
              }
           }
       }
   
     
   public void atirar()
   {
      if(tiro)
      {
         bl.balas.y--;
      }
   }
       
       
       public void haColisao()
       {
           
       }
         
    @Override
   public void run()
   {
      Thread Balas = new Thread(bl);
      Balas.start();
      try
      {
         while(true)
         {
            atirar();
            movimento();
                                //haColisao(bugs, bl);
           Thread.sleep(5);
         }
      }
      catch(Exception e){System.err.println(e.getMessage());}
   }
}

My enemies class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package canvasgmc;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;

public class Alvos extends Objetos
{
    private Image img;
    static Balas bl = new Balas();

   
   public Alvos(int posX, int posY, int altura, int largura, String jog)
   {
            super(posX, posY, altura, largura);
            this.img=Toolkit.getDefaultToolkit().getImage(jog);
   }

    @Override
   void desenho(Graphics g)
   {
      g.drawImage(img, getPosX(), getPosY(), getAltura(), getLargura(), null);
        }
}


The arrayList is created in the main class
1  
2  
3  
4  
5  
private ArrayList<Alvos> bugs = new ArrayList<>();

//constructor
bugs.add(new Alvos(50, 50 , 70, 70,"imagens/bug.gif"));
bugs.add(new Alvos(145, 50 , 70, 70,"imagens/bug1.gif"));


Can someone show me how can i achieve the collision and remove enemies from the arrayList?

Thanks
Offline alphasil

Junior Newbie





« Reply #1 - Posted 2013-01-11 00:38:49 »

None?

Please...is urgent.

Thanks
Offline tyeeeee1
« Reply #2 - Posted 2013-01-11 01:20:30 »

From what I've seen and have been told you just need to do an intersects() check between the cannonball and the enemy. I don't know much more than that, sorry!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-01-11 01:26:10 »

This type of basic collision should be very, very, simple...
1  
2  
3  
4  
5  
for(int i = 0; i < enemies.size(); i++)
if(bullet.bounds.intersects(enemies.get(i).bounds)){
enemies.remove(i);

}

Obviously this is assuming you have rectangles for the enemy and the bullet, also remember to update the bounds of the rectangles.

this method may not work if your bullets are moving really quickly...
Online SHC
« Reply #4 - Posted 2013-01-11 05:24:36 »

See this base layout

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
Rectangle bounds = new Rectangle(x, y, w, h); // You may init this in a constructor

public void moveTo(int x, int y){
    bounds.x = x;
    bounds.y = y;
}

public Rectangle getBounds(){
    return bounds;
}

public boolean isCollidingWith(GameObject o){
    return bounds.intersects(o.getBounds());
}

Now use

1  
obj1.isCollidingWith(obj2);

Offline alphasil

Junior Newbie





« Reply #5 - Posted 2013-01-11 11:33:27 »

See this base layout

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
Rectangle bounds = new Rectangle(x, y, w, h); // You may init this in a constructor

public void moveTo(int x, int y){
    bounds.x = x;
    bounds.y = y;
}

public Rectangle getBounds(){
    return bounds;
}

public boolean isCollidingWith(GameObject o){
    return bounds.intersects(o.getBounds());
}

Now use

1  
obj1.isCollidingWith(obj2);


Hi

Thanks for your help...
I have a problem, the arrayList is defined in main class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class Jogo extends JFrame
{
    private static final long serialVersionUID = 1L;
    private Image dbImage;
    private Graphics dbg;
    private ArrayList<Alvos> bugs = new ArrayList<>(); //my arrayList
   static Balas balas;
    static Canhao s1 = new Canhao();
    static Objetos obj;
    Rectangle bounds;
           
    public Jogo()
    {
        setSize(600,400);
        setTitle("Trabalho GMC 2012/2013");
        setBackground(Color.WHITE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        addKeyListener(new AL());
       
          //Menu e Submenu
       JMenuBar menubar = new JMenuBar();
        JMenu fic = new JMenu("Ficheiro");
        JMenuItem abrir = new JMenuItem("Abrir");
        JMenuItem novo = new JMenuItem("Novo");
        JMenuItem sair = new JMenuItem("Sair");
        fic.add(abrir);
        fic.add(novo);
        fic.add(sair);
        //Adiciona menuBar a fic
       menubar.add(fic);
        //Define o Menu
       setJMenuBar(menubar);
        //bugs
       bugs.add(new Alvos(50, 50 , 70, 70,"imagens/bug.gif"));
   bugs.add(new Alvos(145, 50 , 70, 70,"imagens/bug1.gif"));
   bugs.add(new Alvos(225, 50 , 70, 70,"imagens/bug.gif"));
        bugs.add(new Alvos(305, 50 , 70, 70,"imagens/bug1.gif"));
   bugs.add(new Alvos(385, 50 , 70, 70,"imagens/bug.gif"));
        bugs.add(new Alvos(480, 50 , 70, 70,"imagens/bug.gif"));
   bugs.add(new Alvos(60, 100 , 50, 50,"imagens/bug1.gif"));
        bugs.add(new Alvos(155, 100 , 50, 50,"imagens/bug.gif"));
   bugs.add(new Alvos(235, 100 , 50, 50,"imagens/bug1.gif"));
   bugs.add(new Alvos(315, 100 , 50, 50,"imagens/bug.gif"));
   bugs.add(new Alvos(395, 100 , 50, 50,"imagens/bug1.gif"));
   bugs.add(new Alvos(490, 100 , 50, 50,"imagens/bug.gif"));
       
        //posicao dos alvos
       bounds = new Rectangle();// Here a put the values?? Do i have to get the methos getPosX(), getPosY()?
   }
     public Rectangle getBounds()
    {
        return bounds;
    }
     
    @Override
    public void paint(Graphics g)
    {
        //super.paint(g);
       dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);  
    }
   
    public void paintComponent(Graphics g)
    {
        super.paint(g);
        //Desenha o objeto canhoa
       s1.desenho(g);
        repaint();
        //Desenha os bugs
       for(Alvos av: bugs)
        {
            av.desenho(g);
        }
    }
   
    public void getTamanho()
    {
        bugs.size();
    }

   public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e){
            s1.keyPressed(e);
        }
        @Override
        public void keyReleased(KeyEvent e){
            s1.keyReleased(e);
        }
    }
   
    public static void main(String[] args) {
        new Jogo();
        //Threads
       Thread canhao = new Thread(s1);
        canhao.start();
    }
}


Thanks
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (25 views)
2014-04-14 17:35:47

Porlus (42 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (223 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!