Basic Collision Detection Problem

Problems with Canvas, OpenGL, etc...

Basic Collision Detection Problem

Postby PurpleNurple » Wed Jan 26, 2011 11:49 pm

Hi, I'm just starting out with Android and I've been playing around with graphics and gestures and stuff but now I'm trying to get col detection working but I'm having trouble.

I figured i'd start with basic bounding box collision detection using Rect.intersects(rect, rect).

Here's my method. GraphicObject is a class I defined that stores the bitmap, coordinates, speed etc.
I'm using a normal for loop to iterate through my ArrayList as I'm storing the position of each GraphicObject in
two other lists.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void detectCollisions() {
  2.                         for (int i=0; i <_graphics.size(); i++) {
  3.                                 GraphicObject graphicA = _graphics.get(i);
  4.                                 Rect boxA = new Rect(graphicA.getCoordinates().getX(), graphicA.getCoordinates().getY(),
  5.                                 graphicA.getCoordinates().getX()+graphicA.getGraphic().getWidth(),
  6.                                 graphicA.getCoordinates().getY()+graphicA.getGraphic().getHeight());
  8.                                 for (int j=0; j <_graphics.size(); j++) {
  9.                                         GraphicObject graphicB = _graphics.get(i);
  10.                                         Rect boxB = new Rect(graphicB.getCoordinates().getX(),
  11.                                         graphicB.getCoordinates().getY(),
  12.                                         graphicB.getCoordinates().getX()+graphicB.getGraphic().getWidth(),
  13.                                         graphicB.getCoordinates().getY()+graphicB.getGraphic().getHeight());
  15.                                         if (Rect.intersects(boxA,boxB)) {
  16.                                                 _collisionA.add(i);
  17.                                                 _collisionB.add(j);    
  18.                                         }
  19.                                 }
  20.                         }
Parsed in 0.012 seconds, using GeSHi

When I run the game it run's as it did before I implemented the collision detection, it seems to have no effect. I've made sure it's being called from the main loop (obviously :P ). I'm not really sure what the problem is, theres no compile-time or run-time errors or exceptions being thrown. It just doesn't work.

Any help is greatly appreciated :)
Posts: 4
Joined: Wed Jan 26, 2011 9:32 pm


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

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest