【问题标题】:Breakout game, rectagle collision detection突围游戏,矩形碰撞检测
【发布时间】:2014-04-01 22:46:02
【问题描述】:

我正在尝试制作突围游戏的副本,但在检查两个物体(球和桨)是否相交时遇到问题。

我现在有这种碰撞检测方法:

public static void handleCollisions() {

    System.out.println(ball.getBounds());        // just to check that they                  
    System.out.println(paddle.getBounds());      //are there and moving correct

    if (ball.getBounds().intersects(paddle.getBounds())) {
        System.out.println("collision");
    }
}

但即使矩形肯定在某些点相交,该方法也不会检测到任何东西。我想我在某些课程上做错了。 以下是主要课程:

package assignment1;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;
import java.util.Random; 



public class Breakout extends TimerTask implements KeyListener {            

public static Vector bricks;
public static Ball ball;
public static Paddle paddle;
public static PaintingPanel panel;
public static boolean gameRunning;
public static Breakout game;

public Breakout()
{

}


public static void setupGame()
{
    Paddle paddle = new Paddle(350, 790, 100, 10);  //initial paddle creation                           
    Ball ball = new Ball(393, 790, 7);

    showGUI();
}

public static void beginGame()
{
    gameRunning = true;  

}

public void run()
{
    if(gameRunning == true)
    {


        Ball.move();
        Paddle.move();
        handleCollisions();




        if(Ball.x < 0 || Ball.x > 800 || Ball.y < 0 || Ball.y > 790){
            gameRunning = false;
            System.out.println("Game over");    
        }


    }


}

public static void stopGame()
{   

}



public static void handleCollisions()
{

    System.out.println(ball.getBounds());
    System.out.println(paddle.getBounds());

    if (ball.getBounds().intersects(paddle.getBounds()))
        {
    System.out.println("Yay");
        }

}


public static void main(String[] args)
{   
    Timer t = new Timer();  
    t.schedule(new Breakout(), 0, 40);

    panel = new PaintingPanel(PaintingPanel.contents);
    setupGame();
    beginGame();





    while (gameRunning == true)
    {
            panel.repaint();
    }       

    // TODO a lot
}

private static void showGUI()
{

    JFrame frame = new JFrame("Breakout");  
    game = new Breakout();                   

    frame.addKeyListener(game);

    frame.add(panel);

    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);                          //  create game window
    frame.setVisible(true);                                 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   


}


public void keyPressed(KeyEvent e) {
    Paddle.keyPressed(e);   
}


public void keyReleased(KeyEvent e) {
    Paddle.keyReleased(e);
}


public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

}

package assignment1;

import java.awt.*;
import java.util.Vector;
import javax.swing.*;

public class PaintingPanel extends JPanel{





public static Vector contents;


public PaintingPanel(Vector contents)
{   
    contents = new Vector();
    contents.addElement(Breakout.paddle);
    contents.addElement(Breakout.ball);



    System.out.println(contents);

}

public void paintComponent(Graphics g)
{

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setBackground(Color.GREEN);
    g2d.drawRect(2, 2, 800, 800);
    Breakout.paddle.paintThis(g2d);
    Breakout.ball.paintThis(g2d);       
}

}

package assignment1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Paddle {



public static int PADDLE_SIZE = 100;
public static int PADDLE_THICKNESS = 10;
public static int centreX = 400;                                // X Centre of the paddle
public static int centreY = 795;                                // Y Centre of the paddle
public static int paddleX = centreX - (PADDLE_SIZE/2);          // Top left X coordinate
public static int paddleY = centreY -(PADDLE_THICKNESS/2);      // Top left Y coordinate    
public static int dirX;


public Paddle(int paddleX, int paddleY, int PADDLE_SIZE, int PADDLE_THICKNESS)
{
    this.paddleX = paddleX;
    this.paddleY = paddleY;
    this.PADDLE_SIZE = PADDLE_SIZE;
    this.PADDLE_THICKNESS = PADDLE_THICKNESS;

    System.out.println("Paddle created at " + paddleX + ", " + paddleY + ", " + PADDLE_SIZE + ", " + PADDLE_THICKNESS);


}

public static void paintThis(Graphics g)
{
    g.setColor(Color.BLACK);
    g.fillRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);
    g.drawRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);

}

