椭圆和直线之间的碰撞检测

时间:2018-11-29 02:46:32

标签: graphics collision-detection

有什么方法可以检测Java中的椭圆和直线之间的碰撞。我正在制作一个游戏,其中用户通过“洞”操纵火箭,其中“洞”由线条组成,我需要某种方式来检测碰撞。代码在下面

@Override //draws all the lines and ship
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D) g;
    Rectangle r=new Rectangle();
    g2.setStroke(new BasicStroke(3));
    g2.drawLine(0, getHeight()-(getHeight()/6), getWidth()/6, getHeight()-(getHeight()/6));
    g2.drawLine(getWidth()/6, getHeight()-(getHeight()/6), getWidth()/6, getHeight()-getHeight()/4);
    g2.drawLine(getWidth()/6, getHeight()-getHeight()/4, getWidth()/6+getWidth()/10, getHeight()-getHeight()/4);
    g2.drawLine(getWidth()/6+getWidth()/10, getHeight()-getHeight()/4, getWidth()/6+getWidth()/10, getHeight()-getHeight()/12);
    g2.drawLine(getWidth()/6+getWidth()/10, getHeight()-getHeight()/12, getWidth()/2, getHeight()-getHeight()/12);
    g2.drawLine(getWidth()/2, getHeight()-getHeight()/12, getWidth()/2, getHeight()-getHeight()/4);
    g2.drawLine(getWidth()/2, getHeight()-getHeight()/4, getWidth()/3+getWidth()/15, getHeight()-getHeight()/4);
    g2.drawLine(getWidth()/3+getWidth()/15, getHeight()-getHeight()/4, getWidth()/3+getWidth()/15, getHeight()-getHeight()/3);
    g2.drawLine(getWidth()/3+getWidth()/15, getHeight()-getHeight()/3, getWidth()/2+getWidth()/7, getHeight()-getHeight()/3);
    g2.drawLine(getWidth()/2+getWidth()/7, getHeight()-getHeight()/3, getWidth()/2+getWidth()/7, getHeight()-getHeight()/6);
    g2.drawLine(getWidth()/2+getWidth()/7, getHeight()-getHeight()/6, getWidth()-getWidth()/4 , getHeight()-getHeight()/6);
    g2.drawLine(getWidth()-getWidth()/4, getHeight()-getHeight()/6, getWidth()-getWidth()/4, getHeight()-getHeight()/4);
    g2.drawLine(getWidth()-getWidth()/4, getHeight()-getHeight()/4, getWidth()-getWidth()/7,  getHeight()-getHeight()/4);
    g2.drawLine(getWidth()-getWidth()/7,  getHeight()-getHeight()/4, getWidth()-getWidth()/7, getHeight()-getHeight()/8);
    g2.drawLine(getWidth()-getWidth()/7, getHeight()-getHeight()/8, getWidth(), getHeight()-getHeight()/8);
    g2.setColor(Color.YELLOW);
    g2.fillRect(getWidth()-getWidth()/7, getHeight()-getHeight()/8, getWidth()/7, 20);
    g2.rotate(Math.toRadians(theta),rocketx+10, rockety+20);
    g2.setColor(Color.orange);
    g2.fillOval(rocketx, rockety, 20, 40);//draws ship
}

public void initialRocket(int x, int y) {//inital position of ship
    rocketx=x/12-10;
    rockety=y-(y/4)-20;
}

0 个答案:

没有答案