【发布时间】:2013-12-12 16:05:27
【问题描述】:
我正在开发一个弹跳球程序。我成功地制作了一个上下起伏的球。我设置了球不能出界,所以当它碰到屏幕边缘时,它会简单地反弹回来等等。
现在,问题是,我希望球最终停止移动。例如,我启动程序,球落下并弹回大约 80% 的起始高度。当它再次下降时,它会因重力而加速,然后再上升,但可能只能达到原来高度的 60% 左右,最终它会停止移动。
我如何创建这样的东西?我已经搜索了几个小时,但没有找到任何帮助。所以现在我恳求你帮我一把。另外,如果您决定给我一个方便的提示,请尽量具体且非常清楚。我已经很久没有编程了。提前致谢。
这是我的代码:
编辑:请注意我没有此类的主要方法,因为我不需要它。我通过一个对象在另一个类中运行它。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Game extends JPanel implements ActionListener{
int DIAMETER = 40;
int yPos;
int yVel = 3;
int GRAVITY =1;
Timer tm = new Timer(5,this);
public void paintComponent(Graphics g){
super.paintComponent(g);
//Setting the characteristics for the ball
g.setColor(Color.red);
g.fillOval(0, yPos, DIAMETER, DIAMETER);
tm.start();
repaint();
}
public void actionPerformed(ActionEvent e) {
//If it decides to go out of the screen, change direction.
if(yPos<0 || yPos>430)yVel=-yVel;
//This basically is the "engine". It moves the ball.
yPos = yPos + yVel;
}
}
【问题讨论】:
-
你所指的概念叫做弹性碰撞,查一下你就会知道如何将物理方程应用到你的模拟中
-
不,肯定是没有弹性的。
-
同理,弹性系数为0
-
@MangO_O:我不认为"coefficient of elasticity" 意味着你认为它的作用,如果你想说的是0,那可能是一个完全非弹性的碰撞。这不是完全无弹性的碰撞。