0

我有一个对象(它是一个球),它的位置在屏幕顶部,每当我开始运行程序时它就会直接落下。问题是球以恒定速度下落,我希望它在重力作用下加速下落,当它到达地面时,我希望它在停止移动之前再反弹几次。有人可以帮我解决这个问题吗?

这是我尝试过的:

public class Balls
{
private double x;
private double y;
private double speed;
private double mass;
private final double gravity = -9.8;
private final double width = 100;
private double height = 100;
private final Board board;
private boolean isFalling = false;
private double distance_y;
private double distance_x = 0;

public Balls(double x, double y, double speed, double mass, Board board)
{
    this.x = x;
    this.y = y;
    this.board = board;
    this.speed = convertToMeterSpeed(speed);
    this.mass = mass;

}

private double convertToMeterSpeed(double speed)
{
    return speed / 3.6;
}

public void moveBall(long dt)
{

    double time = dt / 1e9; // seconds
    double diameter_y = height / 2.0;
    double radius = (diameter_y / 2.0);
    double velocity_y = speed * dt / 1e9;
    distance_y = board.getHeight() - y;

    if (distance_y - radius > 0)
    {
        isFalling = true;
    }

    if (isFalling)
    {
        if (distance_y >= height)
        {
            distance_y = distance_y + (0.5 * gravity * (time * time));  // represents the 1/2,
            distance_y = board.getHeight() - height;
            y += velocity_y;
        }

        else
        {
            isFalling = false;
        }

        try
        {
            Thread.sleep(10);
        }
        catch (InterruptedException e)
        {

        }
    }
}


public void render(Graphics2D g2d)
{
         g2d.fillOval((int) x, (int) y, (int) width, (int) height);

}
}
4

3 回答 3

3

速度 = v0 + gt^2/2,

在哪里

v0 - 初始速度

g = 9.81 在地球上。

t - 时间

现在您可以随时计算速度。

于 2011-11-09T08:30:46.897 回答
2

你可能想定义一个最大速度(终端速度),这样你的球就不会加速到一个巨大的速度。重力以 9.8m/s/s 的速度加速。一旦球击中“地面”,您只需反转速度并更新当前位置以使其反弹,然后在您的下一次迭代中将再次应用重力,因此它会回落。最终,由于球没有反弹太多,速度将变为 0,并且会停止。

这是一个(未经测试的)示例:

private static final double GRAVITY = 9.8;
private static final double TERMINAL_VELOCITY = 100;
private double speed;
private int current_y;

public void fallAndBounce() {
    speed = speed + GRAVITY;

    if (speed > TERMINAL_VELOCITY) { speed = TERMINAL_VELOCITY; }

    if (current_y >= bottomOfScreen)
    {
        //We have hit the "ground", so bounce back up. Reverse
        //the speed and divide by 4 to make it slower on bouncing.
        //Just change 4 to 2 or something to make it faster.
        speed = -speed/4; 
    } 
    current_y += speed; 
}
于 2011-11-09T08:53:35.457 回答
0

您的问题是您正在尝试为球下落设置动画,但您正在编写一个算法来解决给定时间的位置。

这意味着您应该将时间变量dt 完全排除在等式之外,并且只需像这样在循环的每次迭代中移动您的球

while (true)
{
    moveBall();
    render();
    try {
        Thread.sleep(10)
    } catch(InterruptedException e) {
        e.printStackTrace(); 
    }
}

另外,稍微改变你的变量:

// Add this to your variables
private final double GRAVITY = -9.8;    // Final variables should be capitalized
private final double TERMINAL_VELOCITY = -30; // Whatever you want it to be 

这是主要的变化:

public void moveBall()
{
    double diameter_y = height / 2.0;
    double radius = (diameter_y / 2.0);
    double velocity_y = speed * dt / 1e9;
    distance_y = board.getHeight() - y;

    if (distance_y - radius > 0)
    {
        isFalling = true;
    }

    if (isFalling)
    {
        if (height < distance_y)
        {
            if (velocity_y <= TERMINAL_VELOCITY)
                velocity_y += GRAVITY;  // implementing acceleration (gravity)
                                        // just means adding it to velocity.
            y += velocity_y;
        }

真的,我不知道这以前是如何工作的。

于 2013-11-02T00:48:30.890 回答