首页 > 其他 > 详细

碰撞检测的Demo

时间:2014-08-12 00:22:53      阅读:342      评论:0      收藏:0      [点我收藏+]

看了李明华老师那本游戏入门, 碰撞检测哪个部分不知道为什么判断时候要多加上那几句,感觉不加上也是可以的呀???? 求指点

现在把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
            }
        }
        
    }
}

运行效果:

未发生碰撞,bubuko.com,布布扣发生碰撞:bubuko.com,布布扣

其中,在检测碰撞的代码部分,书本上面是这样子的(意思就是排除两个矩形不碰撞的情形,剩余的情况即发生了碰撞):bubuko.com,布布扣

但是我觉得只要这样写不就行了吗:????

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
                }
            }
            
        }
    }

运行效果:

bubuko.com,布布扣bubuko.com,布布扣

碰撞检测的Demo,布布扣,bubuko.com

碰撞检测的Demo

原文:http://www.cnblogs.com/tiejiangweigaibianercunzai/p/3905802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!