2011-08-29 77 views
-2

我想檢查遊戲中的碰撞檢測(代碼如下)。我有一個遊戲課,我正在繪製位圖,進行觸摸事件並製作一個移動車輛的數組列表。在車輛類中,我正在使用它的getter setter位圖,並且在青蛙類中使用與車輛相同的位圖。Java/Android中的碰撞檢測

我有一隻正在四個方向移動的青蛙。車輛正在水平移動。現在,如果青蛙碰到車輛,它應該與它碰撞。我應該怎麼做來通過測量高度和寬度來檢查碰撞?

我的代碼是:

對於Game類:

public class Game extends SurfaceView implements SurfaceHolder.Callback { 
     private Frog frog; 
     private FrogControl fg; 
     private FrogControlY fg1; 
     private MainThread thread; 
     //private Vehicle vh; 

     ImageView imgview = (ImageView)findViewById(R.id.imageview); 
     ImageView imageArr[] = new ImageView[20]; 
     int id[] = {R.drawable.audi_r8, R.drawable.bike, R.drawable.bus, R.drawable.car, 
        R.drawable.chrysler, R.drawable.cruise_bike}; 

     ArrayList<Vehicle> vh = new ArrayList<Vehicle>(); 
     ArrayList<VehicleReverse> vhs = new ArrayList<VehicleReverse>(); 

     public Game(Context context) { 
      super(context); 
       getHolder().addCallback(this); 
       frog= new Frog(BitmapFactory.decodeResource(getResources(), R.drawable.frog),100,400); 
       fg= new FrogControl(BitmapFactory.decodeResource(getResources(), R.drawable.arrow),250,400); 
       fg1=new FrogControlY(BitmapFactory.decodeResource(getResources(), R.drawable.arrow),0,400); 
       int noOfCar=(int)Math.random()*5; 
       int totalCar=3+noOfCar; 
       for(int j=0;j<totalCar; j++){ 
        int y=(int)(Math.random()*3); 
        int[] LaneSelection = {50,150,250}; 
        int yValue=LaneSelection[y]; 
        int x=0; 
        vh.add(new Vehicle(BitmapFactory.decodeResource(getResources(), id[y]),x,yValue)); 
       } 

       for(int k=totalCar-1; k>0; k--){ 
        int y=(int)(Math.random()*3); 
        int[] LaneSelection = {100,200,300}; 
        int yValue=LaneSelection[y]; 
        int x=300; 
        vhs.add(new VehicleReverse(BitmapFactory.decodeResource(getResources(), id[y]),x,yValue)); 
       } 
       thread=new MainThread(getHolder(),this); 
       setFocusable(true); 
      } 

      @Override 
      public void surfaceChanged(SurfaceHolder holder, int format, int width, 
             int height) { 
       // TODO Nothing 
      } 

      @Override 
      public void surfaceCreated(SurfaceHolder holder) { 
       thread.setRunning(true); 
       thread.start(); 
      } 

      @Override 
      public void surfaceDestroyed(SurfaceHolder holder) { 
       boolean retry=true; 
       thread.setRunning(false); 
       while(retry){ 
        try{ 
         thread.join(); 
         retry=false; 
        }catch(Exception e){ 

        } 
       } 
      } 

      @Override 
      public boolean onTouchEvent(MotionEvent event){ 
       if(event.getAction()==MotionEvent.ACTION_DOWN){ 
       // vh.carmove(); 
        fg.handleAction((int)event.getX(), (int)event.getY()); 
        fg1.handleActionY((int)event.getX(), (int)event.getY()); 
        if(fg.isTouched()){ 
         //fg1.setTouchedY(false); 
         if((int)event.getX()<(250+(fg.getX()/2))&& (frog.getX()>0)){ 
          frog.frogShiftLeft(); 
         // fg1.setTouchedY(true); 
         } 
         if((int)event.getX()>=(250+(fg.getX()/2))&&(frog.getX()<300)){ 
          frog.frogShiftRight(); 
         } 
       } 
        if(fg1.isTouched()){ 
         //fg.setTouched(false); 
         if((int)event.getY()<(380+(fg1.getY()/2))&& (frog.getY()>0)&&((int)event.getX()<100)){ 
          frog.frogShiftUp(); 
         // fg.setTouched(true); 
         } 
         if((int)event.getY()>=(380+(fg1.getY()/2))&&(frog.getY()<400)&&((int)event.getX()<100)){ 
          frog.frogShiftDown(); 
         } 
        } 
       } 

       if(event.getAction()==MotionEvent.ACTION_MOVE){} 

       if(event.getAction()==MotionEvent.ACTION_UP){ 
        if(frog.isTouched()){ 
         frog.setTouched(false); 
        } 
       } 
       return true; 
      } 

