【问题标题】:Buoyant Force simulation浮力模拟
【发布时间】:2016-05-29 18:14:29
【问题描述】:

我正在尝试在 Android 应用程序的 2D 对象上模拟浮力行为,不涉及摩擦。有些东西不能正常工作:“反弹”的高度以不规则的方式变化,我认为错误应该在速度的更新中但我不确定。

谁能看到我的代码中的错误?

public class MainActivity extends Activity {

    Semaphore moveSemaphore = new Semaphore(1);
    static WindowManager.LayoutParams params;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        params = new WindowManager.LayoutParams( 80, 80,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT);

        final BallView ball = new BallView(this);
        addContentView(ball, params);
        ball.setX(100);
        ball.setY(50);

        final Thread move = new Thread(new Runnable(){
            double speed = 0;
            double G = 9;
            double fullBuoyancy = -G * 10;
            double prevtime = System.currentTimeMillis();
            double elapsed;

            double fluidstart = 300;
            double objectHeight = 100;
            double currPosy = 50;
            double p;

            @Override
            public void run() {

                while(true){

                    try {
                        moveSemaphore.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            double currDepth = 0;
                            long currtime = System.currentTimeMillis();
                            elapsed = (currtime - prevtime) * 0.01;
                            prevtime = currtime;
                            currPosy = ball.getY();

                            // COMPUTE HOW MUCH OF THE BALL IS INSIDE OF THE FLUID
                            if (currPosy > (fluidstart + objectHeight/2)){
                                currDepth = objectHeight;
                            } else if (currPosy > (fluidstart - objectHeight/2)) {
                                currDepth = currPosy - fluidstart;
                            }


                            p = currDepth / objectHeight; // PERCENTAGE OF THE FULL BUOYANT FORCE TO BE APPLIED
                            speed = speed + (G + fullBuoyancy * p) * elapsed;

                            currPosy += speed * elapsed;
                            ball.setY((float)currPosy);

                            moveSemaphore.release();
                        }
                    });
                }
            }
        });

        move.start();
    }
}

【问题讨论】:

    标签: android 2d game-physics


    【解决方案1】:

    问题出在 if 语句中,这是正确的解决方案:

    if (currPosy > (fluidstart + objectHeight/2)){//complete submersion
        currDepth = objectHeight;
    } else if (currPosy > (fluidstart - objectHeight/2)) {//partial submersion
        currDepth = (currPosy + objectHeight/2) - fluidstart;
    }
    

    【讨论】:

      【解决方案2】:

      if 语句逻辑不正确。

      double emerge = fluidstart - (currPosy - (objectHeight / 2));
      if (emerge <= 0) { // complete submersion
         currDepth = objectHeight;
      } else {
         currDepth = objectHeight - emerge;
      }
      

      【讨论】:

      • 您好,感谢您的回答!这确实是 if 语句的问题,但解决方案与您的解决方案略有不同,因为在这种情况下 y 轴指向下方。 currDepth = 0 的情况被覆盖,因为它的值在循环开始时被初始化为那个值。
      • @u09 已编辑。抱歉,我错过了这一点 - 我以为你在外面声明它 way 就像其他变量一样 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 2017-02-25
      • 2022-01-14
      • 2012-10-29
      相关资源
      最近更新 更多