Basic Coder
Basic Coder

Reputation: 11422

AndEngine: collision of two sprites

I am developing a small Android game. Before I started using AndEngine, I used the Canvas object and draw everything on it. For testing if two bitmaps collides with each other I checked if their bounding boxes overlap. In case of this, I checked if the overlapping rectangle of both bitmaps got one none transparent pixel in common. This method worked perfektly and I was able to detect pixel perfect collisions.

Because of some performance issues I started using AndEngine. Collision detection works quiet good but collision tests for two sprites definitly dosent work pixel perfect. The collision tests is "just" a bounding box test.

Upvotes: 4

Views: 5540

Answers (2)

Hitesh Bhalala
Hitesh Bhalala

Reputation: 2870

Below code for collision of two animated sprite works well for me in andEngine without using pixelPerfect class. It may help for you.

public boolean isCollides(AnimatedSprite animSprite1 ,AnimatedSprite animSprite2) throws Exception{


float diffX = Math.abs( (animSprite1.getX() +  animSprite1.getWidth()/2 )- 
             (animSprite2.getX() + animSprite2.getWidth()/2 ));
float diffY = Math.abs( (animSprite1.getY() +  animSprite1.getHeight()/2 )- 
             (animSprite2.getY() + animSprite2.getHeight()/2 ));

if(diffX < (animSprite1.getWidth()/2 + animSprite2.getWidth()/3) 
           && diffY < (animSprite1.getHeight()/2 + animSprite2.getHeight()/3)){

   return true;
}else
  return false;
}

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67286

Here is a nice example of pixel perfect Detection,

AndEngine - Pixel Perfect Detection

Upvotes: 5

Related Questions