0
votes

im trying to make a game in which collision detection (between ball & bar) per pixel is required. here is my logic and logcat displays x-axis cordinated of the ball. ball location registeration is not continuous pixel wise.

scene 1 - ball hitting bar on right

scene 2 - ball hitting bar on top

scene 3 - ball hitting bar on bottom

scene 4 - ball hitting bar on left

@Override

public void onUpdate(final float pSecondsElapsed) {

int collisionDetection(final Ball ball, final Sprite bar){

//scene 1
if((ball.getX() == bar.getX()) && ((ball.getY()+(ball.getHeight()/2)) >= bar.getY()) && ((ball.getY()+(ball.getHeight()/2)) >= (bar.getY()+bar.getHeight()))){

        Ball.mPhysicsHandler.setVelocityX(-(Ball.mPhysicsHandler.getVelocityX()));
        return 1;
    }
    //scene 4
    else if(((ball.getX()+ball.getWidth()) == bar.getX()-1)){// && ((ball.getY()+(ball.getHeight()/2)) >= bar.getY()) && ((ball.getY()+(ball.getHeight()/2)) >= (bar.getY()+bar.getHeight()))){
        changeToBlue();
        Ball.mPhysicsHandler.setVelocityX(-(Ball.mPhysicsHandler.getVelocityX()));
        return 1;
    }
    //scene 2
    else if(((ball.getY()+ball.getHeight()) == bar.getY()) && (ball.getX()+(ball.getWidth()/2) >= bar.getX()) && (ball.getX()+(ball.getWidth()/2) <= (bar.getX()+bar.getWidth()))){

        Ball.mPhysicsHandler.setVelocityY(-(Ball.mPhysicsHandler.getVelocityY()));
        return 2;
    }
    //scene 3
    else if((ball.getY() == (bar.getY()+bar.getHeight())) && (ball.getX()+(ball.getWidth()/2) >= bar.getX()) && (ball.getX()+(ball.getWidth()/2) <= (bar.getX()+bar.getWidth()))){

        Ball.mPhysicsHandler.setVelocityY(-(Ball.mPhysicsHandler.getVelocityY()));
        return 2;
    }
    else{
        return 0;
    }
}

}

Logcat :

08-12 18:19:32.710  32531-32560/? A/GameCore: 860

08-12 18:19:32.720  32531-32560/? A/GameCore: 867

08-12 18:19:32.740  32531-32560/? A/GameCore: 874

08-12 18:19:32.760  32531-32560/? A/GameCore: 880

08-12 18:19:32.770  32531-32560/? A/GameCore: 887

08-12 18:19:32.790  32531-32560/? A/GameCore: 894

08-12 18:19:32.810  32531-32560/? A/GameCore: 900

08-12 18:19:32.820  32531-32560/? A/GameCore: 907

08-12 18:19:32.840  32531-32560/? A/GameCore: 914

08-12 18:19:32.860  32531-32560/? A/GameCore: 921

08-12 18:19:32.870  32531-32560/? A/GameCore: 927

08-12 18:19:32.890  32531-32560/? A/GameCore: 934

08-12 18:19:32.910  32531-32560/? A/GameCore: 941

08-12 18:19:32.930  32531-32560/? A/GameCore: 947

08-12 18:19:32.940  32531-32560/? A/GameCore: 955

08-12 18:19:32.960  32531-32560/? A/GameCore: 961

08-12 18:19:32.970  32531-32560/? A/GameCore: 968

08-12 18:19:32.990  32531-32560/? A/GameCore: 974

08-12 18:19:33.010  32531-32560/? A/GameCore: 982

08-12 18:19:33.020  32531-32560/? A/GameCore: 987

08-12 18:19:33.040  32531-32560/? A/GameCore: 994

08-12 18:19:33.060  32531-32560/? A/GameCore: 1000

08-12 18:19:33.070  32531-32560/? A/GameCore: 1007

08-12 18:19:33.090  32531-32560/? A/GameCore: 1014

08-12 18:19:33.110  32531-32560/? A/GameCore: 1020

08-12 18:19:33.120  32531-32560/? A/GameCore: 1027

08-12 18:19:33.140  32531-32560/? A/GameCore: 1034

08-12 18:19:33.160  32531-32560/? A/GameCore: 1042

08-12 18:19:33.170  32531-32560/? A/GameCore: 1047

08-12 18:19:33.190  32531-32560/? A/GameCore: 1054

08-12 18:19:33.210  32531-32560/? A/GameCore: 1061

08-12 18:19:33.220  32531-32560/? A/GameCore: 1067

08-12 18:19:33.241  32531-32560/? A/GameCore: 1074

08-12 18:19:33.261  32531-32560/? A/GameCore: 1080

08-12 18:19:33.271  32531-32560/? A/GameCore: 1087

08-12 18:19:33.291  32531-32560/? A/GameCore: 1094

08-12 18:19:33.311  32531-32560/? A/GameCore: 1100

08-12 18:19:33.321  32531-32560/? A/GameCore: 1107

08-12 18:19:33.341  32531-32560/? A/GameCore: 1114

08-12 18:19:33.361  32531-32560/? A/GameCore: 1120

08-12 18:19:33.371  32531-32560/? A/GameCore: 1127

08-12 18:19:33.391  32531-32560/? A/GameCore: 1134

08-12 18:19:33.411  32531-32560/? A/GameCore: 1140

08-12 18:19:33.421  32531-32560/? A/GameCore: 1147

08-12 18:19:33.441  32531-32560/? A/GameCore: 1154

08-12 18:19:33.461  32531-32560/? A/GameCore: 1160

08-12 18:19:33.471  32531-32560/? A/GameCore: 1167
1

1 Answers

0
votes

It looks like you are not performing "pixel perfect" collision, rather a circle/line collision.

The simplest way to find a circle line collision is to find the closest point on the line to the circle and then check the distance from that point to the circle. There is a good tutorial on that here: http://plasticsturgeon.com/2011/03/actionscript-collision-detection-2-circle-line-collision-detection/ Its written in actionscript 3, but it close enough to java that you should have no problem following. And the Math from one to another is identical.

Another approach, particularly if you need continuous collision detection, is to use Box2D. For this, use the Box2DExtension for andengine. https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension