I decided to ask for advice after having been stuck with this problem for half a day.
My goal is to get the blue rect to land on the orange rect.
I drew diagrams to illustrate my thought process on the coordinates of both objects(blue rect and orange rect).
I'm 100% sure, my coordinate on the diagrams are labeled correctly.
I figure if I evaluate each x coordinate of blue rect against the orange rect x coordinate as well applies this idea with the y coordinate, I would get somewhere. I was mistaken.
One thing to note: I am keeping the blue rect at the middle of the screen throughout the game so its coordinates won't change. But the blocks coordinate for drawing will change because it needs to take into account the camera system I have in place.
Where did I go wrong?
The first two conditions in the if statement use knowledge from the first diagram. The last two conditions use knowledge from the second diagram.
Here is my Java code:
if( (rectangle.getX() + idleRightAnim.getWidth()) >= (block.getX() - camera.getX())
&& (rectangle.getY() + idleRightAnim.getHeight()) >= (block.getY() - camera.getY())
&& rectangle.getX() <= (block.getX() - camera.getX()) + block.getWidth()
&& rectangle.getY() + idleRightAnim.getHeight() <= block.getY() - camera.getY()
)
{
System.out.println("collide");
}
FINAL EDIT: Spotted my mistake: the last condition should be: rectangle.getY() + idleRightAnim.getHeight() <= block.getY() - camera.getY() + block.getHeight()
I was forgot to add the block.getHeight(). No wonder...
I decided to post the solution here: This is for only for checking collision of edges case of the left side of the block.
// edge case for left side of block
if(rectangle.getX() + idleRightAnim.getWidth() >= block.getX() - camera.getX()
&& rectangle.getX() <= block.getX() - camera.getX() + block.getWidth()
&& rectangle.getY() + getHeight() >= block.getY() - camera.getY()
&& rectangle.getY() + getHeight() <= block.getY() - camera.getY() + block.getHeight()
)