public static Rectangle getBounds()
{
    return new Rectangle(paddleX, paddleX, PADDLE_SIZE, PADDLE_THICKNESS);

}

public static void move()
{
    paddleX = paddleX + dirX;
}

public static void keyPressed(KeyEvent e) 
{

    if (e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        dirX = -10;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        dirX = 10;
    }
    else {}
}

public static void keyReleased(KeyEvent e) 
{
    dirX = 0;
}

}

package assignment1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class Ball {

public static int x;
public static int y;
public static int radius;
public final static Color colour = Color.RED;
public static double dirX;
public static double dirY;
public static int speed = 0;


public Ball(int x, int y, int radius)
{       
    this.x = x;
    this.y = y;
    this.radius = radius;
    dirX = 0;
    dirY = -Math.sqrt(1 - dirX*dirX);
    System.out.println("Ball created at " + x + ", " + y + ", " + dirX + ", " + dirY);
}


public static void paintThis(Graphics g)
{
    g.setColor(colour);
    int width = radius*2;
    g.fillOval(x, y, width, width);
    g.drawOval(x, y, width, width);
}


public static Rectangle getBounds()
{
    return new Rectangle(x, y, radius*2, radius*2);                     
}



public static void move()
{
    x = (int) (x-dirX*speed);           //check for inaccuracy
    y = (int) (y-dirY*speed);
    System.out.println("Moved, New X = " + x + ", new Y = "+ y);
}




public void reflect(boolean horizontal, boolean vertical)
{
    if (vertical == true)
    {
        dirX = 0-dirX;
    }
    if (horizontal == true)
    {
        dirY = 0-dirY;
    }
}


public boolean intersects(Paddle paddle) {
    if (getBounds().intersects(Paddle.getBounds())){
        return true;
    }
    else return false;
}

}

https://dl.dropboxusercontent.com/u/65678182/assignment1.rar

【问题讨论】:

  • 发布getBounds()intersects()的代码。
  • 我确实在此处发布了整个代码的链接,所以现在您可以全部查看
  • 我看到@yannis 出于某种原因删除了您的链接。请原谅我,但当我不确定文件的来源时,我对下载和打开文件有点担心。此外,当问题只是一小部分时,您不应该让我们浏览您的所有代码。获取相关的部分,并在此处发布。
  • 1) 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。 2) 请参阅this example 了解使用 Java-2D API 完成繁重工作的 MVCE。

标签: java swing graphics collision-detection


【解决方案1】:

Paddle 类中的getBounds() 方法似乎是问题所在。您将返回一个左上坐标为paddleXpaddleX 的矩形。我想你的意思是 return new Rectangle(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS); 而是。

【讨论】:

  • 天啊,我已经为这样一个愚蠢的错误坐了好几个小时......谢谢你:D
  • 没问题,很高兴能帮上忙 :)
  • 关于如何确定球将击中对象的矩形的哪一侧的任何标题?显然不需要与桨的交叉点,但是对于墙壁 + 砖块,我需要知道它们击中哪一侧,所以我以正确的方式反射球。似乎还没有找到一个很好的教程:P
  • 您已经知道球的位置、墙壁的坐标和积木的位置。您可以通过查看球的 x 坐标是否达到 0 或板宽来确定球是否撞到了侧壁。要确定它是否已经碰到天花板,然后检查它的 y 坐标是否到达板的顶部。对于积木,您可以使用 intersect 方法来确定球是否与积木相交。在上述任何情况下,如果球撞到任何东西,那么只需通过反转位移量的符号来反转球的运动方向。
  • 但是仅仅反转运动不会真的有效吗?如果球向东北方向移动,然后撞到砖的下面,你只想改变垂直方向,让它向东南方向移动,但如果它撞到砖的侧面,你只想恢复水平运动,使其继续向西北方向移动。如果你只是反转所有的运动方向,球会不会完全按照原来的方式反弹回来?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
相关资源
最近更新 更多