【问题标题】:How to make two balls collide and deflect in opposite directions?如何使两个球相撞并朝相反方向偏转?
【发布时间】:2016-08-04 00:42:55
【问题描述】:

我已经编写了用于在矩形(画布)内移动两个球的代码。当球击中矩形的顶部、底部、左侧或右侧时,它们会向相反的方向偏转。但是,我试图让球相互碰撞并朝相反的方向偏转,但徒劳无功。我搜索了许多网站和文章,但徒劳无功。有人可以帮忙吗?

这是 MainActivity.java

 package com.example.movements;
 public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MovementView(this));
}
}

这是 MovementView.java

package com.example.movements;
public class MovementView extends SurfaceView implements SurfaceHolder.Callback{
private int xPos,xPos1;
private int yPos,yPos1;
private int xVel,xVel1;
private int yVel,yVel1;
private int width;
private int height;
private int circleRadius,circleRadius1;
private Paint circlePaint,circlePaint1;
UpdateThread updateThread;
public MovementView(Context context) {
    super(context);
    getHolder().addCallback(this);
    circleRadius = 10;
    circlePaint = new Paint();
    circlePaint.setColor(Color.BLUE);
    xVel = 10;
    yVel = 10;
    circleRadius1 = 10;
    circlePaint1 = new Paint();
    circlePaint1.setColor(Color.MAGENTA);
    xVel1 = 11;
    yVel1 = 11;
}
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    canvas.drawCircle(xPos, yPos, circleRadius, circlePaint);
    canvas.drawCircle(xPos1, yPos1, circleRadius1, circlePaint1);

}
public void updatePhysics() {
    xPos += xVel;
    yPos += yVel;
    if (yPos - circleRadius < 0 || yPos + circleRadius > height) {
        if (yPos - circleRadius < 0) {
            yPos = circleRadius;
        }else{
            yPos = height - circleRadius;
        }
        yVel *= -1;
    }
    if (xPos - circleRadius < 0 || xPos + circleRadius > width) {
        if (xPos - circleRadius < 0) {
            xPos = circleRadius;
        } else {
            xPos = width - circleRadius;
        }
        xVel *= -1;
    }
    xPos1 += xVel1;
    yPos1 += yVel1;
    if (yPos1 - circleRadius1 < 0 || yPos1 + circleRadius1 > height) {
        if (yPos1 - circleRadius1 < 0) {
            yPos1 = circleRadius1;
        }else{
            yPos1 = height - circleRadius1;
        }
        yVel1 *= -1;
    }
    if (xPos1 - circleRadius1 < 0 || xPos1 + circleRadius1 > width) {
        if (xPos1 - circleRadius1 < 0) {
            xPos1 = circleRadius1;
        } else {
            xPos1 = width - circleRadius1;
        }
        xVel1 *= -1;
    }
}
public void surfaceCreated(SurfaceHolder holder) {
    Rect surfaceFrame = holder.getSurfaceFrame();
    width = surfaceFrame.width();
    height = surfaceFrame.height();
    xPos = width / 2;
    yPos = circleRadius;
    xPos1 = width / 2;
    yPos1 = circleRadius1;
    updateThread = new UpdateThread(this);
    updateThread.setRunning(true);
    updateThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    updateThread.setRunning(false);
    while (retry) {
        try {
            updateThread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }
}
}

这是 UpdateThread.java

package com.example.movements;
import android.view.SurfaceHolder;
public class UpdateThread extends Thread {
private long time;
private final int fps = 20;
private boolean toRun = false;
private MovementView movementView;
private SurfaceHolder surfaceHolder;
public UpdateThread(MovementView rMovementView) {
    movementView = rMovementView;
    surfaceHolder = movementView.getHolder();
}
public void setRunning(boolean run) {
    toRun = run;
}
@Override
public void run() {
    Canvas c;
    while (toRun) {
        long cTime = System.currentTimeMillis();
        if ((cTime - time) <= (1000 / fps)) {
            c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                movementView.updatePhysics();
                movementView.onDraw(c);
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
        time = cTime;
    }
}
}

【问题讨论】:

  • google 恢复系数弹性碰撞

标签: java android


【解决方案1】:

要检查它们是否相互碰撞,您只需检查它们中心之间的距离是否小于(半径*2)。为了做出很好的偏转,你必须做一些我无法理解的数学运算。 Here 是另一个答案。

我用谷歌搜索了一下,显然物理学中用于这个问题的术语称为弹性碰撞。我找到了一个很好的关于主题here 的教程(尤其是动态圆-圆碰撞位)。此外,here 是一个演示了这一点的小程序。可以在[这里]找到源代码

【讨论】:

  • 我加了一点解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2019-02-05
相关资源
最近更新 更多