AndEngine: collision of two sprites

旧城冷巷雨未停 提交于 2019-12-10 15:54:07

问题


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.


回答1:


Here is a nice example of pixel perfect Detection,

AndEngine - Pixel Perfect Detection




回答2:


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;
}


来源:https://stackoverflow.com/questions/7386282/andengine-collision-of-two-sprites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!