Kees Koenen
Kees Koenen

Reputation: 770

collision between 2 bitmaps

Working on game. I have a movable basket and a - for now - stationairy apple. In the Update step of the game, I check for collisions between the two. In the future, I want to add multiple apples, so I'm looking for the best way to detect collision between the basket and apple x.

Got code:

 public void update() {
 int basketcenterX = basket.getX() + (basket.getWidth() / 2);
 int basketcenterY = basket.getY() + (basket.getHeight() / 2);

 if (basketcenterX < apple.getX() + apple.getWidth() && basketcenterX > apple.getX())
     if ( basketcenterY < apple.getY() + apple.getHeight() && basketcenterY > apple.getY())

 {

     Log.d(TAG, "Collision detected!!");

 }

 Log.d(TAG, "Apple Safe!");}

It seems pretty laggy in the emulator, I'm worried that it will be too slow when I add more apple-instances. Any suggestions on how to improve this and / or how to revamp this to check multiple apples?

Upvotes: 1

Views: 1967

Answers (3)

poitroae
poitroae

Reputation: 21367

There are 2 main improvements for your code.

Access methods

Don't use getters and setters toO excessively. Those cannot be optimized in android in contrast to normal java applications. Just make public properties like

public float positionX;

and access them directly.

Precalculate

You don't want to move and have the bitmaps left top. You want to have its position and its position is its center. So wrap your bitmaps into a custom class GameObject or similar and precalculate the center. You just modify the center.

gameObjectPosition = basket.getX() + (basket.getWidth() / 2)

Store width and height of your bitmap in public fields of your GameObject class and access them directly.

/**
 A possible GameObject class.

**/
public class GameObject{
    private Bitmap bitmap;

    public float width, height;
    public float positionX;
    public float positionY;

    public GameObject(Bitmap bitmap){
            this.bitmap = bitmap;
            
            width = bitmap.getWidth(); 
            height = btimap.getHeight(); 
    }

    public void draw(...){
            bitmap.draw(positionX-width/2, positionY-height/2);
    }

}

If you are still concerned about performance, you also may precalculate the width/2 and height/2.

Upvotes: 4

Joqus
Joqus

Reputation: 585

I don't know if this is faster but instead of doing this (considering the answer of Michael):

if (basketcenterX < apple.getX() + apple.getWidth() && basketcenterX > apple.getX())
     if ( basketcenterY < apple.getY() + apple.getHeight() && basketcenterY > apple.getY())

you could use the concept of vectors like this:

calculate the vector distance between the two points

distanceVectorX = GameObjectBasket.positionX - GameObjectApple.positionX;
distanceVectorY = GameObjectBasket.positionY - GameObjectApple.positionY;

And then calculate the size of this vector that is the distance between the two points

distance = Math.sqrt(Math.pow(distanceVectorX, 2) + Math.pow(distanceVectorY, 2))

Now you can test this in your device to see if this operation may be faster or not.

Upvotes: 0

Eric
Eric

Reputation: 1775

Agree with what Michael said. Also don't test it on the emulator as it's too slow for the game developers, grab a device instead.

Upvotes: 1

Related Questions