CCRenderTexture大小和位置在Cocos2d-iphone [英] CCRenderTexture size and position in Cocos2d-iphone

查看:187
本文介绍了CCRenderTexture大小和位置在Cocos2d-iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用CCRenderTexture像素完美的碰撞检测,如本论坛中所述:

I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:

http://www.cocos2d-iphone.org/forum/topic/18522/page/2

代码as is工作,我已经将它与我的项目集成了

The code "as is" works, and I have integrated it with my project

但是我遇到了麻烦,其他事情讨论:
如果我创建的renderTexture的任何大小小于屏幕尺寸,碰撞检测不能正常工作 - 似乎显示碰撞时,sprite彼此接近(<15px)但实际上没有发生碰撞。

But I am having trouble doing some of the other things discussed: If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.

此外,我在更改渲染纹理的位置时遇到问题。不管我指定的位置,它似乎从左下(0,0)到宽度&高度指定。我遵循此职位:

Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:

http ://www.cocos2d-iphone.org/forum/topic/18796

但它不能解决我的问题。我仍然得到坏的冲突像上面指定的。此外,我在列表中提到的第一个帖子包含许多用户的纹理已调整为10x10,并重新定位到屏幕上的重新定位的评论。

But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.

有人有任何示例代码,所以我可以看到我做错了什么?我只是使用样板代码:

Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:

    CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];

    _rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
    [[RIGameScene sharedGameScene]addChild:_rt];
    _rt.visible = YES;

我使用cocos2d-iphone 1.0.1

I use cocos2d-iphone 1.0.1

推荐答案

在调用draw或visit之前,需要将要绘制的sprite移动到renderTexture的区域。移动renderTexture不会改变_rt.sprite的位置。

You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.

交叉矩形必须​​在renderTexture的区域,否则会得到不准确的碰撞。

The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.

你似乎不能改变_rt.sprite的位置。

It seems that you cannot change the position of _rt.sprite.

我使用的解决方案是确定原点x,y),并且将碰撞子画面偏移那么多。这将确保交叉矩形的原点为0,0。然后我再次计算交叉矩形(确保交叉矩形的原点为0,0)。然后我按照论坛帖子中的说明。

The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.

在确定渲染纹理的尺寸时,我确保它们至少与交叉矩形一样大,而I确保交叉矩形完全在渲染纹理内。这种方式有准确的碰撞。如果即使交叉框的一部分在渲染纹理之外,我得到不准确的碰撞,因此在绘制到渲染纹理之前,请确保移动您打算访问的sprite,以便交叉框完全在渲染纹理内。

When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.

记住在完成后移动精灵回来。 :)

Remember to move the sprites back after you're done. :)

这篇关于CCRenderTexture大小和位置在Cocos2d-iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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