Search code examples
javaimageawtcollision-detectionjava-2d

How do I do collision detection using images?


I am making a game similar to pong, where there are two paddles on either side of the screen, and the player would have to hit the ball. The problem is, how do i do collision detection with images. I know there is a simple method with rectangles. Here is my code. My window class

import javax.swing.JFrame;
import java.awt.Rectangle;
public class window{
    public static void main(String args[])
    {
        JFrame f = new JFrame();
        second s = new second();
        f.add(s);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(806,628);
        f.setResizable(false);
        f.setTitle("Pong!");
    }
}

And in this class is where I really need help! This is my second class, and I called it second (how creative).

import javax.swing.*;
import java.awt.Rectangle;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.JFrame;
public class second extends JPanel implements ActionListener, KeyListener
{
    Timer t = new Timer(5, this);
    double x1 = 20, y1 = 250, velx1 = 0, vely1 = 0;
    double x = 750, y = 250, velx = 0, vely = 0;
    double xb = 384, yb  = 290, velbx = 3.0, velby = 3.0;
    boolean collision = false;
    public second()
    {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.fill(new Rectangle2D.Double(x,y,40,40));
        super.paintComponent(g);
        ImageIcon i = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong1.png");
        ImageIcon a = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong-bg.png");
        ImageIcon b = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong1.png");
        ImageIcon c = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin/Ball.png");
        a.paintIcon(this, g, 0, 0);
        i.paintIcon(this, g, (int)x1 , (int)y1);
        b.paintIcon(this, g, (int) x, (int) y); 
        c.paintIcon(this, g, (int)xb, (int)yb);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
        if (y1<=0 || y1 >= 600 || y1 >= 500)
        {
            vely1 = -vely1;
        }
        x1 +=velx1;
        y1 += vely1;
        repaint();
        if (y<=0 || y >= 600 || y > 500)
        {
            vely = -vely;
        }
        repaint();
        x += velx;
        y += vely;
        repaint();
        if (xb<0 || xb > 800)
        {
            velbx = -velbx;
        }
        if (yb<0 || yb > 600)
        {
            velby = -velby;
        }
        xb += velbx;
        yb += velby;
        repaint();
    }
    public void up()
    {
        vely1 = -3.0;
        velx1 = 0;

    }
    public void up1()
    {
        vely = -3.0;
        velx = 0;
    }
    public void down()
    {
        vely1 = 3.0;
        velx1 = 0;
    }
    public void down1()
    {
        vely = 3.0;
        velx = 0;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_W)
        {
                up();
        }
        if(code == KeyEvent.VK_S)
        {
            down();
        }
        if (code == KeyEvent.VK_UP)
        {
            up1();
        }
        if(code == KeyEvent.VK_DOWN)
        {
            down1();
        }
    }
    @Override
    public void keyReleased(KeyEvent ee) {
        int code1 = ee.getKeyCode();
        if(code1 == KeyEvent.VK_W)
        {
            vely1 = 0;
        }
        if (code1 == KeyEvent.VK_S)
        {
            vely1 = 0;
        }
        if(code1 == KeyEvent.VK_UP)
        {
            vely = 0;
        }
        if(code1 == KeyEvent.VK_DOWN)
        {
            vely = 0;
        }
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

As you can see I have most of the pong game finished EXCEPT for the collision detection between the pong and the paddle. The most important part of the game. Thank you, Aadarsh Jha


Solution

  • There are a lot of ways of doing it. If you don't need a lot of precission, you can use bounding boxes, and with the sizes and locations of them, find out if there is a collision. You can read this: http://gpsnippets.blogspot.com.es/2008/10/bounding-box-collision-detection.html for more info.