      public void render(Canvas canvas){ 
       canvas.drawColor(Color.BLACK); 
       fg.draw(canvas); 
       fg1.draw(canvas); 
       frog.draw(canvas); 
       for(int j=0;j<vh.size();j++){ 
       //for(int j=vh.size()-1; j>0; j--){ 
        Vehicle o=vh.get(j); 
        o.draw(canvas); 
       } 
       for(int k=vhs.size()-1; k>0; k--){ 
        VehicleReverse v=vhs.get(k); 
        v.draw(canvas); 
       } 
      } 
     } 

對於青蛙類:

import android.graphics.Bitmap; 
    import android.graphics.Canvas; 

    public class Frog { 
     private Bitmap bmp; 
     private int x; 
     private int y; 
     private boolean touched; 

     public Frog(Bitmap bitmap,int x,int y){ 
      this.bmp=bitmap; 
      this.x=x; 
      this.y=y; 
     } 

     public Bitmap getBmp() { 
      return bmp; 
     } 

     public void setBmp(Bitmap bmp) { 
      this.bmp = bmp; 
     } 

     public int getX() { 
      return x; 
     } 

     public void setX(int x) { 
      this.x = x; 
     } 

     public int getY() { 
      return y; 
     } 

     public void setY(int y) { 
      this.y = y; 
     } 

     public boolean isTouched(){ 
      return touched; 
     } 

     public void setTouched(boolean touched){ 
      this.touched=touched; 
     } 

     public void draw(Canvas canvas){ 
     canvas.drawBitmap(bmp, x, y,null); 
     } 

     public void frogShiftRight(){ 
      this.x=this.x+20; 

     } 
     public void frogShiftLeft(){ 
      this.x=this.x-20; 

     } 
     public void frogShiftUp(){ 
      this.y=this.y-20; 

     } 
     public void frogShiftDown(){ 
      this.y=this.y+20; 

     } 
    } 

對於車輛的類:

public class Vehicle extends Thread implements Runnable{ 
     private Bitmap bmp; 
     private static int x; 
     private int y; 
     Frog frog; 
     Vehicle vehicle; 

     public Vehicle(Bitmap bitmap,int x,int y){ 
      this.bmp=bitmap; 
      this.x=x; 
      this.y=y; 
     } 

     public Bitmap getBmp() { 
      return bmp; 
     } 

     public void setBmp(Bitmap bmp) { 
      this.bmp = bmp; 
     } 

     public int getX() { 
      return x; 
     } 

     public void setX(int x) { 
      this.x = x; 
     } 

     public int getY() { 
      return y; 
     } 

     public void setY(int y) { 
      this.y = y; 
     } 

     public void carmove(){ 
      try { 
       x = x + 5; 
       if(x>=300){ 
        x=-50; 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      setX(x); 
     } 

     public void collision(){ 
      if(frog.getX() <= x + getX() && frog.getY() <= y + getY()){ 
       Toast.makeText(getApplicationContext(), "Collided", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     private Context getApplicationContext() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     public void draw(Canvas canvas){ 
      canvas.drawBitmap(bmp, x, y,null); 
      carmove(); 
      collision(); 
     } 
    } 
+0

有什麼建議?這是我的第一場比賽。所以我對它沒有太多的瞭解 – Naina

+0

非常無益的評論。這是非常具體的:他想知道在Android下碰撞檢測的最佳方法。 Android框架中的庫和實用程序可能會提供幫助。兩個很好的例子,無論如何說,但無論如何說。 – RichieHH

回答

2

我知道處理碰撞檢測是什麼樣子。如果你想要便宜的建議,這是: 這將適用於2D遊戲。爲每個對象設置一個矩形的邊界。 將矩形x和y設置爲左上位置,然後將對象的寬度設置爲對象的高度 。對其他對象做同樣的事情。

在矩形類中有一個叫做相交或類似的方法。做rect1.isIntersecting(rect2); 在更新方法中。 希望這會有所幫助

0

您可以使用矩形碰撞檢查。如果(frogLeftvehicleLeft和frogTopvehicleTop),那麼你知道它們必須重疊。您可以添加頂部,右側,左側和底部屬性的方法,以使代碼更加美觀。

EX:

int right() 
{ 
    return this.x+this.width; 
} 

或者你可以讓你的對象從Java的rectangle2D.double對象繼承並使用內置在路口功能:http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Rectangle2D.html#intersects(double, double, double, double)

另外,我對用Java編寫的2D碰撞檢測教程張貼在這裏:http://tylergriffin.me/collision_detection/

0

,如果你正在處理rectangles那麼它可以幫助你

/** 
* Check if two rectangles collide 
* x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle 
* x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle 
*/ 
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) 
{ 
    return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2); 
}