Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java球到线碰撞不起作用_Java - Fatal编程技术网

java球到线碰撞不起作用

java球到线碰撞不起作用,java,Java,我正在用Java开发我的游戏项目原型,我遇到了“碰撞”这一行的问题。我试过很多不同的方法让它工作。有人能帮我吗?我只是没有想法,检查了一些像Mathworld这样的网站,但当我尝试将这些数学重新编写成代码时,它并没有真正起作用 class Ball { int x, y, radius, dx, dy; Color BallColor; public Ball(int x, int y, int radius, int dx, int dy, Color bColor

我正在用Java开发我的游戏项目原型,我遇到了“碰撞”这一行的问题。我试过很多不同的方法让它工作。有人能帮我吗?我只是没有想法,检查了一些像Mathworld这样的网站,但当我尝试将这些数学重新编写成代码时,它并没有真正起作用

class Ball {

    int x, y, radius, dx, dy;
    Color BallColor;

    public Ball(int x, int y, int radius, int dx, int dy, Color bColor) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.dx = dx;
        this.dy = dy;
        BallColor = bColor;
    }
}

public class ball_1 extends Applet implements Runnable, MouseListener, MouseMotionListener {

    Ball ball1, ball2;
    int x1, x2, y1, y2,r1;
    int a, b;
    String msg = "";
    double c,d,e;


    @Override
    public void init() {
        this.setSize(800, 800);
        ball1 = new Ball(400, 70, 20, 4, 1, Color.green);
        ball2 = new Ball(40, 70, 20, 4, 1, Color.black);
        Thread t = new Thread(this);
        t.start();
        addMouseListener(this);
        addMouseMotionListener(this);
        Point p1 = new Point(x1,y1);
        Point p2 = new Point(x2,y2);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(ball1.BallColor);
        g.fillOval(ball1.x, ball1.y, ball1.radius, ball1.radius);
        g.setColor(ball2.BallColor);
        g.fillOval(ball2.x, ball2.y, ball2.radius, ball2.radius);
        g.drawLine(x1, y1, x2, y2);
        g.drawString(msg, 80, 80);
    }

    @Override
    public void run() {
        boolean run = true;
        while (run) {
            try {

                displacementOperation(ball2);
                lineCollision(ball1);
                ballCollision(ball1);
                ballCollision(ball2);
                Thread.sleep(40);
                repaint();
            } catch (Exception e) {
            }
            if (ball1.x >= 780 || ball1.y <= 0) {
                run = false;
                msg = "game over";
            }
            if (ball1.y >= 780 || ball1.x <= 0) {
                run = false;
                msg = "game over";
            }
        }
    }

    public void displacementOperation(Ball ball) {
        if (ball.y >= 780 || ball.y <= 0) {
            ball.dy = -ball.dy;
        }
        if (ball.x >= 780 || ball.x <= 0) {
            ball.dx = -ball.dx;
        }
        ball.y = ball.y - ball.dy;
        ball.x = ball.x - ball.dx;

    }

    public void lineCollision(Ball ball) {
        d = ((y2-y1)*(ball.radius) -(x2-x1)*(ball.radius) +x2*y1 -y2*x1)/((y2-y1)*(y2-y1))/2 + ((x2-x1)*(x2-x1))/2 ;
        if (ball.radius <= d){
            ball.dy = -ball.dy;
            ball.dx = -ball.dx;

        }

        ball.y = ball.y - ball.dy;
        ball.x = ball.x - ball.dx;


    }

    public void ballCollision(Ball ball) {
        a = (ball2.y - ball1.y) * (ball2.y - ball1.y) + (ball2.x - ball1.x) * (ball2.x - ball1.x);
        b = ((ball1.radius + 10) + (ball2.radius + 10)) / 2 * ((ball1.radius + 10) + (ball2.radius + 10)) / 2;
        if (a <= b) {
            ball.dy = -ball.dy;
            ball.dx = -ball.dx;

        }

        ball.y = ball.y - ball.dy;
        ball.x = ball.x - ball.dx;

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {

        x2 = e.getX();
        y2 = e.getY();
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
班级舞会{
int x,y,半径,dx,dy;
彩色球;
公共球(整数x,整数y,整数半径,整数dx,整数dy,彩色b颜色){
这个.x=x;
这个。y=y;
这个半径=半径;
this.dx=dx;
this.dy=dy;
BallColor=b颜色;
}
}
公共类ball_1扩展小程序实现Runnable、MouseListener、MouseMotionListener{
球1,球2;
int-x1,x2,y1,y2,r1;
INTA,b;
字符串msg=“”;
双c,d,e;
@凌驾
公共void init(){
这个。设置大小(800800);
ball1=新球(400,70,20,4,1,颜色为绿色);
ball2=新球(40,70,20,4,1,颜色为黑色);
螺纹t=新螺纹(此);
t、 start();
addMouseListener(这个);
addMouseMotionListener(此);
点p1=新点(x1,y1);
点p2=新点(x2,y2);
}
@凌驾
公共空间涂料(图g){
g、 setColor(ball1.BallColor);
g、 圆角椭圆(ball1.x,ball1.y,ball1.radius,ball1.radius);
g、 setColor(ball2.BallColor);
g、 圆角椭圆(ball2.x,ball2.y,ball2.radius,ball2.radius);
g、 抽绳(x1、y1、x2、y2);
g、 抽绳(味精,80,80);
}
@凌驾
公开募捐{
布尔运行=真;
while(运行){
试一试{
置换手术(ball2);
线碰撞(ball1);
ballCollision(ball1);
ballCollision(ball2);
睡眠(40);
重新油漆();
}捕获(例外e){
}

如果(ball1.x>=780 | | | ball1.y=780 | | ball.x=780 | | ball.y=780 | | | ball.x代码有什么问题?什么特别不起作用?您使用什么调试器来查看计算的值?很抱歉没有像我应该说的那样清楚。这段代码主要像在任何点都不检测线,如果它主要导致球卡在原地。它会被删除。)它正在运行,没有显示任何错误。我使用NetBeans。错误的是lineCollision中的数学部分,我相信是它造成的。对于最后一个问题,我在编码方面相当新手,所以我这里没有你问题的答案。调试器允许你单步执行一个程序并查看计算出的值。这就是你需要看到的,以便理解y线碰撞计算不正确。只是使用了调试器,但它没有显示任何值。老实说,这意味着我做错了什么。idk的问题是如何在代码中设置为“嘿,球,你碰到了线,改变了运动方向”.现在我知道我应该在提出问题之前对每件重要的事情进行注释,以使其更清晰。我发现了这一点,并尝试重新编写代码,以防某个交叉点弹离直线。但仍然不确定如何在代码中执行此操作。