# My if statement is never true?

Page 1 of 1

## 5 Replies - 475 Views - Last Post: 08 August 2019 - 09:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417023&amp;s=ecc5715fb0ff7561d7163c348938406c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tyses96

Reputation: 1
• Posts: 10
• Joined: 22-March 14

# My if statement is never true?

Posted 08 August 2019 - 09:22 AM

So ive been trying to design a collision detection system in Slick2d. I've attempted to create a "box" using coordinates and if statements in which if these 2 "boxes" meet, then collision returns true.

The issue is i've tried many iterations of this code and including nested if statements, checking for the x intercept, then the y in a nested if statement, but it either never returns true, or always returns true depending on the iteration, and I can't figure it out.

Any help would be appreciated, here is the if statement:

```public boolean collides( )
{
boolean hit = false;

if((firstItemX1 > secondItemX1 && firstItemX1 < secondItemX2) &&
(firstItemX2 > secondItemX1 && firstItemX2 < secondItemX2) &&
(firstItemY1 > secondItemY1 && firstItemY1 < secondItemY2) &&
(firstItemY2 > secondItemY1 && firstItemY2 < secondItemY2)) {
hit = true;
}

else hit = false;

return hit;
}

```

Here is the information that is passed into the if statement - where player.getPos is the first coordinate, and the player.getpos+player.getimage.getwidth/height is the other end of the line.

```cdFoodPlayer = new CollisionDetection(player.getPosX(), player.getPosX() + player.getPlayerImage().getWidth(),
player.getPosY(), player.getPosY() + player.getPlayerImage().getHeight(), food.getFoodX(),
food.getFoodX() + food.getFoodImage().getWidth(), food.getFoodY(), food.getFoodY() + food.getFoodImage().getHeight());

```

Is This A Good Question/Topic? 0

## Replies To: My if statement is never true?

### #2 tyses96

Reputation: 1
• Posts: 10
• Joined: 22-March 14

## Re: My if statement is never true?

Posted 08 August 2019 - 09:30 AM

```	public boolean collides( )
{
boolean hit = false;

if(		(firstItemX1 > secondItemX1 && firstItemX1 < secondItemX2)&&
(firstItemY1 > secondItemY1 && firstItemY1 < secondItemY2)||
(firstItemX2 > secondItemX1 && firstItemX2 < secondItemX2)&&
(firstItemY2 > secondItemY1 && firstItemY2 < secondItemY2)||
(firstItemX2 > secondItemX1 && firstItemX2 < secondItemX2)&&
(firstItemY1 > secondItemY1 && firstItemY1 < secondItemY2)||
(firstItemX1 > secondItemX1 && firstItemX2 < secondItemX2)&&
(firstItemY2 > secondItemY1 && firstItemY2 < secondItemY2))
{
hit = true;
}

else hit = false;

return hit;
}
}

```

This always returns true

### #3 jon.kiparsky

• Beginner

Reputation: 11604
• Posts: 19,726
• Joined: 19-March 11

## Re: My if statement is never true?

Posted 08 August 2019 - 09:32 AM

Your code requires that firstItem be completely contained by secondItem, which is probably not what you want. (all points if firstItem must be within the boundaries of secondItem)

### #4 modi123_1

• Suitor #2

Reputation: 15166
• Posts: 60,698
• Joined: 12-June 08

## Re: My if statement is never true?

Posted 08 August 2019 - 09:32 AM

Have you considered using the 'rectangle' class and using the 'intersects' method?
https://docs.oracle..../Rectangle.html

I would think about checking the easier format - if one rectangle is completely outside of another.

### #5 tyses96

Reputation: 1
• Posts: 10
• Joined: 22-March 14

## Re: My if statement is never true?

Posted 08 August 2019 - 09:37 AM

modi123_1, on 08 August 2019 - 09:32 AM, said:

Have you considered using the 'rectangle' class and using the 'intersects' method?
https://docs.oracle..../Rectangle.html

I would think about checking the easier format - if one rectangle is completely outside of another.

I think the Rectangle method looks promising, ill just draw invisible rectangles around them and check for intersects.

Thanks for the help

### #6 g00se

• D.I.C Lover

Reputation: 3639
• Posts: 16,717
• Joined: 20-September 08

## Re: My if statement is never true?

Posted 08 August 2019 - 09:49 AM

```return (firstItemX1 > secondItemX1 && firstItemX1 < secondItemX2) &&