以图形方式模拟 java 中两个粒子的物理影响

Graphically simulate physical impact of two particles in java

我想使用 Java 编写一个程序来模拟两个球之间的碰撞。

我从在线教程中复制并修改了初稿。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class grafico extends JPanel implements ActionListener {

    Timer tm = new Timer(5, this);
    int x1 = 0;
    int x2 = 0;
    int velX1 = 1;
    int velX2 = 1;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(x1, 160, 10, 10);
        g.setColor(Color.BLUE);
        g.fillRect(x2, x2, 10, 10);
        tm.start();
    }

    public void actionPerformed(ActionEvent e) {
        if (x1 < 0 || x1 > 500) {
            velX1 = -velX1;
        }
        x1 = x1 + velX1;

        if (x2 < 0 || x2 < 350) {
            velX2 = -velX2;
        }
        x2 = -x2 + velX2;
        repaint();
    }

    public static void main(String[] args) {
        grafico t = new grafico();
        JFrame jf = new JFrame();
        jf.setTitle("Tutorial");
        jf.setSize(600, 400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(t);
    }
}

在执行本程序时我不想使用在线小程序。

关于这个程序我想改变的第一件事如下。

球不会相互干扰,但我希望它们相互作用,以便在它们碰撞时改变轨迹。

我不知道如何实现这个目标。

有人可以帮助我或告诉我这是否可能吗?

物理碰撞的主要思想是:

当 x1 = x2 和 y1 = y2 时,我们可以说 object1 和 object2 在二维规划器系统中相互碰撞

但我们知道物体有尺寸,我们应该在计算中假设这一点。所以最好纠正一下我们关于两个物体碰撞的说法:

我们可以说对象 1 和对象 2 在 |x1-x2| 时相互碰撞< n 和 |y1-y2| < n 在二维规划器系统中,当 n 例如是对象宽度或高度的一半时

还在您的代码中绘制了一个正方形,g.fillRect 中使用的起点是该正方形的左上角,而不是它的中心。所以你应该在你的计算中意识到这一点。

例如,您可以在 class 中使用 checkCollision 方法来检查每次重绘时两个方块是否发生碰撞。

在下面的代码中,您可以看到我只提供了一个简单的碰撞检查,因为我只是想为您提供想法而不是完整的工作解决方案。

同样,在碰撞之后,取决于物体的质量和它们的速度以及它们之前的方向和碰撞的弹性,下一个方向和速度可以变化。所以如果你想模拟一个近似真实的碰撞,有两个参数。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class grafico extends JPanel implements ActionListener {
    Timer tm = new Timer(5, this);
    int x1 = 0;
    int x2 = 0;
    int velX1 = 1;
    int velX2 = 1;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(x1, 160, 10, 10);
        g.setColor(Color.BLUE);
        g.fillRect(x2, x2, 10, 10);     
    }

    public void actionPerformed(ActionEvent e) {
        if (x1 < 0 || x1 > 500)
            velX1 = -velX1;
        x1 = x1 + velX1;

        if (x2 < 0 || x2 > 350)
            velX2 = -velX2;
        x2 = x2 + velX2;

        if( checkCollition() ) { // only two show the Idea.
            velX2 = -velX2;      // this code is not simulating a 
            x2 = x2 + velX2;     //    collision. You should change it
                                 //    in the future.
        }
        repaint();
    }

    private boolean checkCollition() {
        return Math.abs(x1-x2) < 10 && Math.abs(160-x2) < 10;
    }

    public static void main(String[] args) {
        grafico t = new grafico();
        JFrame jf = new JFrame();
        jf.setTitle("Tutorial");
        jf.setSize(600, 400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(t);
        t.tm.start();
    }
}

在你运行上面的代码有了思路之后,你可能想把每个物体的x,y,之前的方向和速度矢量,质量等封装在一个java物体中使编码更容易。例如,您可以创建一个 class Particle 然后将 checkCollision 方法的逻辑移动到此 class:

会更加优雅
public class Particle{
    private int x;
    private int y;
    private int width;
    private int height;
    private float mass;
    //
    public Particle() {
        //...
    }

    public boolean collide(Particle other){
        return Math.abs(this.x-other.x) < width 
                && Math.abs(this.y-other.y) < height; // Just to illustrate the idea 
    }
}

然后在您的程序中您可以创建两个实例 Particle p1Particle p2 并且在您的代码中您可以简单地将 if 语句更改为:

if( p1.collide(p2) ) {
    // change the direction, velocity, ... of p1 and p2
}

希望这会有所帮助。