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 に答える