以图形方式模拟 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 在 2D 规划系统中时,对象 1 和对象 2 相互碰撞

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

我们可以说对象 1 和对象 2 在 2D 规划器系统中

同样在你的代码中,你画了一个正方形,g.fillRect中使用的起点是该正方形的左上角,而不是它的中心。因此,您应该在计算中意识到这一点。

因此,例如,您可以在类中使用一个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 对象中,以便于编码。例如,您可以创建一个类Particle然后将checkCollision方法的逻辑移动到此类中会更加优雅:

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
}

希望这会有所帮助。

最新更新