【发布时间】:2017-04-24 14:01:43
【问题描述】:
我一直在编写一些关于弹丸运动的代码。我启动了一个计时器 t 并为其使用了构造函数 10,每个动作事件我将 0.01 添加到初始化为 0.00 的时间变量中,以模拟时间过去。使用这段时间我计算高度/距离等。我无法弄清楚 2 个问题:
1) 将当前时间打印到控制台时,它会保留许多小数位,运行代码。 2) 高度会一直增加,虽然不应该增加,感觉好像是加速的正负作用。
(我的面板是 1920x1080,面板动画占用 80 像素 - 这就是为什么它是 1000-50)(1000 半径)
来自回复: 1) 忘记将 vy 乘以 t。 2)使用DecimalFormat截断值
感谢@G_H; @砖; @汤姆; @Diginoise 代码:
package projectV1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Animate extends JPanel implements ActionListener {
double x = 0;
double time = 0.00;
String timeText2 = "0";
double y = 1000;
double velY = 0.0;
double velX = 0.0;
public static Timer t;
public Animate() { // Constructor
super();
}
public void start(String acceleration, String initialVelocity, String angle){
new Ball(acceleration, angle, initialVelocity);
t = new Timer(10, this);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
updateTime();
move();
}
public void move(){ // HOlds the repaint method
x = x + Ball.getVelX();
Simulation.distanceText.setText(Double.toString(Ball.getDistance(time))); // Some of these are just checks for me in the gui
Simulation.velocityText.setText(Double.toString(time));
y = y- Ball.getVelY(time);
Simulation.heightText.setText(Double.toString(Ball.getHeight(time)));
repaint();
}
public void updateTime(){
time= time + 0.01;
System.out.println(Double.toString(time)); // Using this as a test to check the time output.
timeText2 = Double.toString(time);
Simulation.timeText.setText(timeText2);
}
public void paintComponent(Graphics gg){
super.paintComponent(gg);
gg.drawRect(0, 0, 1920, 1000);
Graphics2D g = (Graphics2D) gg;
Ellipse2D.Double shape = new Ellipse2D.Double(x, y-50.0, 50, 50);
g.fill(shape);
// g.fillOval(x, y-50, 50, 50);
}
}
来自“球类”的代码
public Ball(String acceleration, String angle, String initialVelocity ) {
Ball.acceleration = acceleration;
Ball.initialVelocity = initialVelocity;
Ball.angle = angle;
}
public static double getVelX(){ // This stays constant throughout the program
double velX = 0.0;
int u = Integer.parseInt(initialVelocity);
double ang = Double.parseDouble(angle);
velX = u*(Math.cos(ang));
return velX;
}
public static double getVelY(double time ){
double velY = 0.0 ;
double ang, acc = 0;
int u = 0;
ang = Double.parseDouble(angle);
acc = Double.parseDouble(acceleration);
u = Integer.parseInt(initialVelocity);
velY = u*(Math.sin(ang))- (acc*time);
return velY;
}
public static double getDistance(double time){
double distance = 0;
double ang = 0;
int u = 0;
ang = Double.parseDouble(angle);
u = Integer.parseInt(initialVelocity);
distance = (u*time)*(Math.cos(ang));
return distance;
}
public static double predictedTime(){
double ptime = 0;
double ang = 0;
ang = Double.parseDouble(angle);
int u = Integer.parseInt(initialVelocity);
ptime = ((u*u)*(Math.sin(ang)*Math.sin(ang))/(2*9.81));
return ptime;
}
public static double getHeight(double time ){
double height;
double ang, acc;
int u = 0;
ang = Double.parseDouble(angle);
acc = Double.parseDouble(acceleration);
u = Integer.parseInt(initialVelocity);
height = u*(Math.sin(ang))-(0.5*acc*(time*time));
return height;
}
【问题讨论】:
-
我不认为这是一个四舍五入的问题,老实说我以前见过,我认为它没有帮助。如果是,那么我错了,但正如你所见,这不是这里唯一的问题。我试过: Math.round(time*100)/100 但它不起作用
-
如果你需要物理部分的帮助,我建议你写出你认为你正在使用的方程。与您说要解决的问题相比,我无法快速理解您的代码。
-
你说的小数位数问题几乎肯定是四舍五入的错误,可以按照@Tom提供的链接中的说明进行控制。
-
是的,我将在接下来的 5 分钟内添加我正在使用的 5 个屏幕截图。我试图舍入它,但它只是停留在 0.0。如果您可以帮助我如何实例化计时器并将其与“实时”毫秒相关联,那将是惊人的。屏幕将在接下来的 5 分钟内启动
标签: java timer simulation physics projectile