弹弹球不会止步于边境 JAVA

问题描述 投票:0回答:1

我的弹跳球是要在200x200的窗口边框之间弹跳的,我设法让他在触及右边和底部边框时停止并改变方向,但当他到达顶部和左侧边框时,球的14个部分穿过边框,然后才改变方向。

我不知道为什么会发生这种情况,我是说每个边界的代码都是一样的。怎么可能会出现同样的代码会有不同的效果呢?

我在网上查了很多关于这个问题的代码,尝试了所有的解决方案或代码,但还是一样。

谢谢。

 public Point applyToPoint(Point p) {
        return new Point(p.getX() + dx, p.getY() + dy);
    }

    public void moveOneStep(int width, int height) {
    if (this.center.getX() + this.getVelocity().dx + r > width) {
        this.setVelocity(-(this.getVelocity().dx), this.getVelocity().dy);
    }
    if (this.center.getX() + this.getVelocity().dx < 0) {
        this.setVelocity(-(this.getVelocity().dx), this.getVelocity().dy);
    }
    if (this.center.getY() + this.getVelocity().dy + r > height) {
        this.setVelocity(this.getVelocity().dx, -(this.getVelocity().dy));
    }
    if (this.center.getY() + this.getVelocity().dy < 0) {
        this.setVelocity(this.getVelocity().dx, -(this.getVelocity().dy));
    }
    moveOneStep();
}

public void moveOneStep() {
    this.center = this.getVelocity().applyToPoint(this.center);
}

r = 球的半径。

"this.center" = 球的中心点。

左边框上的球的截图。

img

 import biuoop.DrawSurface;
import biuoop.GUI;
import biuoop.Sleeper;

public class BouncingBallAnimation {

    static private void drawAnimation(Point start, double dx, double dy) {
        GUI gui = new GUI("BouncingBall",200,200);
        Sleeper sleeper = new Sleeper();
        Ball ball = new Ball(new Point(start.getX(), start.getY()), 30, java.awt.Color.BLACK);
        ball.setVelocity(dx, dy);
        while (true) {
            DrawSurface d = gui.getDrawSurface();
            ball.moveOneStep(d.getHeight(),d.getWidth());
            ball.drawOn(d);
            gui.show(d);
            sleeper.sleepFor(50);  // wait for 50 milliseconds.
        }
    }
    public static void main(String[] args) {
        drawAnimation(new Point(20,33),6,6); //just a random input that I decided
    }
}
java animation geometry border bounce
1个回答
0
投票

我的意思是,每个边框的代码都是一样的。

那就有问题了。因为你们的情况不同,怎么可能一样呢?

当球向右下移动时,xy值增加。

当球向左上移动时,xy值就会减少。

if (this.center.getX() + this.getVelocity().dx < 0) {

我猜应该是这样的。

if (this.center.getX() - this.getVelocity().dx - r < 0) {

假设 "中心 "实际上是圆的中心

修改一下。

还有..:

    this.setVelocity(-(this.getVelocity().dx), this.getVelocity().dy);
}
if (this.center.getX() + this.getVelocity().dx < 0) {
    this.setVelocity(-(this.getVelocity().dx), this.getVelocity().dy);

两种情况下X速度的变化怎么可能都是负数呢?

当向右移动时,你从正速度变成了负速度。

那么当你向左移动时,你是否应该从负速度变成正速度?


0
投票

好了,下面是工作代码: public void moveOneStep(int startX, int height, int startY, int width) { {

    if (this.center.getX() + this.getVelocity().dx + r >= width) {
        this.setVelocity(-1 * (this.getVelocity().dx), this.getVelocity().dy);
    }
    if (this.center.getX() + this.getVelocity().dx - r <= startX) {
        this.setVelocity(-1 * (this.getVelocity().dx), this.getVelocity().dy);
    }
    if (this.center.getY() + this.getVelocity().dy + r >= height) {
        this.setVelocity(this.getVelocity().dx, -1 * (this.getVelocity().dy));
    }
    if (this.center.getY() + this.getVelocity().dy - r <= startY) {
        this.setVelocity(this.getVelocity().dx, -1 * (this.getVelocity().dy));
    }
    moveOneStep();
}

问题不在于代码,而在于我输入检查圆的起始点.你不能给圆一个起始中心点让我们说(20,50),如果半径是30.为什么呢?因为这样的话,圆从一开始就会出界,因为他的起始中心点是20,50,半径是30,这意味着他的左半径将达到(-10,50)。这就是为什么我有很多问题.你总是应该检查起点是否适合半径。

© www.soinside.com 2019 - 2024. All rights reserved.