Kobold2D Pixel Perfect Collision Detection - Device issue

本秂侑毒 提交于 2019-12-08 12:10:26

问题


I am very fortunate to be using a free engine, Kobold2d (built on top of cocos2d).

I, like others, are experiencing issues with the KKPixelMaskSprite class and the Pixel Perfect collision detection on our devices. The following results come from both my personal project, as well as the Kobold2d pixel perfect collision detection sample project.

Everything works fine in the iOS simulators (ipad or iphone), but crashes on any device I deploy to. I have tried deploying in both debug and release modes, changed the build target to 4, 5, and 6, all crashing the same.

What's interesting is, if I run the application using "Profile" on the device, it works as expected.

My team and I have successfully extended Kobold2d's extensions to perform pixel perfect collisions on sprite frames. We have also extended the CCSpriteFrame class to hold it's respective pixel mask and update the sprite automatically when the sprite's frame is changed.

Is there anyone who can help us resolve the issue between the pixel perfect collision detection working on the simulator and not on the device (except when running a profile build)? We would love to share our code and push upstream so all can benefit. Steffen did an excellent job and I hate for such an awesome class to fail short due to a discrepancy between simulator and device.

It fails with an assert message: "other pixelMask index out of bounds for x,y".. I have a feeling it has to do with the intersecting rectangles origin becoming skewed between checks, but I am not sure.

I have spent weeks trying to figure this out, and am willing to try anything anyone has to offer.

I am using the latest Kobold v.2.1.0 and am using OSX Mountain Lion with Xcode 4.6.1.

Update:

Further testing in the profiler detected the following memory leaks when colliding two sprites using the sample project:

Leaked Object   #   Address Size    Responsible Library Responsible Frame
Malloc 144 Bytes    1   0x2b41f0    144 Bytes   QuartzCore  mem_alloc
CALayer 1   0x1e061730  48 Bytes    UIKit   -[UIView _createLayerWithFrame:]
UIImageView 1   0x1e0615f0  96 Bytes    UIKit   -[UIClassicController _newChromeViewForOrientation:]

Not sure if that, helps.. but I will continue to investigate..

Update 2: Initial Solution

In the KKPixelMaskSprite.m file, if you take a look inside the function:

-(BOOL) pixelMaskIntersectsPixelMaskSprite:(KKPixelMaskSprite*)other

look for the origin offset variables:

NSUInteger originOffsetX = intersectOther.origin.x - intersectSelf.origin.x;
NSUInteger originOffsetY = intersectOther.origin.y - intersectSelf.origin.y;

Now, we can see that these are (of course) unsigned variables. By debugging through a device (retina or non-retina), I took note that the values of intersectSelf.origin.x and/or intersectSelf.origin.y have cases in which they are smaller than their respective intersectOther.origin.x and/or intersectOther.origin.y values.

These cases are currently handled by the unsigned variables looping around to there largest values and counting down, thus accounting for the "other pixelmask index out of bounds" errors.

Thus, I propose that negative offset values are indeed valid cases, and should be respected by changing the aforementioned variable lines of code to be:

NSInteger originOffsetX = intersectOther.origin.x - intersectSelf.origin.x;
NSInteger originOffsetY = intersectOther.origin.y - intersectSelf.origin.y;

So far, we have tested this change on both retina and non retina simulators, as well as iPad devices (2nd gen and 3rd gen) with success. We will continue testing to see if this fix holds but so far it's looking very promising and giving us expected results!

I hope this information helps people out!

来源:https://stackoverflow.com/questions/15697152/kobold2d-pixel-perfect-collision-detection-device-issue

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