How to do pixel perfect collision on many sprites in cocos2d?

隐身守侯 提交于 2019-12-07 16:41:03

问题


in my app I have a player and many enemies (about 100+)..I don't want to use CGRects because it doesn't fits to the enemies. Is there any example how to do pixel perfect collision detection in cocos2d with many sprites?

Some explaination would be great ;)

Thank you very much!

EDIT: I'm using CCSprite and .png files. the png has transparency but it should only detect a collision on non-transparency pixels.

Edit: My enemies are round.


回答1:


Circle-circle collision is the easiest.. And computing is the fastest.. I hope you know the radius of the player and radius of enemy.. Let 20 be radius of player and 10 be radius of enemy.. A simple calculation would be:

float dx = player.spr.x - enemy.spr.x;
float dy = player.spr.y - enemy.spr.y;
float dxy = dx*dx + dy*dy;
float collisionRad = (20+10)*(20+10);

if(dxy<= collisionRad)
{
//collision
}

We are calculating distance between 2 points using the Pythagorean Theorem.. http://en.wikipedia.org/wiki/Pythagorean_theorem



来源:https://stackoverflow.com/questions/5735575/how-to-do-pixel-perfect-collision-on-many-sprites-in-cocos2d

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