sid
sid

Reputation: 1116

Ball to Ball collision detection

I am trying to get the intersection point when the two balls collide so that i can re- bounce them in some other direction. i have tried various way to implement it but didn't got success.Can any one please tell me that is there any easy way to detect the collision and re bounce it.Here is my sample code where the balls collides with the screen of device and bounces back :-

class Ball extends ShapeDrawable {
        protected static final int BOUNDS_IN = 0;
        protected static final int BOUNDS_OUT_LEFT = 1;
        protected static final int BOUNDS_OUT_TOP = 2;
        protected static final int BOUNDS_OUT_RIGHT = 3;
        protected static final int BOUNDS_OUT_BOTTOM = 4;

        public float x;
        public float y;
        public int r;

        public float dx;
        public float dy;

        public Ball() {
            super(new OvalShape());

            getPaint().setColor(0xff888888);
        }

        public void reBounds() {
            setBounds((int) x - r, (int) y - r, (int) x + r, (int) y + r);
        }

        public void step(long deltaTimeMs) {
            // Log.i(TAG, "Time:" + deltaTimeMs);

            x += dx * deltaTimeMs / 100;
            y += dy * deltaTimeMs / 100;

            // Log.i(TAG, "x: "+x);
        }

        public int inBounds(int minx, int miny, int maxx, int maxy) {
            if (dx > 0 && x + r > maxx) {
                return BOUNDS_OUT_RIGHT;
            } else if (dx < 0 && x - r < minx) {
                return BOUNDS_OUT_LEFT;
            } else if (dy > 0 && y + r > maxy) {
                return BOUNDS_OUT_BOTTOM;
            } else if (dy < 0 && y - r < miny) {
                return BOUNDS_OUT_TOP;
            }
            return BOUNDS_IN;
        }
    };

    public CustomDrawableView(Context context) {
        super(context);

        Log.i(TAG, "Constructor Start");

        balls = new ArrayList<Ball>();

        update();

        Log.i(TAG, "Constructor End");
    }

    protected void onFirstDraw(Canvas canvas) {
        addRandomBalls(5);
    }

    protected void addRandomBalls(int number) {
        Ball mBall;

        Random r = new Random();

        for (int i = 0; i < number; i++) {
            mBall = new Ball();
            mBall.x = r.nextInt(MAX_X);
            mBall.y = r.nextInt(MAX_Y);
            mBall.r = 40;
            mBall.dx = r.nextInt(50) - 25;
            mBall.dy = r.nextInt(50) - 25;

            balls.add(mBall);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        MAX_X = MeasureSpec.getSize(widthMeasureSpec);
        MAX_Y = MeasureSpec.getSize(heightMeasureSpec);
    }

    protected void onDraw(Canvas canvas) {
        // Log.i(TAG, "onDraw Start");

        if (EXECUTE_ON_DRAW == false) {
            EXECUTE_ON_DRAW = true;

            onFirstDraw(canvas);
        }

        for (Ball b : balls) {
            // Log.i(TAG, "ball draw");
            b.reBounds();
            b.draw(canvas);
        }
    }

    public void update() {
        long now = System.currentTimeMillis();

        long difference = now - then;
        int inBound;
        for (Ball b : balls) {
            b.step(difference);
            inBound = b.inBounds(0, 0, MAX_X, MAX_Y);

            if (inBound == Ball.BOUNDS_OUT_BOTTOM
                    || inBound == Ball.BOUNDS_OUT_TOP) {
                b.dy *= -1;
            } else if (inBound == Ball.BOUNDS_OUT_LEFT
                    || inBound == Ball.BOUNDS_OUT_RIGHT) {
                b.dx *= -1;
            }
        }

        then = now;

        invalidate();

        mRedrawHandler.sleep(60);
    }

Upvotes: 0

Views: 314

Answers (1)

Vedad Zornic
Vedad Zornic

Reputation: 1

Well dunno if I got what you mean, but easiest and most successfull way to check collision between two circles is to check with radius. You gotta find x and y coordinates in center of each ball. It should look like this: centerX = x + widthOfBall/2 ; centerY =y + heigthOfBall/2; To check collisions between two balls all you need is to check if sum of two radii is higher then distance between two centers of balls. To check distance between two centers google distance between two points.

Upvotes: 0

Related Questions