由于重力问题,利用时间来提高速度(无法获得完美的弹性)

发布于 2024-11-16 08:33:20 字数 837 浏览 16 评论 0原文

我只是想把球弹到原来的高度。但一旦我引入时间而不是框架,我就会在某个地方失去动力。

将代码降至最低限度,我有以下内容:

public void onDrawFrame(GL10 gl) {

float timeDelta = System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();

update(timeDelta);
}

float gravity = -1.0f;
float PosY = 2400;
float VelocityY = 0;

public void update(float timeDelta){

PosY+=VelocityY;
if(PosY<=0){
    VelocityY=Math.abs(VelocityY);
} else{
    VelocityY+=gravity*timeDelta;
}       
Log.d(SystemSingleton.sLogDebug,String.format("Pos: y%f. y%f, timeDelta: %f",PosY, VelocityY, timeDelta));

}

我将时间增量保留为毫秒(而不是像您期望的那样除以 1000 以获得秒数),以确保我不会因强制转换或舍入而丢失任何内容。

如果我忽略 timeDelta 并仅在每一帧应用整个重力,则效果很好,其弹跳峰值将恰好为 2400(它的起始位置)。但考虑到 timedelta,它总是小于 2400,并且缓慢下降,然后在再次开始下降之前不时地大幅上升(有时远高于 2400)。

显然我发现了一些非常严重的错误,但我看不到它。

任何帮助表示赞赏。

I am just trying to bounce a ball to the same height it was at originally. But as soon as I introduce time, rather than frame, i lose momentum somewhere.

Taking the code down to its bare minimum I have the following:

public void onDrawFrame(GL10 gl) {

float timeDelta = System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();

update(timeDelta);
}

float gravity = -1.0f;
float PosY = 2400;
float VelocityY = 0;

public void update(float timeDelta){

PosY+=VelocityY;
if(PosY<=0){
    VelocityY=Math.abs(VelocityY);
} else{
    VelocityY+=gravity*timeDelta;
}       
Log.d(SystemSingleton.sLogDebug,String.format("Pos: y%f. y%f, timeDelta: %f",PosY, VelocityY, timeDelta));

}

Ive left the time delta as milliseconds (rather than dividing by 1000 to give me seconds as you would expect) to make sure I'm not losing anything due to casts or rounding.

If I ignore timeDelta and just apply the entire gravity every frame, this works fine, the peak of its bounce will be exactly 2400 (it's starting position). But taking timedelta into account, it is always less than 2400 and slowly degrades and then every now and again jumps up a significant amount (sometimes way above 2400) before it begins to degrade again.

Ive obviously got something quite drastically wrong but I can't see it.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-11-23 08:33:20

计算新位置时,您需要考虑时间步长 (timeDelta)。不要这样做:

PosY += VelocityY;  // no!!

这样做:

PosY += VelocityY * timeDelta;

更好的是,这样做:

PosY += VelocityY * timeDelta + 0.5 * gravity * timeDelta * timeDelta;

另外,我会稍微改变一下反弹检测。这是一些可以尝试的新代码:

// Perform the integration
PosY      += VelocityY * timeDelta + 0.5 * gravity * timeDelta * timeDelta;
VelocityY += gravity * timeDelta;

// Check whether it's time to bounce
if (PosY<=0 && VelocityY<0){
    VelocityY = Math.abs(VelocityY);
}       
Log.d(...)

You need to take the time step (timeDelta) into account when you calculate the new position. Instead of doing this:

PosY += VelocityY;  // no!!

Do this:

PosY += VelocityY * timeDelta;

Even better, do this:

PosY += VelocityY * timeDelta + 0.5 * gravity * timeDelta * timeDelta;

Also, I would change the bounce detection a little. Here is some new code to try:

// Perform the integration
PosY      += VelocityY * timeDelta + 0.5 * gravity * timeDelta * timeDelta;
VelocityY += gravity * timeDelta;

// Check whether it's time to bounce
if (PosY<=0 && VelocityY<0){
    VelocityY = Math.abs(VelocityY);
}       
Log.d(...)
我喜欢麦丽素 2024-11-23 08:33:20
float timeDelta = System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();

我的猜测是这个原因。调用 currentTimeMillis 可能会给出两个不同的数字。我会做什么:

float currentTime = System.currentTimeMillis();
float delta = currentTime - startTime;
startTime = currentTime;
float timeDelta = System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();

My guess is this is the cause. Calling currentTimeMillis could give you two different numbers. What I would do:

float currentTime = System.currentTimeMillis();
float delta = currentTime - startTime;
startTime = currentTime;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文