使用Box2D,cocos2d和Physics Editor在视网膜中显示PTM_RATIO [英] PTM_RATIO in retina display using Box2D, cocos2d and Physics Editor

查看:177
本文介绍了使用Box2D,cocos2d和Physics Editor在视网膜中显示PTM_RATIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在子画面上绘制身体形状时遇到很多问题.我尝试了很多组合,但无法使其在视网膜显示屏上正常工作.

I’m having a lot of problems trying to draw body shapes on my sprites. I tried a lot of combinations but I’m not able to make it work on retina display.

我仅支持iPhone视网膜和iPad(而不是视网膜).

I’m supporting iPhone retina only and both iPad (retina and not retina).

A. PTM_RATIO

A. PTM_RATIO

第一个问题是,如果我要在所有设备中使用相同的物理原理,则每个设备都使用哪个PTM_RATIO.我读了几篇文章,但我不知道PTM是代表"点对点"还是"像素对点".

The first question is which PTM_RATIO use for each device if I want the same physics in all devices. I read several posts and I don’t know if PTM stands for "Point To Meter" or "Pixel To Meter".

¿哪一列是正确的?

B.物理

我想使用不同形状的不同图像绘制地面,但是为此,我使用了两种最简单的图像.一张200x200px的图片,但主体仅采用了200x80px下方的矩形.

I want do draw a ground using different images of different shapes, but for this purpose I’m using two of the simplest type. An image of 200x200px but the body only takes the lower 200x80px rectangle.

为了保持子画面的外观,我在iPhone和iPad SD上使用200x200px的图片,在iPad HD上使用400x400.然后,我尝试为红色区域定义一个主体.我使用Physcs Editor尝试了所有可能的组合,并且尝试使用Vertex Helper手动进行了组合.我使它适用于iPad SD,但不适用于视网膜显示屏(始终为x2).

To keep the sprite aspect I use 200x200px art for iPhone and iPad SD and 400x400 for iPad HD. Then I try to define a body for the red area. I tried all possible combinations using Physcs Editor and I tried to do it manually also with Vertex Helper. I made it work for iPad SD but not for retina display (always x2).

我的版本:

int num = 4;
        b2Vec2 verts[] =
        {
            b2Vec2(-100.0f / 100.0, -20.0f / 100.0),    //A
            b2Vec2(100.0f / 100.0, -20.0f / 100.0), //B
            b2Vec2(100.0f / 100.0, -100.0f / 100.0),    //C
            b2Vec2(-100.0f / 100.0, -100.0f / 100.0),   //D
        };

我除以100是因为我使用了200x200的艺术作品.我试图除以PTM_RATIO和PTM_RATIO * CC_CONTENT_SCALE_FACTOR()而没有有用的结果.我尝试使用400x400的美术作品来做……我想我尝试了所有可能的组合.

I divided by 100 because I used the 200x200 art. I tried to divide by PTM_RATIO and by PTM_RATIO * CC_CONTENT_SCALE_FACTOR() without useful results. I tried to do it with the 400x400 art… I think I tried all the possible combinations.

出于调试目的,我使用了cocos2d + Box2d模板随附的GLES-Render类.

For debugging purposes I’m using GLES-Render class provided with cocos2d+Box2d template.

我图层中的draw方法:

The draw method in my layer:

- (void) draw
{
    [super draw];
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    kmGLPushMatrix();
    world->DrawDebugData();
    kmGLPopMatrix();
}

我应该使用吗?:

 glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1);

我真的很困.

Pysics编辑器组合和结果:

Pysics Editor combinations and results:

宏定义: 适用于iPhone的50 PTM_RATIO和适用于iPad 2和iPad 3的100 PTM_RATIO.

Macro definition: 50 PTM_RATIO for iPhone and 100 PTM_RATIO for iPad 2 and iPad 3.

推荐答案

