Java 如何使两个球在相反方向碰撞和偏转?

Java 如何使两个球在相反方向碰撞和偏转?,java,android,Java,Android,我已经编写了矩形(画布)内两个球的运动代码。当球击中矩形的顶部、底部、左侧或右侧时,球会朝相反方向偏转。但是,我试图让球相互碰撞并朝相反的方向偏转,但没有成功。我搜索了很多网站和文章,但都没有找到。有人能帮忙吗 这是MainActivity.java package com.example.movements; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedIn

我已经编写了矩形(画布)内两个球的运动代码。当球击中矩形的顶部、底部、左侧或右侧时,球会朝相反方向偏转。但是,我试图让球相互碰撞并朝相反的方向偏转,但没有成功。我搜索了很多网站和文章,但都没有找到。有人能帮忙吗

这是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) {
        }
    }
}
}
package com.example.movements;
公共类MovementView扩展了SurfaceView实现了SurfaceHolder.Callback{
私有int xPos,xPos1;
私人国际石油公司,石油公司1;
私有int xVel,xVel1;
伊维尔私人酒店,伊维尔1号;
私有整数宽度;
私人内部高度;
私人国际循环,循环1;
私人油漆油漆,油漆1;
更新读取更新读取;
公共移动视图(上下文){
超级(上下文);
getHolder().addCallback(此);
环磷酰胺=10;
circlePaint=新油漆();
circlePaint.setColor(颜色:蓝色);
xVel=10;
yVel=10;
圆环病毒1=10;
CirclePaint 1=新油漆();
CirclePaint 1.setColor(颜色为洋红色);
xVel1=11;
yVel1=11;
}
@凌驾
受保护的void onDraw(画布){
画布。drawColor(颜色。白色);
画布.画圈(xPos、YPO、circleRadius、circlePaint);
画布.画圈(xPos1,yPos1,circleRadius1,CirclePaint 1);
}
公共物理学(){
xPos+=xVel;
yPos+=yVel;
if(yPos-环线<0 | | yPos+环线>高度){
if(yPos-环类<0){
yPos=环状;
}否则{
yPos=高度-圆环;
}
yVel*=-1;
}
if(xPos-circleRadius<0 | | xPos+circleRadius>宽度){
if(xPos-循环小于0){
xPos=循环;
}否则{
xPos=宽度-圆环;
}
xVel*=-1;
}
xPos1+=xVel1;
yPos1+=yVel1;
if(yPos1-圆环半径1<0 | | yPos1+圆环半径1>高度){
if(yPos1-环脲类1<0){
yPos1=circleRadius1;
}否则{
yPos1=高度-圈度1;
}
yVel1*=-1;
}
if(xPos1-圆环半径1<0 | | xPos1+圆环半径1>宽度){
if(xPos1-circleRadius1<0){
xPos1=环类1;
}否则{
xPos1=宽度-圆环半径1;
}
xVel1*=-1;
}
}
已创建的公共空白表面(表面持有人){
Rect surfaceFrame=holder.getSurfaceFrame();
宽度=surfaceFrame.width();
高度=surfaceFrame.height();
xPos=宽度/2;
yPos=环状;
xPos1=宽度/2;
yPos1=circleRadius1;
updateThread=新的updateThread(此);
updateThread.setRunning(true);
updateThread.start();
}
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
}
公共空间表面覆盖(表面覆盖物持有人){
布尔重试=真;
updateThread.setRunning(false);
while(重试){
试一试{
updateThread.join();
重试=错误;
}捕捉(中断异常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;
    }
}
}
package com.example.movements;
导入android.view.SurfaceHolder;
公共类UpdateThread扩展线程{
私人时间长;
私人最终整数fps=20;
私有布尔函数toRun=false;
私有移动视图移动视图;
私人地勤人员地勤人员;
公共更新读取(移动视图或移动视图){
movementView=rMovementView;
surfaceHolder=movementView.getHolder();
}
公共void setRunning(布尔运行){
toRun=运行;
}
@凌驾
公开募捐{
帆布c;
while(托伦){
长cTime=System.currentTimeMillis();

如果((cTime-time)要检查它们是否相互撞击,你可以只检查它们中心之间的距离是否小于(半径*2)。要产生良好的偏转,你必须做一些我无法理解的数学。这是另一个答案


我在谷歌上搜索了一下,很明显,物理学中用于这个问题的术语叫做弹性碰撞。我找到了一个关于这个主题的很好的教程(特别是动态圆碰撞位)。还有一个小程序演示了这一点。可以在[这里]找到这个问题的源代码

谷歌搜索恢复系数或弹性碰撞我添加了一些解释