5 Replies - 555 Views - Last Post: 08 August 2019 - 09:49 AM Rate Topic: -----

#1 tyses96   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11623
  • View blog
  • Posts: 19,754
  • 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)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15219
  • View blog
  • Posts: 60,921
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 tyses96   User is offline

  • New D.I.C Head

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

Re: My if statement is never true?

Posted 08 August 2019 - 09:37 AM

View Postmodi123_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
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,727
  • Joined: 20-September 08

Re: My if statement is never true?

Posted 08 August 2019 - 09:49 AM

Had your code been correct all you need:
return (firstItemX1 > secondItemX1 && firstItemX1 < secondItemX2) &&
				(firstItemX2 > secondItemX1 && firstItemX2 < secondItemX2) && 
				(firstItemY1 > secondItemY1 && firstItemY1 < secondItemY2) && 
				(firstItemY2 > secondItemY1 && firstItemY2 < secondItemY2));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1