第一件事:对PTM_RATIO的需求:Box2D使用任意坐标系.据说以米为单位,但这是一个任意定义,因为Box2D中的1米无法用标尺测量.只有将Box2D实体的位置转换到屏幕上时,您才能获得可测量的距离.而该距离取决于屏幕的大小及其像素密度(ppi).

First things first: the need for PTM_RATIO: Box2D uses an arbitrary coordinate system. It is said to be in meters but this is an arbitrary definition because 1 meter in Box2D can not be measured with a ruler. It is only when you translate the positions of Box2D bodies onto a screen that you get a measurable distance. And that distance depends on the size of the screen and its pixel density (ppi).

对于Retina,您不需要其他PTM_RATIO!

这是因为在UIKit和cocos2d(但不是纯OpenGL)中,Retina iPhone与非Retina iPhone具有相同的磅值. iPad和iPad Retina的情况相同,它们的点坐标大小均为1024x768-iPad Retina内部仅使用4个像素表示一个点. UIKit和cocos2d都使用点坐标来定位其视图,因此您完全不必担心转换为像素或内容比例因子.

This is because in UIKit and cocos2d (but not pure OpenGL) a Retina iPhone has the same point size as a non-Retina iPhone. Same goes for iPad and iPad Retina, they both have a point coordinate size of 1024x768 - internally the iPad Retina simply uses 4 pixels to represent a single point. Both UIKit and cocos2d use point coordinates to position their views so you don't need to worry at all about converting to pixels or the content scale factor.

这意味着对非Retina和Retina设备使用相同的PTM_RATIO.

That means use the same PTM_RATIO for non-Retina and Retina devices.

但是,您可能想在iPhone和iPad之间更改PTM_RATIO,例如,如果您不希望iPad版本的应用显示更大的屏幕部分,而是提供2倍的缩放视图,那么您只需在iPad上使用两次PTM_RATIO.这意味着在iPhone上其位置时间PTM_RATIO转换为480x320的主体在iPad上将不会在480x320处,而是在960x640处,因为iPad的PTM_RATIO的高度是其两倍.同样,只有当您尝试在iPhone和iPad屏幕(大约大2倍)之间使用非常相似的比例时,才是您想要的-否则不要更改PTM_RATIO.

You may however want to change the PTM_RATIO between iPhone and iPad, for example if you don't want the iPad version of your app to show a much larger portion of the screen but instead provide a 2x zoomed view into your scene, then you would simply use twice the PTM_RATIO on iPad. Meaning a body on an iPhone whose position times PTM_RATIO converts to 480x320 will not be at 480x320 on the iPad but rather at position 960x640 because the iPad's PTM_RATIO is twice as high. Again, only if you attempt to use a very similar scale between iPhone and iPad screens (which are roughly 2x bigger) is what you want - otherwise don't change PTM_RATIO.

现在,VertexHelper并没有帮助您,因为它不考虑或不包含PTM_RATIO,因此很容易出错(旁边是繁琐的工具).而是依靠使用PhysicsEditor及其形状缓存.确保已将PhysicsEditor中的PTM_RATIO设置为您在应用程序中使用的PTM_RATIO.

Now VertexHelper doesn't help you with this because it doesn't consider or include PTM_RATIO, so its easy to make mistakes with it (beside it's a cumbersome tool). Instead rely on using PhysicsEditor and its shape cache. Make sure you have set the PTM_RATIO in PhysicsEditor to the same you're using in your app.

总而言之,请勿在任何地方使用CC_CONTENT_SCALE_FACTOR来增加头寸.是的,某些GL功能需要它,但是已经提供给您,它们可以正常工作而无需任何更改.

By all means don't use CC_CONTENT_SCALE_FACTOR anywhere to multiply positions. Yes, some GL functions need it but those are already given to you and they work properly without requiring any changes.

这篇关于使用Box2D,cocos2d和Physics Editor在视网膜中显示PTM_RATIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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