Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it possible to detect when an object and a bitmap collide. I have an arraylist of sprites that I am shooting with an image. I tried using this method here but as soon as the bitmap appears the sprite disappears, this is in the Sprite class:

public boolean isCollision(Bitmap other)
{   
    if(other.getWidth() > x && other.getWidth() < x + width && >other.getHeight() > y && other.getHeight() < y + height);
    return true;
}
share|improve this question
add comment

2 Answers

You have two major, basic problems. First, this won't even compile because of this part: && >other.getHeight() > y && (notice the > before other). Secondly, this:

if(something);
return true;

will always return true. What you probably meant was this:

if(something)
    return true;
return false;

which, since something is just a boolean expression, is the same as

return something;

Both Eclipse and Netbeans have an "AutoFormat" option that would have made this error immediately obvious, while simultaneously fixing your horrendous formatting.


After you fix those, you still have problems with your logic, but those at least can be easily discovered with a debugger.

share|improve this answer
add comment

The following statement will always be false:

other.getWidth() > x && other.getWidth() < x

x can not be both greater and smaller than width.

As for the rest of your question, it seems to be two folded. You seem to be both having problem with checking the collision and with displaying it.

To solve your collision problems I would need to know more. What kind of collision do you want? (Should sprites collide with a background image that is divided into two colors, red for collidable?) And how are you handling sprite-sprite collisions currently?

To solve your displaying problem I would need to see more of your drawing functions.

(on an other note, who keeps upping every question asked on gamdev even if they are short unclear questions?)

share|improve this answer
    
I want to detect if they have collided, whether one is changing colors or if theirs a counter that increments every time they collide doesn't matter. As long as I can tell that the system recognizes the collision. I have bitmap images that are created and move randomly when the trackball is pressed. –  MarkEz Jun 25 '11 at 22:12
    
So the bitmaps should just be seen as rectangles? (With the color thingey, I meant for example that it should collide only with one color, which is often used for complex shapes) –  Roy T. Jun 26 '11 at 7:42
    
Yes, if possible –  MarkEz Jun 28 '11 at 21:31
1  
You overlooked a bit: other.getWidth() > x && other.getWidth() < x + width That's assuming the question wasn't edited without a history after you posted this. –  Jonathan Hobbs Jul 6 '11 at 13:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.