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