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

时间:2013-12-23 11:02:06

标签: java android

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

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

1 个答案:

答案 0 :(得分:1)

要检查它们是否互相撞击,您可以检查它们的中心之间的距离是否小于(半径* 2)。为了做出一个很好的偏转,你将不得不做一些超出我的数学。 Here是另一个答案。

我已经用Google搜索了一下,并且显然在物理学中用于此问题的术语称为弹性碰撞。我找到了关于主题here的精彩教程(尤其是动态圆圈碰撞位)。此外,here是一个演示此内容的小程序。可以在[此处]

找到此源代码