Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Collision detection in Java/Android

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.

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?

My code is:

For class 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);
                }
            }
        }

For the Frog class:

    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;

        }
    }

And for the Vehicle class:

    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();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know what it is like about dealing with collision detection. If you want cheap advice here it is: This will work with 2d games. Set each object a Rectangle for its boundries. Set rectangle x and y as the top left positon and then the width the width of your object set the height as the height of your object. Do the same thing with the other objects.

In the rectangle class there is a method called intersect or something like that. Do rect1.isIntersecting(rect2); in the update method. Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...