gpt4 book ai didi

java - 物体下落时如何施加重力?

转载 作者:行者123 更新时间:2023-12-01 17:34:10 24 4
gpt4 key购买 nike

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

这是我尝试过的:

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

}
}

最佳答案

速度 = v0 + gt^2/2,

哪里

v0 - 初始速度

地球上的 g = 9.81。

t - 时间

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

关于java - 物体下落时如何施加重力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com