如何使用图像进行碰撞检测?

时间:2013-08-26 23:20:36

标签: java image awt collision-detection java-2d

我正在制作类似乒乓球的游戏,屏幕两侧有两个拨片,玩家必须击球。问题是,我如何与图像进行碰撞检测。我知道有一个简单的矩形方法。这是我的代码。 我的窗口类

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!");
    }
}

在这堂课上,我真的需要帮助! 这是我的第二课,我把它称为第二课(多么有创意)。

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

    }
}

正如你所看到的,我的大部分乒乓球游戏都完成了乒乓球和球拍之间的碰撞检测。游戏中最重要的部分。 谢谢, Aadarsh Jha

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。如果您不需要大量的精确度,可以使用边界框,并根据它们的大小和位置,找出是否存在碰撞。您可以阅读:http://gpsnippets.blogspot.com.es/2008/10/bounding-box-collision-detection.html了解更多信息。