Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've since just started coding for an android game using eclipse. I've read Beginning Android Game Programming and various other e-books. Recently, I've encountered a problem with collision between sprites. I've used this code template for my program.

package com.project.CAI_test;

import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
       // direction = 0 up, 1 left, 2 down, 3 right,
       // animation = 3 back, 1 left, 0 front, 2 right
       int[] DIRECTION_TO_ANIMATION_MAP = { 3, 1, 0, 2 };
       private static final int BMP_ROWS = 4;
       private static final int BMP_COLUMNS = 3;
       private static final int MAX_SPEED = 5;
       private GameView gameView;
       private Bitmap bmp;
       private int x = 0;
       private int y = 0;
       private int xSpeed;
       private int ySpeed;
       private int currentFrame = 0;
       private int width;
       private int height;

       public Sprite(GameView gameView, Bitmap bmp) {
             this.width = bmp.getWidth() / BMP_COLUMNS;
             this.height = bmp.getHeight() / BMP_ROWS;
             this.gameView = gameView;
             this.bmp = bmp;

             Random rnd = new Random();
             x = rnd.nextInt(gameView.getWidth() - width);
             y = rnd.nextInt(gameView.getHeight() - height);
             xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
             ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED;
       }

       private void update() {
             if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
                    xSpeed = -xSpeed;
             }
             x = x + xSpeed;
             if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
                    ySpeed = -ySpeed;
             }
             y = y + ySpeed;
             currentFrame = ++currentFrame % BMP_COLUMNS;
       }

       public void onDraw(Canvas canvas) {
             update();
             int srcX = currentFrame * width;
             int srcY = getAnimationRow() * height;
             Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
             Rect dst = new Rect(x, y, x + width, y + height);
             canvas.drawBitmap(bmp, src, dst, null);
       }

       private int getAnimationRow() {
             double dirDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
             int direction = (int) Math.round(dirDouble) % BMP_ROWS;
             return DIRECTION_TO_ANIMATION_MAP[direction];
       }

       public boolean isCollition(float x2, float y2) {
             return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
       }
}

The above code only detects collision between the generated sprites and the surface border. What I want to achieve is a collision detection that is controlled by the update function without having to change much of the coding. Probably several lines placed in the update() function. Tnx for any comment/suggestion.

share|improve this question
1  
Hi, and welcome to gamedev.stackexchange. This is a QA site, not a discussion forum, so you may wish to rephrase your question. See the FAQ: gamedev.stackexchange.com/faq –  Jari Komppa Sep 27 '12 at 7:21
add comment

2 Answers

I've used this template, too and I assigned my sprites to an array list. I've tried automating collision from the update () method in the sprite class itself. So far all I get is moving sprites bounding from the borders but not bounding when they collide with each other...

share|improve this answer
    
Hi aurezza, you should not post things like this as an answer, open a new question if there is something you need help with. –  Max Sep 28 '12 at 15:11
    
Sorry for that, already posted one but its on the stackoverflow website. –  aurezza Sep 28 '12 at 16:07
add comment

This is fairly simple. In this example the class we check for collision with is called Example, and that class has its own getBounds() method.

 public Rect getBounds(){
   return new Rect(x, y, x+width, y+height);
 }

 Public void checkCollision(){
    Rect mySprite = this.getBounds();
    Rect myCollisionObject = example.getBounds();
    if(mySprite.intersect(myCollisionObject)){
        //Todo code here
    }
  }

then you just put this method in your update-method.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.