java - 如何使两个球相撞并朝相反方向偏转?

标签 java android

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

这是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;
    }
}
}

最佳答案

要检查它们是否相互碰撞,只需检查它们中心之间的距离是否小于(半径*2)即可。为了做出漂亮的偏转,你必须做一些超出我能力范围的数学计算。 Here这是另一个答案。

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

关于java - 如何使两个球相撞并朝相反方向偏转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742180/

相关文章:

android - 分页库 - 网络 + db 的边界回调,API 获取页面和大小

Android:EditText 中的多行和无自动建议

java - 使用 Scanner 类获取输入时遇到问题

java - 我可以为动态数组列表父类(super class)类型对象创建子类对象吗?

java - 多个 fragment 需要位置更新

java - Firestore 文档引用数组

java - JSON 格式的字符串到字符串数组

java - 用if语句覆盖所有场景

java - Android工具栏到所有 Activity

请求网站时与发送的用户代理不同的 Javascript 用户代理(ajax)