0

I have already posted a question about this topic..but did not get the answer.I am doing a game.in that app i have to detect the collition of two objects in a canvas. The object class is pasted here.

     public class Droid {

private Bitmap bitmap;  // the actual bitmap
private int x;          // the X coordinate
private int y;          // the Y coordinate
private boolean touched;    // if droid is touched/picked up
private Speed speed;    // the speed with its directions

public Droid(Bitmap bitmap, int x, int y) {
    this.bitmap = bitmap;
    this.x = x;
    this.y = y;
    this.speed = new Speed();
}

public Bitmap getBitmap() {
    return bitmap;
}
public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}

public boolean isTouched() {
    return touched;
}

public void setTouched(boolean touched) {
    this.touched = touched;
}

public Speed getSpeed() {
    return speed;
}

public void setSpeed(Speed speed) {
    this.speed = speed;
}


public void draw(Canvas canvas) {
    canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
}

/**
 * Method which updates the droid's internal state every tick
 */
public void update() {
    if (!touched) {
        x += (speed.getXv() * speed.getxDirection()); 
        y += (speed.getYv() * speed.getyDirection());
    }
}


/**
 * Handles the {@link MotionEvent.ACTION_DOWN} event. If the event happens on the 
 * bitmap surface then the touched state is set to <code>true</code> otherwise to <code>false</code>
 * @param eventX - the event's X coordinate
 * @param eventY - the event's Y coordinate
 */
public void handleActionDown(int eventX, int eventY) {
    if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
        if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
            // droid touched
            setTouched(true);
        } else {
            setTouched(false);
        }
    } else {
        setTouched(false);
    }

}
}

I am creating 5 droid objects. 4 of them are continually moving on the canvas, and 1 is controlled by the user. I want to detect the event in which those 4 moving objects collide with user-controlled object. I have read many tutorials but not yet got a solution.please anybody help me...

2
  • nobody have answer for this question?????? :( Commented Nov 18, 2011 at 8:12
  • It would be better to use AndEngine for gaming. Commented Nov 18, 2011 at 8:48

3 Answers 3

1

It's much simpler to use AndEngine and let it simplify these tasks for you.

Sign up to request clarification or add additional context in comments.

Comments

1

In general, simplifying the collision detection by using an imprecise representation is generally a good idea, since it is easy to work with and implement, and the speed is generally fine. For such a solution, collision detection with axis-aligned bounding boxes, or possibly circles, should work fine. For more complicated solutions, polygon-based solutions are available, but they are harder to use.

If you need more precise collision detection, such as detecting collisions between two images, you need pixel-perfect collision detection. This is generally really hard to do efficiently, so using an existing library is recommended.

Since you work with Java, and you seem to use a bitmap, AndEngine as Reno recommended or PoxelColl should do the trick. If you need basic transformations like rotation and scaling, I recommend PoxelColl.

Comments

0

You should use a library that provides functionality for detecting sprite collisions. Try googling "android sprite collision".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.