当物体掉落时如何施加重力?

时间:2011-11-09 08:28:17

标签: java gravity bounce

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

这是我尝试过的:

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);

}
}

3 个答案:

答案 0 :(得分:3)

速度= v0 + gt ^ 2/2,

其中

v0 - 初始速度

g = 9.81在地球上。

时间

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

答案 1 :(得分: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; 
}

答案 2 :(得分: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;
        }

真的,我不知道之前是怎么回事。

相关问题