首页 > 代码库 > 碰撞检测的Demo
碰撞检测的Demo
看了李明华老师那本游戏入门, 碰撞检测哪个部分不知道为什么判断时候要多加上那几句,感觉不加上也是可以的呀???? 求指点
现在把demo发上来:
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MySurfaceView(this)); }}
MySurfaceView.java
public class MySurfaceView extends SurfaceView implements Runnable, Callback { private Canvas canvas; private Paint paint; private boolean state; private boolean isCollision; private SurfaceHolder holder; private Thread mThread; private static final int MAX_TIME = 50; private int x1 =100,y1 =110,x2 = 100,y2 = 110; private int h1 = 40,w1 = 40,h2 = 40,w2 = 40; public MySurfaceView(Context context) { super(context); // TODO Auto-generated constructor stub holder = this.getHolder(); holder.addCallback(this); paint = new Paint(); paint.setColor(Color.BLUE); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub state = true; mThread = new Thread(this); mThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see android.view.View#onTouchEvent(android.view.MotionEvent) */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub x1 = (int)event.getX() - w1/2; y1 = (int)event.getY() - h1/2; if (isCollisionWithRect(x1,y1,w1,h1,x2,y2,w2,h2)) { isCollision = true; }else { isCollision = false; } return true; } private boolean isCollisionWithRect(int x12, int y12, int w12, int h12, int x22, int y22, int w22, int h22) { // TODO Auto-generated method stub if (x1 + w1 <= x2) { return false; }else if (x1>=x2 + w2){ return false; }else if (y1>=y2+h2) { return false; }else if (y1+h1<=y2) { return false; } return true; } public void mDraw(){ try { canvas = holder.lockCanvas(); if (canvas != null) { canvas.drawRGB(0, 0, 0); if (isCollision) { paint.setColor(Color.RED); paint.setTextSize(20); canvas.drawText("Collision!", 0, 50, paint); }else { paint.setColor(Color.WHITE); } canvas.drawRect(x1, y1, x1+w1, y1+h1, paint); canvas.drawRect(x2, y2, x2+w2, y2+h2, paint); } } catch (Exception e) { // TODO: handle exception }finally{ if (canvas!=null) { holder.unlockCanvasAndPost(canvas); } } } private void logic() { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub while (state) { long start = System.currentTimeMillis(); mDraw(); logic(); long end = System.currentTimeMillis(); try { if ((end - start) < MAX_TIME) { Thread.sleep(MAX_TIME - (end - start)); } } catch (Exception e) { // TODO: handle exception } } }}
运行效果:
未发生碰撞,发生碰撞:
其中,在检测碰撞的代码部分,书本上面是这样子的(意思就是排除两个矩形不碰撞的情形,剩余的情况即发生了碰撞):
但是我觉得只要这样写不就行了吗:????
private boolean isCollisionWithRect(int x12, int y12, int w12, int h12, int x22, int y22, int w22, int h22) { // TODO Auto-generated method stub if (x1 + w1 <= x2) { return false; }else if (x1>=x2 + w2){ return false; }else if (y1>=y2+h2) { return false; }else if (y1+h1<=y2) { return false; } return true; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
圆形区域的碰撞检测:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new CircleCollisionDemo(this)); }}
public class CircleCollisionDemo extends SurfaceView implements Runnable, Callback{ private Canvas canvas; private Paint paint; private boolean state; private boolean isCollision; private SurfaceHolder holder; private Thread mThread; private static final int MAX_TIME = 50; private int x1 =100,y1 =110,x2 = 100,y2 = 110; private int r1 = 40,r2 = 50; public CircleCollisionDemo(Context context) { super(context); // TODO Auto-generated constructor stub holder = this.getHolder(); holder.addCallback(this); paint = new Paint(); paint.setColor(Color.BLUE); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub state = true; mThread = new Thread(this); mThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see android.view.View#onTouchEvent(android.view.MotionEvent) */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub x1 = (int)event.getX(); y1 = (int)event.getY(); if (isCollisionWithCircle(x1,y1,x2,y2,r1,r2)) { isCollision = true; }else { isCollision = false; } return true; } private boolean isCollisionWithCircle(int x12, int y12, int x22, int y22, int r11, int r22) { // TODO Auto-generated method stub if (Math.sqrt(Math.pow(x12-x22, 2) + Math.pow(y12-y22, 2)) <= r1+r2) { return true; }else { return false; } //return false; } public void mDraw(){ try { canvas = holder.lockCanvas(); if (canvas != null) { canvas.drawRGB(0, 0, 0); if (isCollision) { paint.setColor(Color.RED); paint.setTextSize(20); canvas.drawText("Collision!", 0, 50, paint); }else { paint.setColor(Color.WHITE); } canvas.drawCircle(x1, y1, r1, paint); canvas.drawCircle(x2, y2, r2, paint); } } catch (Exception e) { // TODO: handle exception }finally{ if (canvas!=null) { holder.unlockCanvasAndPost(canvas); } } } private void logic() { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub while (state) { long start = System.currentTimeMillis(); mDraw(); logic(); long end = System.currentTimeMillis(); try { if ((end - start) < MAX_TIME) { Thread.sleep(MAX_TIME - (end - start)); } } catch (Exception e) { // TODO: handle exception } } } }
运行效果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。