(JAVA)移动物体之间的弹性碰撞效应

huangapple go评论61阅读模式
英文:

(JAVA) elastic collision effect between moving objects

问题

在下面的代码中,我成功使这两个矩形在JPanel边界处弹跳,但是当它们发生碰撞时(弹性碰撞),我无法使它们弹跳。我认为在actionPerformed方法中存在问题(如果我错了,请纠正我)。我应该在我的代码中修复什么?

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

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

public class box {
    public static void main(String args[]) {
        objectFrame frame = new objectFrame();
    }
}

class objectFrame extends JFrame {
    objectFrame() {
        objectPanel panel = new objectPanel();
        this.setSize(500, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.add(panel);
    }
}

class objectPanel extends JPanel implements ActionListener {
    private Timer t;
    private int x, velx, x2, velx2;
    private Rectangle r1, r2;

    objectPanel() {
        t = new Timer(5, this);
        x = 100;
        x2 = 400;
        velx = 2;
        velx2 = 2;
        r1 = new Rectangle(x, 10, 50, 30);
        r2 = new Rectangle(x2, 10, 50, 30);

        t.start();
    }

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

        Rectangle r1 = new Rectangle(x, 10, 50, 30);
        Rectangle r2 = new Rectangle(x2, 10, 50, 30);

        g.setColor(Color.BLUE);
        g.fillRect(x, 10, 50, 30);

        g.setColor(Color.RED);
        g.fillRect(x2, 10, 50, 30);
    }

    public void actionPerformed(ActionEvent e) {
        if (x < 0 || x > 450 || r1.intersects(r2)) {
            velx = -velx;
        }
        if (x2 < 0 || x2 > 450 || r1.intersects(r2)) {
            velx2 = -velx2;
        }
        x += velx;
        x2 -= velx2;

        repaint();
    }
}
英文:

In the code below, I managed to make these two rectangles bounce at the JPanel border, but I cannot make them bounce when they collide (elastic collision). I think there is a problem within actionPerformed method (correct me if I am wrong). What should I fix in my code?

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class box {
public static void main(String args[]) {
objectFrame frame = new objectFrame();
}	
}
class objectFrame extends JFrame {
objectFrame() {
objectPanel panel = new objectPanel();
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(panel);
}
}
class objectPanel extends JPanel implements ActionListener {
private Timer t;
private int x, velx, x2, velx2;
private Rectangle r1, r2;
objectPanel() {
t = new Timer(5, this);
x = 100;
x2 = 400;
velx = 2;
velx2 = 2; 
r1 = new Rectangle(x, 10, 50, 30);
r2 = new Rectangle(x2, 10, 50, 30);
t.start();
}	
public void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle r1 = new Rectangle(x, 10, 50, 30);
Rectangle r2 = new Rectangle(x2, 10, 50, 30);
g.setColor(Color.BLUE);
g.fillRect(x, 10, 50, 30);
g.setColor(Color.RED);
g.fillRect(x2, 10, 50, 30);
}
public void actionPerformed(ActionEvent e) {
if (x &lt; 0 || x &gt; 450 || r1.intersects(r2)) {
velx = -velx;
}
if (x2 &lt; 0 || x2 &gt; 450 || r1.intersects(r2)) {
velx2 = -velx2;
}
x += velx;
x2 -= velx2;
repaint();
}
}

答案1

得分: 1

我相信你没有模拟考虑碰撞物体的质量和方向的弹跳行为。

你需要实现弹性碰撞背后的物理效应,以获得这种效果:
https://en.wikipedia.org/wiki/Elastic_collision

有很多示例可以参考。这里有一个我一段时间前实现的例子。
https://github.com/gtiwari333/java-collision-detection-source-code
碰撞逻辑取自https://gamedev.stackexchange.com/questions/20516/ball-collisions-sticking-together。

英文:

I believe you are not simulating the bouncing behavior that considers the mass and direction of colliding objects.

You will need to implement the physics behind an elastic collision to get the effect:
https://en.wikipedia.org/wiki/Elastic_collision

There is plenty of examples out there. Here's one that I implemented a while back.
https://github.com/gtiwari333/java-collision-detection-source-code.
The collision logic was taken from https://gamedev.stackexchange.com/questions/20516/ball-collisions-sticking-together.

huangapple
  • 本文由 发表于 2020年9月13日 11:35:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定