Collision detection in Java/Android
<p>I want to check collision detection in a game (the code is below). I have a game class in which I am drawing bitmaps, making touch events and make an array list for moving a vehicle. In the vehicle class, I'm taking its getter setter bitmaps and the same in the frog class as vehicle.</p>

<p>I have a frog which is moving in all four directions. The vehicle is moving horizontally. Now if a frog touches a vehicle it should collide with it. What should I do to check collision by measuring height and width?</p>

<p>My code is:</p>

<p>For class <code>Game</code>:</p>

<pre><code>    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&lt;Vehicle&gt; vh = new ArrayList&lt;Vehicle&gt;();
        ArrayList&lt;VehicleReverse&gt; vhs = new ArrayList&lt;VehicleReverse&gt;();

        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&lt;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&gt;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()&lt;(250+(fg.getX()/2))&amp;&amp; (frog.getX()&gt;0)){
                            frog.frogShiftLeft();
                        //    fg1.setTouchedY(true);
                        }
                        if((int)event.getX()&gt;=(250+(fg.getX()/2))&amp;&amp;(frog.getX()&lt;300)){
                            frog.frogShiftRight();
                        }
                }
                    if(fg1.isTouched()){
                        //fg.setTouched(false);
                        if((int)event.getY()&lt;(380+(fg1.getY()/2))&amp;&amp; (frog.getY()&gt;0)&amp;&amp;((int)event.getX()&lt;100)){
                            frog.frogShiftUp();
                        //    fg.setTouched(true);
                        }
                        if((int)event.getY()&gt;=(380+(fg1.getY()/2))&amp;&amp;(frog.getY()&lt;400)&amp;&amp;((int)event.getX()&lt;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&lt;vh.size();j++){
                //for(int j=vh.size()-1; j&gt;0; j--){
                    Vehicle o=vh.get(j);
                    o.draw(canvas);
                }
                for(int k=vhs.size()-1; k&gt;0; k--){
                    VehicleReverse v=vhs.get(k);
                    v.draw(canvas);
                }
            }
        }
</code></pre>

<p>For the Frog class:</p>

<pre><code>    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;

        }
    }
</code></pre>

<p>And for the Vehicle class:</p>

<pre><code>    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&gt;=300){
                    x=-50;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            setX(x);
        }

        public void collision(){
            if(frog.getX() &lt;= x + getX() &amp;&amp; frog.getY() &lt;= 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();
        }
    }
</code></pre>
