# What is wrong with my collision detection code?

I want to check collision detection between two bitmaps but I am not getting how to do it. I read several queries and tutorials but didnt get properly. I want to check by their width and height. I have implemented some logic also but not getting where I am doing wrong

public boolean collision(){
for(int i=0; i<9; i++){
for(int j=0; j<14; j++){
if(frog.getX() < x + getX() || frog.getY() < y + getY() || frog.getX() > x || frog.getY() > y){
//if(other.getWidth() > x && other.getWidth() < x + width && >other.getHeight() > y && other.getHeight() < y + height);
frog.frogShiftDown();
}
}
}
return true;
}

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


This is a problem with many, many sources across the web (if you want to do a search its called "bounding box collision detection" / "bounding box overlap")

With more background on what you are trying to do (i.e. are you detecting your collisions in a grid like tic-tac-toe, or are you doing them in a continuous space?) we can debug your code.

I'm going to describe this as pseudocode; I don't know what language you're using. If you understand the concepts as I explain them, you should be able to write the code.

Create a list called "cars".

Every time a car is created at the edge of the screen, put that car into "cars" array.

Every time a car is deleted at the edge of the screen, remove that car from your "cars" array.

frog.left = frog.x
frog.top = frog.y
frog.right = frog.x+frog.width
frog.bottom = frog.y+frog.height

For each car in cars
car.left = car.x
car.top = car.y
car.right = car.x+frog.width
car.bottom = car.y+frog.height
if ((car.left is between frog.left and frog.right) or (frog.left is between car.left and car.right))
horizontalOverlap = true
if ((car.top is between frog.top and frog.bottom) or (frog.top is between car.top and car.bottom))
verticalOverlap = true

if (horizontalOverlap and verticalOverlap)
explode frog
if lives == 0
end game
else
lives = lives - 1
place new frog back at start point (usually bottom of screen)


HTH. Draw your two boxes representing the current car and your frog, on a piece of paper, and you will see what is going on. The algorithm is checking overlap first along the x axis, then along the y axis. If there is overlap on both axes, then the car is on top of the frog (or vice versa), so the frog must explode.

• no i am making i a continuous space.. should i paste my code here??? – Naina Aug 29 '11 at 10:23
• I think you should edit your question to add detail on what is happening in your game (rather than pasting more code), and how you want the collision to resolve (eg. does character bounce off wall, do two objects bounce off one another, etc.). Then I will extend my answer to help you further. It's better for you to learn the concepts in use, than for me to just fix your code for you. The code involved for this is very simple, whereas your code isn't making any sense. – Engineer Aug 29 '11 at 10:31
• ok. what i am doing is I have a frog which is moving on button click in all 4 directions. frog has to cross the lane and to reach the other side. on lane cars are moving. my query is just if frog collides with the car i only get result that it is now collided. otherwise it keeps moving. – Naina Aug 29 '11 at 11:00
• thanx a lot :-) – Naina Aug 29 '11 at 12:07
• You're welcome. If you're happy with this answer, please check it as "accepted" (checkmark). – Engineer Aug 29 '11 at 12:11