I understand that you want to detect a collision between the rectangles (rotated in different ways). You do not need to calculate the overlapping area. Moreover, comparing each pixel will be inefficient.
Implement the static boolean isCollision function that tells you that there is a collision between one rectangle and the other. Before you take a piece of paper, do some geometry to find out the exact formulas. For performance reasons, do not wrap the rectangle in some Rectangle class, just use primitive types such as doubles, etc.
Then (pseudo code):
for (every rectangle a) for (every rectangle b) if (a != b && isCollision(a, b)) bounce(a, b)
This is O (n ^ 2), where n is the number of rectangles. There are better algorithms if you need better performance. The bounce function changes the vectors of moving rectangles to simulate a collision. If the weight of the objects was the same (you can aproximate the weight with the size of the rectangles), you just need to change the two velocity vectors.
In order to correctly drop elements, you may need to save the auxiliary table boolean alreadyBounced[][] to determine which rectangles do not need to change their vectors after a bounce (collision), since they have already bounced.
One more tip:
If you are making a game for Android, you need to make sure that you do not allocate memory during the game, because it will run GC faster, which takes a lot of time and slows down your game. I recommend that you watch this video and related videos. Good luck.
Adam stelmaszczyk
source share