gpt4 book ai didi

java - 弹跳球的物理学

转载 作者:行者123 更新时间:2023-11-29 05:06:12 24 4
gpt4 key购买 nike

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

我正在尝试创建具有逼真的物理特性的弹跳球。此刻,当球相互撞击时,它们会朝原来的方向弹回,现在,我在互联网上搜索了如何做到这一点,但我只找到了关于如何检测碰撞的信息,而不是什么之后做。我对物理了解不多,在我能够做到这一点之前我应该​​先学习他们的主题吗?这是我想象球在现实生活中会如何弹跳的图像。这是它的工作原理吗?

how I think it should work
(来源:thewombatguru.nl)

此外,我的代码中是否存在不良做法? (可能,但我学到了很多东西而且我喜欢它)

这是我当前的代码:

小行星.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);
}

}

有人能帮忙吗?

最佳答案

您可以从动量守恒定律开始。当两个物体碰撞时总动量不会改变,可以用this link在尝试预测两个物体碰撞后的轨迹时了解背后的计算。

至于您的代码,您似乎在 Rock.java 中缺少一个关键字段 mass。您需要质量来计算物体的动量,稍后您将使用它来预测物体碰撞后的轨迹。

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

关于java - 弹跳球的物理学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30302112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com