Collision detection for rotated bitmaps on Android

I need perfect collision detection for my Android game. I wrote code to detect collisions with "normal" bitmaps (not rotated); works great. However, I do not get it for rotated bitmaps. Unfortunately, Java does not have a class for rotating rectangles, so I implemented it myself. It holds the position of the four corners relative to the screen and describes the exact location / layer of its bitmap; called "itemSurface". My plan for solving the detection problem was as follows:

  • Detection of intersection of different objects.
  • Calculation of the overlap area
  • Set these areas in relation to your excellent subject. Surface / Bitmap
  • Compare each individual pixel with the corresponding pixel in another bitmap

Well, I have problems with the first and second. Anyone have an idea or some kind of code? Maybe Java / Android already has code, and I just didn't find it.

+1
android detection
source share
1 answer

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.

0
source share

All Articles