Cocos2d iPhone非矩形精灵触摸检测 [英] Cocos2d iPhone Non-Rectangular Sprite Touch Detection

查看:131
本文介绍了Cocos2d iPhone非矩形精灵触摸检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sprite中有一个带有TRIANGLE形状图形的项目。我把这些精灵安排在一个网格,使他们的矩形都重叠。作为sprite被触摸他们的z顺序正在改变(通过我)把他们放在zOrder的顶部。



我使用Cocos 0.8.1和触摸调度器方法。我有触摸的工作,但显然,与其他精灵重叠的隐藏精灵不能被触摸。



问题是 rect方法基于sprite的矩形,但是图像是一个三角形,我想问任何人是否知道一个Cocos友好的方法来测试图像本身是否被命中。



我似乎记得这是一个流行的击中测试的方法,当天,但我找不到任何参考,如何可以在Cocos / iPhone土地。



目标是当触摸图像像素时,只响应触摸,而不仅仅是包含sprite的rect。

解决方案

$ p

我最初发布的可以使用NSBezierPath,但是可以使用NSBezierPath。似乎不在iPhone套件上,只有在Mac上。



使用 CGPathCreateMutable()创建一个新路径, const CGPath * (也称为 CHPathRef

然后使用 CGPathAddRect CGPathAddLines 创建我们的路径。

CGPathContainsPoint



或者,您可以创建一个客户函数(因为你使用三角形)只是做一个简单的计算来检查你的点是否在内部数学的位应该做的伎俩(虽然当你旋转形状它会略微更复杂,我写得轻微,因为你可以只是相对于形状的原点旋转触摸点,并进行命中检测)



对于三角形:

  C 
/ \\
/ __ \
AB
触摸点是P

使用以下算法,您应该能够找到触摸:

  / *首先测试一个盒子, / 
if(Py> C.y || P.Y。 A.y || P.x, A.x || P.X> B.x)
return false; // false如果P落在盒子外面

/ *然后检查它是否在形状内* /
/ *将三角形分割成2部分,围绕C点的轴* / P $ <$ C $ /
if(Px 的左边,如果(Py>((Cy-Ay)/(Cx-Ax)) * Px)
return false; //该点在三角形的边上AC
else //如果点P的x值大于或等于点C
if(Py> Cy - ((Cy-By)/ (Bx-Cx))*(Px-Cx))
return false; //该点在三角形的边上BC

return true; //该点必须在三角形中,所以返回true。

以上是干编码,但应该正确。



上面的代码只适用于我绘制的形状中的三角形(其中Cx在Ax和Bx之间,A和B在同一高度,但低于C)。当然你可以修改它来测试任何形状,但是,你必须使用可用的 CGPath 来衡量它。



如果你没有得到它,或者它有错误,让我知道!


Have a project with a TRIANGLE shaped graphic in a sprite. I am arranging these sprites in a grid so that their rectangles are all overlapping. As sprites get touched their z-order is being changed (by me) to put them on the top of zOrder.

I am using Cocos 0.8.1 and the touch dispatcher method. I have touches working but obviously the "hidden" sprites which are overlapped by other sprites are not able to be touched.

The problem is that the "Is the touch in my rect" method is based on the rectangle of the sprite, but the image is a triangle, and I would like to ask if anyone knows a Cocos-friendly method of testing whether the image itself is being hit.

I seem to remember this was a popular method of hit testing back in the day but I can't find any reference to how it might be done in Cocos/iPhone Land.

The goal is to only respond to touches when an image pixel is touched, not just the rect containing the sprite.

解决方案

Instead of testing against a box you can of course test against any shape that you wish.

I initially posted you can use an NSBezierPath, but appearantly that ins't available on the iPhone kit, only on Macs. Instead, on the iPhone you can use CGPath.

Create a new path by using CGPathCreateMutable() which returns a const CGPath * (also known as CHPathRef
Then use CGPathAddRect or CGPathAddLines to create our path.
CGPathContainsPoint will test if your point was in the shape.

Alternatively, you can create a customer function that (since you use triangles) just does a simple calculation to check whether your point is inside the triangle shape. Bit of math should do the trick (though when you rotate the shape it'll be slightly more complicated. I write slightly, as you can then just rotate the touch point relative to the shape's origin and do the hit detection)

For a triangle:

   C
  /\
 /__\
A    B
point of touch is P

With the following algorithm you should be able to find the touch:

/* first test against a box, for performance */
if( P.y > C.y || P.y < A.y || P.x < A.x || P.X > B.x )
    return false; // false if P falls outside "the box"

/* then check if its within the shape */
/* split the triangle into 2 parts, around the axle of point C */
if( P.x < C.x ) // if the x value of point P is on the left of point C
    if( P.y > ((C.y -A.y) / (C.x - A.x)) * P.x )
        return false; // the point is above the triangle's side AC
else // if the x value of point P is greater than or equal to point C
    if( P.y > C.y - ((C.y - B.y) / ( B.x - C.x )) * ( P.x - C.x ) )
        return false; // the point is above the triangle's side BC

return true; // the point must be in the triangle, so return true.

The above is dry coded, but should be correct.

The above only works against a triangle in the shape as I drew it (where C.x is between A.x and B.x, and A and B are on the same height, but below C). Of course you can modify this to test against any shape, however, you have to weigh this against just using the available CGPath.

If you don't get it, or if it's faulty, let me know!

这篇关于Cocos2d iPhone非矩形精灵触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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