java - 如何使用 Verlet 积分器避免因时间步长变化而产生的不需要的额外速度

标签 java android garbage-collection physics

我知道这个标题让人大跌眼镜,但它主要是一个副作用问题。我正在编写一个 Android 应用程序,我可以将它与我在物理课上学习的数学一起使用。这是一个 2D 弹跳球应用程序。我正在使用时间校正的 Verlet 积分器,在屏幕底部的地板上有一个脉冲。我正在添加摩擦力和弹力,以便球最终达到 0 速度。

当球停在地板上并且发生“显着”时间步长变化时,问题就出现了。积分器完美地调整速度并最终在地板上发射脉冲。当速度的 abs 值大于 2.5 时,脉冲会触发。 Android 的 GC 通常会导致时间调整 -18 速度。

感谢任何帮助。我意识到代码结构可能会更好,但我只是为了好玩而尝试可视化和应用物理学。谢谢。

// The loop
public void run() {
    if(mRenderables != null) {
        final long time = SystemClock.uptimeMillis();
       final long timeDelta = time - mLastTime;

        if(mLastTime != 0) {
            final float timeDeltaSeconds = timeDelta / 1000.0f; 

            if(mLastTimeDeltaSec != 0) {                    
                for(short i = 0; i < mRendLength; i++) {
                    Ball b1 = mRenderables[i];

                    // Acceleration is gauged by screen's tilt angle
                    final float gravityX = -mSV.mSensorX * b1.MASS;
                    final float gravityY = -mSV.mSensorY * b1.MASS;

                    computeVerletMethod(b1, gravityX, gravityY, timeDeltaSeconds, mLastTimeDeltaSec);
                }
            }

            mLastTimeDeltaSec = timeDeltaSeconds;
        }

        mLastTime = time;
    }
}

/*
* Time-Corrected Verlet Integration
* xi+1 = xi + (xi - xi-1) * (dti / dti-1) + a * dti * dti
*/  
public void computeVerletMethod(Renderable obj, float gravityX, float gravityY, float dt, float lDT) {
    mTmp.x = obj.pos.x;
    mTmp.y = obj.pos.y;

    obj.vel.x = obj.pos.x - obj.oldPos.x;
    obj.vel.y = obj.pos.y - obj.oldPos.y;
    // Log "1." here        

    resolveScreenCollision(obj);

    obj.pos.x += obj.FRICTION * (dt / lDT) * obj.vel.x + gravityX * (dt * dt);
    obj.pos.y += obj.FRICTION * (dt / lDT) * obj.vel.y + gravityY * (dt * dt);

    obj.oldPos.x = mTmp.x;
    obj.oldPos.y = mTmp.y;
    // Log "2." here
}

// Screen edge detection and resolver
public void resolveScreenCollision(Renderable obj) {
   final short xmax = (short) (mSV.mViewWidth - obj.width);
   final short ymax = (short) (mSV.mViewHeight - obj.height);
   final float x = obj.pos.x;
   final float y = obj.pos.y;

   // Only testing bottom of screen for now     
   if (y > ymax) {
    // ...
    } else if (y < 0.5f) {
        if(Math.abs(obj.vel.y) > 2.5f) {
            float imp = (obj.MASS * (obj.vel.y * obj.vel.y) / 2) * obj.RESTITUTION / obj.MASS;
            obj.vel.y += imp;
           // Log "bounce" here
        } else {
            obj.vel.y = obj.pos.y = obj.oldPos.y = mTmp.y = 0.0f;
        }
    }
}

当球停在地板上并突然产生冲动时输出 (参见“日志”注释的代码)

1.  vel.y: -0.48258796
2.  pos.y: -0.42748278 /oldpos.y: 0.0 /dt: 0.016 /ldt: 0.017

1.  vel.y: -0.42748278
dalvikvm  GC_FOR_MALLOC freed 8536 objects / 585272 byte s in 74ms
2.  pos.y: -0.48258796 /oldpos.y: 0.0 /dt: 0.017 /ldt: 0.016

1.  vel.y: -0.48258796
2.  pos.y: -18.061148 /oldpos.y: 0.0 /dt: 0.104 /ldt: 0.017

1.  vel.y: -18.061148
bounce  imp: 124.35645
2.  pos.y: 13.805508 /oldpos.y: -18.061148 /dt: 0.015 /ldt: 0.104

最佳答案

你不应该使用基于之前计算发生的时间的时间步长,因为如果你还没有这样的话,这可能会导致诸如此类的问题和碰撞检测中的错误。相反,对于每次更新,您应该为每次更新设置一个“时间 block ”或最大时间量。例如:假设您想要 30fps 并且在纳秒内大约为 33333333。所以 33333333 = 1 个时间 block 。那么你可以做一个 while 循环

long difftime = System.nanoTime() - lastTime;
static long fpstn = 1000000000 / 30;
static int maxtimes = 10;// This is used to prevent what is commonly known as the spiral of death: the calcutaions are longer that the time you give them. in this case you have to increase the value of a base timechunk in your calculations
for (int i = 0; i < maxtimes; i++) {
    if (difftime >= fpstn) {
        world.updateVerlet(1);
    } else {
        world.updateVerlet((float)diffTime / (float)fpstn);
    }
    difftime -= fpstn;
    if (difftime <= 0)
        break;
}

关于java - 如何使用 Verlet 积分器避免因时间步长变化而产生的不需要的额外速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4913269/

相关文章:

java - 如何在java中显示像素值数组中的图像?

android - 如何获得所选国家的国定假日

Android - 如何以编程方式检查当前是否启用了 Firebase Analytics?

android - 每当我尝试创建一个新的 react 函数时,未定义都不是一个函数

swift - Swift 中如何避免内存碎片

java - 尝试在 Apache Tomcat 上部署 GWT 失败

Java - 了解 PrintWriter 和刷新需求

java - 在现有元素之后添加新元素到 LinkedList 时的性能

java - java或其他gc-lang中的gc会自动关闭未关闭的资源吗?以及为什么

python - Python 的垃圾收集器会损害我的应用程序吗?