cocos2d中如何对多个精灵进行像素完美碰撞?

How to do pixel perfect collision on many sprites in cocos2d?(cocos2d中如何对多个精灵进行像素完美碰撞?)

本文介绍了cocos2d中如何对多个精灵进行像素完美碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

一些解释会很棒;)

非常感谢!

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

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

我的敌人是圆形的.

推荐答案

圆-圆碰撞最简单..计算最快..希望你知道玩家的半径和敌人的半径..让20是玩家的半径和 10 是敌人的半径.. 一个简单的计算是:

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
}

我们正在使用勾股定理计算 2 点之间的距离.http://en.wikipedia.org/wiki/Pythagorean_theorem

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

这篇关于cocos2d中如何对多个精灵进行像素完美碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:cocos2d中如何对多个精灵进行像素完美碰撞?

基础教程推荐