如何做像素完美碰撞cocos2d上的许多sprite? [英] How to do pixel perfect collision on many sprites in cocos2d?

查看:223
本文介绍了如何做像素完美碰撞cocos2d上的许多sprite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序我有一个播放器和许多敌人(约100 +)..我不想使用CGRects,因为它不适合的敌人。有没有任何例子如何在cocos2d与许多精灵进行像素完美的碰撞检测?



一些解释将是巨大的;)



非常感谢!



编辑:我使用CCSprite和.png文件。 png具有透明度,但它应该只检测非透明像素上的碰撞。



编辑:我的敌人是圆形的。

解决方案

碰撞是最简单的..计算是最快的..我希望你知道球员的半径和敌人的半径..让20是球员的半径,10是敌人的半径..一个简单的计算将是:

  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
}

我们使用毕达哥拉斯定理计算两点之间的距离。
http://en.wikipedia.org/wiki/Pythagorean_theorem


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.

解决方案

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

这篇关于如何做像素完美碰撞cocos2d上的许多sprite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