androidピクセル衝突


ピクセル衝突:ゲーム開発では推奨されません.正確ですが、コードの効率が低下します.画素衝突検出に代わる方法があるため、多矩形、多円形の検出方式がある.次に、複数の長方形の衝突について説明する:名前の通り、複数の長方形の衝突領域を設定した例を作成する:MoreRectCollsionプロジェクト効果図:MySurfaceView.java
  • 1.
    
    2.package com.rectCollsion;
    
    3.
    
    4.import android.content.Context;
    
    5.import android.graphics.Canvas;
    
    6.import android.graphics.Color;
    
    7.import android.graphics.Paint;
    
    8.import android.graphics.Paint.Style;
    
    9.import android.graphics.Rect;
    
    10.import android.view.KeyEvent;
    
    11.import android.view.MotionEvent;
    
    12.import android.view.SurfaceHolder;
    
    13.import android.view.SurfaceHolder.Callback;
    
    14.import android.view.SurfaceView;
    
    15.
    
    16.public class MySurfaceView extends SurfaceView implements Callback,Runnable{
    
    17.        //    SurfaceView
    
    18.        private SurfaceHolder sfh;
    
    19.        //      
    
    20.        private Paint paint;
    
    21.        //      
    
    22.        private Thread th;
    
    23.        //        
    
    24.        private boolean flag;
    
    25.        //      
    
    26.        private Canvas canvas;
    
    27.        //       
    
    28.        private int screenW,screenH;
    
    29.        //           
    
    30.        private int rectX1 = 10,rectY1 = 10,rectW1 = 40,rectH1 = 40;
    
    31.        private int rectX2 = 100,rectY2 = 110,rectW2 = 40,rectH2 = 40;
    
    32.        //                 
    
    33.        private boolean isCollsion;
    
    34.        //              
    
    35.        private Rect clipRect1 = new Rect(0,0,15,15);
    
    36.        private Rect clipRect2 = new Rect(rectW1 - 15, rectH1 - 15,rectW1,rectH1);
    
    37.        private Rect[] arrayRect1 = new Rect[]{clipRect1,clipRect2};
    
    38.        //              
    
    39.        private Rect clipRect3 = new Rect(0,0,15,15);
    
    40.        private Rect clipRect4 = new Rect(rectW2 - 15,rectH2 - 15,rectW2,rectH2);
    
    41.        private Rect[] arrayRect2 = new Rect[]{clipRect3,clipRect4};
    
    42.        
    
    43.        /**
    
    44.         * SurfaceView     
    
    45.         */
    
    46.        public MySurfaceView(Context context){
    
    47.                super(context);
    
    48.                //  SurfaceHolder
    
    49.                sfh = this.getHolder();
    
    50.                // SurfaceView      
    
    51.                sfh.addCallback(this);
    
    52.                //      
    
    53.                paint = new Paint();
    
    54.                //         
    
    55.                paint.setColor(Color.WHITE);
    
    56.                //    
    
    57.                setFocusable(true);
    
    58.        }
    
    59.        @Override
    
    60.        public void surfaceCreated(SurfaceHolder holder) {
    
    61.                // TODO Auto-generated method stub
    
    62.                screenW = this.getWidth();
    
    63.                screenH = this.getHeight();
    
    64.                flag = true;
    
    65.                //    
    
    66.                th = new Thread(this);
    
    67.                //    
    
    68.                th.start();
    
    69.        }
    
    70.        
    
    71.        
    
    72.        /**
    
    73.         *     
    
    74.         */
    
    75.        public void myDraw(){
    
    76.                try {
    
    77.                        canvas = sfh.lockCanvas();
    
    78.                        if(canvas != null){
    
    79.                                //  
    
    80.                                canvas.drawColor(Color.BLACK);
    
    81.                                //         
    
    82.                                if(isCollsion){//    
    
    83.                                        paint.setColor(Color.RED);
    
    84.                                        paint.setTextSize(20);
    
    85.                                        canvas.drawText("Collision!", 0, 30, paint);
    
    86.                                        
    
    87.                                } else{//     
    
    88.                                        paint.setColor(Color.WHITE);
    
    89.                                }
    
    90.                                //      
    
    91.                                canvas.drawRect(rectX1, rectY1, rectX1 + rectW1, rectY1 + rectH1, paint);
    
    92.                                canvas.drawRect(rectX2, rectY2, rectX2 + rectW2, rectY2 + rectH2, paint);
    
    93.                                //----         ,        
    
    94.                                paint.setStyle(Style.STROKE);
    
    95.                                paint.setColor(Color.WHITE);
    
    96.                                //---               
    
    97.                                for(int i=0; i < arrayRect1.length; i++){
    
    98.                                        canvas.drawRect(arrayRect1[i].left + this.rectX1, arrayRect1[i].top + this.rectY1, 
    
    99.                                                        arrayRect1[i].right + this.rectX1, arrayRect1[i].bottom + this.rectY1, paint);
    
    100.                                }
    
    101.                                //---               
    
    102.                                for(int i=0;i < arrayRect2.length; i++){
    
    103.                                        canvas.drawRect(arrayRect2[i].left + this.rectX2, arrayRect2[i].top + this.rectY2,
    
    104.                                                        arrayRect2[i].right + this.rectX2,arrayRect2[i].bottom + this.rectY2, paint);
    
    105.                                }
    
    106.                                
    
    107.                        }
    
    108.                }catch(Exception e){
    
    109.                        
    
    110.                }finally{
    
    111.                        if(canvas != null)
    
    112.                                sfh.unlockCanvasAndPost(canvas);
    
    113.                }
    
    114.        }
    
    115.        /**
    
    116.         *       
    
    117.         */
    
    118.        @Override
    
    119.        public boolean onTouchEvent(MotionEvent event) {
    
    120.                // TODO Auto-generated method stub
    
    121.                //   1        (            )
    
    122.                rectX1 = (int) event.getX() - rectW1/2;
    
    123.                rectY1 = (int) event.getY() - rectH1/2;
    
    124.                //         
    
    125.                if(isCollsionWithRect(arrayRect1, arrayRect2)){
    
    126.                        isCollsion = true;//       
    
    127.                        //           
    
    128.                }else{
    
    129.                        isCollsion = false; //       
    
    130.                }
    
    131.                return true;
    
    132.        }
    
    133.        /**
    
    134.         *        
    
    135.         * @param left           X  
    
    136.         * @param top           Y  
    
    137.         * @param right          X  
    
    138.         * @param buttom          Y  
    
    139.         */
    
    140.        public boolean isCollsionWithRect(Rect[] rectArray, Rect[] rect2Array){
    
    141.                Rect rect = null;
    
    142.                Rect rect2 = null;
    
    143.                for(int i = 0; i < rectArray.length; i++){
    
    144.                        //                  
    
    145.                        rect = rectArray[i];
    
    146.                        //                     
    
    147.                        int x1 = rect.left + this.rectX1;
    
    148.                        int y1 = rect.top + this.rectY1;
    
    149.                        int w1 = rect.right - rect.left;
    
    150.                        int h1 = rect.bottom - rect.top;
    
    151.                        for(int j = 0; j < rect2Array.length; j++){
    
    152.                                //                  
    
    153.                                rect2 = rect2Array[i];
    
    154.                                //                   
    
    155.                                int x2 = rect2.left + this.rectX1;
    
    156.                                int y2 = rect2.top + this.rectY2;
    
    157.                                int w2 = rect2.right - rect2.left;
    
    158.                                int h2 = rect2.bottom - rect2.top;
    
    159.                                //                         
    
    160.                                if(x1 >= x2 && x1 > x2 + w2){
    
    161.                                }else if(x1 <= x2 && x1 + w1 <= x2) {
    
    162.                                }else if(y1 >=y2 && y1 >= y2 + h2){
    
    163.                                }else if(y1 <=y2 && y1 + h1 <= y2){
    
    164.                                }else {
    
    165.                                        //                              
    
    166.                                        return true;
    
    167.                                }
    
    168.                        }
    
    169.                }
    
    170.                return false;
    
    171.        }
    
    172.
    
    173.        
    
    174.        /**
    
    175.         *       
    
    176.         */
    
    177.        @Override
    
    178.        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
    179.                // TODO Auto-generated method stub
    
    180.                return super.onKeyDown(keyCode, event);
    
    181.        }
    
    182.        /**
    
    183.         *     
    
    184.         */
    
    185.        private void logic(){
    
    186.                
    
    187.        }
    
    188.        @Override
    
    189.        public void run() {
    
    190.                // TODO Auto-generated method stub
    
    191.                while(flag){
    
    192.                        long start = System.currentTimeMillis();
    
    193.                        myDraw();
    
    194.                        logic();
    
    195.                        long end = System.currentTimeMillis();
    
    196.                        try {
    
    197.                                if (end - start < 50) {
    
    198.                                        Thread.sleep(50 - (end - start));
    
    199.                                }
    
    200.                        } catch(InterruptedException e){
    
    201.                                e.printStackTrace();
    
    202.                        }
    
    203.                }
    
    204.                
    
    205.        }
    
    206.        /**
    
    207.         * Surfaceview        ,     
    
    208.         */
    
    209.        @Override
    
    210.        public void surfaceChanged(SurfaceHolder holder, int format, int width,
    
    211.                        int height) {
    
    212.                // TODO Auto-generated method stub
    
    213.                
    
    214.        }
    
    215.        /**
    
    216.         * Surfaceview    ,     
    
    217.         */
    
    218.        @Override
    
    219.        public void surfaceDestroyed(SurfaceHolder holder) {
    
    220.                // TODO Auto-generated method stub
    
    221.                
    
    222.        }
    
    223.}