我想创建Pong,并且正在为球创造物理。为什么不会倍增倍增? (Java)
我有以下代码:
public Ball(Pong pong) {
random = new Random();
this.x = pong.width/2 - this.width/2;
this.y = pong.height/2 - this.height/2;
this.pong = pong;
this.motionX = 2.0;
this.motionY = random.nextDouble();
while (this.motionY > 0.7) {
this.motionY = random.nextDouble();
System.out.println(motionY);
}
multiplier = 8/this.motionX;
System.out.println(motionX);
this.motionX = multiplier * motionX;
System.out.println(motionX);
this.motionY *= multiplier;
}
public void update(Paddle paddle1, Paddle paddle2) {
this.x += this.motionX;
this.y += this.motionY;
}
public void render(Graphics2D g) {
Graphics2D gg = (Graphics2D) g;
g.setColor(Color.WHITE);
Ellipse2D.Double shape = new Ellipse2D.Double(x, y, width, height);
g.fill(shape);
}
在球的方法,我已经设置的MotionX至2.0。稍后在方法中,我使用乘法器对motionX进行计时,这样我可以使所有的球速度保持不变。但是,8.0版本应该是2.0 * 8.0(16.0)。这发生在每个数字。
任何想法是什么错?
只是一个侧面说明,但为什么要使用一个while循环来获得'motionY'的值小于0.7?只需乘以0.7即可离开环路。更高效,更好的风格,... – Paul
只要一点:不要把'[解决]'放在你的问号上,这个答案对你更有帮助,因为你已经接受了(在答案的左边有一个勾号,只是按它来标记你最喜欢的答案为接受):) –