Bitmap collison detection/drag & drop for game

Problems with Canvas, OpenGL, etc...

Bitmap collison detection/drag & drop for game

Postby saps1727 » Thu Jul 08, 2010 9:58 pm

Hey guys I am writing a game that uses bitmaps and a canvas in 2D. I am implementing a "drag and drop" feature which is a big part of the game design.

What I need help on is on the "drop" side of my design. When a touch ACTION_UP occurs, I need to recognize where on my canvas, a.k.a game board, i dropped the piece. Right now my background is drawn first as bitmap and all pieces on are drawn subsequently.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         canvas.drawBitmap(BitmapFactory.decodeResource(
  2.                         getResources(), R.drawable.table), 0, 0, null);
  3.         canvas.drawBitmap(chipsHolder[0].getBitmap(), chipsHolder[0].getX(),chipsHolder[0].getY(), null);
  4.         canvas.drawBitmap(chipsHolder[1].getBitmap(), chipsHolder[1].getX(),chipsHolder[1].getY(), null);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


...and so on.

I have the touch events working as well. They detect placement by matching x-values of the "piece" and a specific location, but I cannot get the inside if loop to work correctly. No matter what x-value on the canvas, the inside if loop always equates to true.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  public void checkPlacement(Chip chip)
  3.     {
  4.         if(chip.getY() <= 180 && chip.getY() >= 150)
  5.         {
  6.                 if(chip.getX() >= 25 && chip.getX() <= 100);
  7.                 {
  8.                         Toast.makeText(getContext(), "green", Toast.LENGTH_SHORT).show();
  9.                 }
  10.  
  11.         }
  12.  
  13.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I have read stuff on collision detection by coordinates, but my game board is comprised of many "areas" that a piece can land on. There has to be a more efficient way of detecting specific placement?

Thanks for the help.
saps1727
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jun 24, 2010 4:58 am

Top

Re: Bitmap collison detection/drag & drop for game

Postby mark@project8games.com » Thu Jul 08, 2010 10:45 pm

I'm having a bit of a hard time understanding your exact problem but what I gathered you're creating a board game where you can drag and drop pieces (like chess, checkers, etc) on predefined areas of the gameboard (can be the tiles of a chess/checkers board for instance). Is that correct?

If this is the case, here are my suggestions(this all assumes you're using AABB's)
1)Split the screen into 4 quads
2)Detect if the piece lies in one of those quads
3)Taking the quad that was detected as containing the piece, hit-test all the "areas" in that quad against your piece

Assuming you have an 8x8 board, you go from potentially worse case 64 iterations down to 20 using this method. You can further subdivide your quad in 4, but the gains aren't as great. I think it would be about 16 iterations worse case screnario.
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Bitmap collison detection/drag & drop for game

Postby saps1727 » Thu Jul 08, 2010 11:08 pm

Yes you are correct. I understand your logic, but my game board has certain "areas" that would overlap multiple quads. In the code above, I show one "area" given by its y-value and x-values. My problem now is that only the outside if loop is working. When I drop a piece within the y-range, it works fine, but it doesn't take into account the x-range of the area.

For example, how could I check my piece landed on an easy area like this...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. canvas.drawRect(30, 30, 60, 60);
  2.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
saps1727
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jun 24, 2010 4:58 am

Re: Bitmap collison detection/drag & drop for game

Postby mark@project8games.com » Thu Jul 08, 2010 11:54 pm

So I stubbed this bit of code out just now. I use something very similar to determine if a user clicked on a button. Couple of things to remember. First, make sure your coordinate system is based on (0,0) being the UPPER-LEFT corner. Although, since you're having a problem in the X coordinate, I dont suspect that this is the problem.

Like I said earlier though, I'm still confused as to why you're having the problem you are. If the below isn't helpful you should post some more of the code up.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         static Area[] areas = new Area[1];
  2.  
  3.         static {
  4.                 areas[0] = new Area(30, 30, 60, 60);
  5.         }
  6.  
  7.         public void onTouchDownEvent(MotionEvent event) {
  8.                 //get x/y of event
  9.                 float touchX = event.getRawX();
  10.                 float touchY = event.getRawY();
  11.                 //is it in a bounds
  12.                 for (Area area : areas) {
  13.                         if (area.contains((int) touchX, (int) touchY)) {
  14.                                 //inside of an area.
  15.                                 //do some stuff
  16.                         }
  17.                 }
  18.         }
  19.  
  20.         public class Area {
  21.                 int x1, y1, x2, y2;
  22.  
  23.                 public Area(int x1, int y1, int x2, int y2) {
  24.                         this.x1 = x1;
  25.                         this.y1 = y1;
  26.                         this.x2 = x2;
  27.                         this.y2 = y2;
  28.                 }
  29.  
  30.                 public boolean contains(int x, int y) {
  31.                         return
  32.                                         x >= x1 && y >= y1 &&
  33.                                                         x <= x2 && y <= y2;
  34.                 }
  35.  
  36.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Bitmap collison detection/drag & drop for game

Postby saps1727 » Fri Jul 09, 2010 5:17 pm

Hey Mark, thanks for the help. Your for loop seems to be working well. I don't know why my nested if loop wasn't checking the x-values, but what you gave me works. Appreciate the help.
saps1727
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jun 24, 2010 4:58 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 5 guests