物理与弹跳球

时间:2015-05-18 11:33:47

标签: java swing collision-detection collision game-physics

这是提出这个问题的正确网站,因为我现在已经两次被推荐到另一个网站。

我试图用真实的物理学创造弹跳球。此刻,当球相互碰撞时,它们会以与它们相同的方向反弹回来,现在,我已经在互联网上搜索了如何做到这一点,但我只找到了关于如何检测碰撞的事情,不怎么做。我不太了解物理学,在我能够做到这一点之前,我应该先学习他们的主题吗?这是我如何想象球会在现实生活中反弹的图像。这是怎么回事?

how I think it should work http://thewombatguru.nl/Bouncing.png

另外,我的代码中有不良做法吗? (可能,但我学到了很多,我喜欢它)

这是我目前的代码:

Asteroids.java

package Asteroids;

import javax.swing.*;

public class Asteroids {

    public static void createAndShowGui() {
        GamePanel gamePanel = new GamePanel();
        JFrame frame = new JFrame("Asteroids");
        frame.getContentPane().add(gamePanel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocation(2000, 50);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

GamePanel.java

package Asteroids;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GamePanel extends JPanel implements ActionListener {

    private final int WIDTH = 1000;
    private final int HEIGHT = 1000;

    Timer animationTimer;

    ArrayList<Rock> rocks;

    public GamePanel() {
        Dimension preferredDimension = new Dimension(WIDTH, HEIGHT);
        setPreferredSize(preferredDimension);

        animationTimer = new Timer(10, this);

        setUp();
    }

    public void setUp() {

        rocks = new ArrayList<>();

        rocks.add(new Rock(475, 1000, 0, -1));
        rocks.add(new Rock(0, 500, 1, 0));
        //rocks.add(new Rock(300, 270, -2, 2));
        //rocks.add(new Rock(400, 315, -5, -1));

        animationTimer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();

        for (Rock rock : rocks) {
            for (Rock rockToCheck : rocks) {
                if (!rock.equals(rockToCheck)) {
                    rock.checkForCollisionWithRocks(rockToCheck);
                }
            }
            rock.checkForCollisionWithFrame(WIDTH, HEIGHT);
            rock.setxPos(rock.getxPos() + rock.getxVelocity());
            rock.setyPos(rock.getyPos() + rock.getyVelocity());
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();

        RenderingHints mainRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(mainRenderingHints);

        for (Rock rock : rocks) {
            rock.display(g2d);
        }

        g2d.dispose();
    }
}

Rock.java

package Asteroids;

import java.awt.*;

public class Rock {

    private int xPos;
    private int yPos;
    private int rockWidth;
    private int rockHeight;
    private int xVelocity;
    private int yVelocity;
    private int rockRadius;
    private Color rockColor;

    public Rock(int xPos, int yPos, int xVelocity, int yVelocity) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.xVelocity = xVelocity;
        this.yVelocity = yVelocity;
        rockWidth = 50;
        rockHeight = rockWidth;
        rockRadius = rockWidth / 2;
        rockColor = new Color((int) (Math.random() * 255),(int) (Math.random() * 255),(int) (Math.random() * 255));
    }

    public void setxPos(int xPos) {
        this.xPos = xPos;
    }
    public int getxPos() {
        return xPos;
    }
    public int getRockWidth() {
        return rockWidth;
    }
    public void setRockWidth(int rockWidth) {
        this.rockWidth = rockWidth;
    }
    public int getRockHeight() {
        return rockHeight;
    }
    public void setRockHeight(int rockHeight) {
        this.rockHeight = rockHeight;
    }
    public int getyPos() {
        return yPos;
    }
    public void setyPos(int yPos) {
        this.yPos = yPos;
    }
    public int getxVelocity() {
        return xVelocity;
    }
    public void setxVelocity(int xVelocity) {
        this.xVelocity = xVelocity;
    }
    public int getyVelocity() {
        return yVelocity;
    }
    public void setyVelocity(int yVelocity) {
        this.yVelocity = yVelocity;
    }
    public int getRockRadius() {
        return rockRadius;
    }
    public void setRockRadius(int rockRadius) {
        this.rockRadius = rockRadius;
    }

    public void checkForCollisionWithRocks(Rock rock) {
        int radiusOfBoth = rock.getRockRadius() + rockRadius;
        int horDistance = Math.abs((rock.getxPos() + rock.getRockRadius()) - (xPos + rockRadius));
        int verDistance = Math.abs((rock.getyPos() + rock.getRockRadius()) - (yPos + rockRadius));
        int diagDistance = (int) Math.sqrt(Math.pow(horDistance, 2) + Math.pow(verDistance, 2));

        if (diagDistance <= radiusOfBoth) {

            xVelocity = -xVelocity;
            yVelocity = -yVelocity;

            rock.setxVelocity(-rock.getxVelocity());
            rock.setyVelocity(-rock.getyVelocity());
            rock.setxPos(rock.getxPos() + rock.getxVelocity());
            rock.setyPos(rock.getyPos() + rock.getyVelocity());
        }
    }

    public void checkForCollisionWithFrame(final int WIDTH, final int HEIGHT) {
        if (xPos < 0) {
            xVelocity *= -1;
            xPos = 0;
        } else if (xPos + rockWidth > WIDTH) {
            xVelocity *= -1;
            xPos = WIDTH - rockWidth;
        }

        if (yPos < 0) {
            yVelocity *= -1;
            yPos = 0;
        } else if (yPos + rockHeight > HEIGHT) {
            yVelocity *= -1;
            yPos = HEIGHT - rockHeight;
        }
    }

    public void display(Graphics2D g2d) {
        g2d.setColor(rockColor);
        g2d.fillOval(xPos, yPos, rockWidth, rockHeight);
    }

}

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

你可以从动量守恒定律开始。当两个物体碰撞时,总动量不会改变,你可以使用this link来理解后面的计算,同时试图预测两个物体碰撞后的轨迹。

至于你的代码,你似乎错过了Rock.java中的关键字段 mass 。你需要质量来计算一个物体的动量,以后你将用它来预测物体碰撞后的轨迹。

编辑:请注意,链接中的示例在某种程度上局限于2个方向,在这两个方向上,对象相互碰撞,它们之间的距离为180 *。然而,通过将动量/速度矢量分解为4个方向(即0,90,180,270度)来概括并找到碰撞后物体的速度并不难。您必须使用向量数学来表达给定对象在4个方向上的速度。

答案 1 :(得分:1)

现实生活中的物理学是棘手的(重力,惯性等),但首先,将球互相反弹:

当两个球击中时,会有一个碰撞角。幸运的是,因为它们是圆形(假设),你可以通过找到穿过两个球的中心的线的角度来找到入射角。然后你想在垂直于那条线的1条路上发送1个球,而另一条路则发送另一条球。

有意义吗?

答案 2 :(得分:0)

在这里回答: 还有一个很好的GIF,你可以很容易地找到,如何计算新的速度;) Ball to Ball Collision - Detection and Handling